We need to understand the problem. It's about converting a date stored under an assumption that the era name (Heisei) continues indefinitely, to the actual date using the correct era name (Heisei until 2019-04-30, and new era from 2019-05-01). But the stored dates are given as "g y m d" where g is always "HEISEI" (since they stored under assumption that era is Heisei). And y is the year number within that era (Heisei years). But this "Heisei" era year counts from 1989-01-08 (the start of Heisei) and continues. However, we know that Heisei actually ended on April 30, 2019. So after that date, the correct era becomes a new era (unknown, represented by "?"). Also the first year of the new era starts on May 1, 2019. We need to convert the stored date to the correct era and year.
Important: The stored date is based on the assumption that the Heisei era continues, i.e., they use "Heisei year y" where year 1 corresponds to 1989-01-08 to 1990-01-07? Wait careful: In Japanese calendar, the first year of an era (gannen) is the year when the era begins, and it lasts until the end of that calendar year? Actually, Japanese imperial eras: When a new era starts, the year resets to 1. However, the numbering changes at the start of the era, and then increments on January 1 each year. But there's nuance: The first year of an era is from its start date to December 31 of that year, and then the second year starts on January 1. This is known as "gannen" rule. However, the problem says: "Normally, the first year of the era is written as the first year, but in the output of this problem, ignore this rule and output 1 as the year." So they simplify: they treat the year number as simply counting years from the start of the era, incrementing on the anniversary of the start? But from description: "the date uses the Japanese calendar (the Japanese calendar that expresses the year by the era name and the number of years following it). It is saved in the database in the format of "d days". Since this storage format cannot be changed, JAG saves the date expressed in the Japanese calendar in the database assuming that the era name does not change, and converts the date to the format using the correct era name at the time of output." So they stored dates using the Heisei era, with the year number according to the standard Japanese calendar? Or with a simplified system? Let's read carefully.
They say: "In the system developed by the ACM-ICPC OB / OG Association (Japanese Alumni Group; JAG), the date uses the Japanese calendar (the Japanese calendar that expresses the year by the era name and the number of years following it). It is saved in the database in the format of "d days". Since this storage format cannot be changed, JAG saves the date expressed in the Japanese calendar in the database assuming that the era name does not change, and converts the date to the format using the correct era name at the time of output. It was to be."
I'm not fully clear, but perhaps they store the date as "g y m d" where y is the year number in that era, based on the conventional Japanese calendar (i.e., after the first year, the year increments on Jan 1). But they also say: "Normally, the first year of the era is written as the first year, but in the output of this problem, ignore this rule and output 1 as the year." So for output, they want to output year number simply as "1" if it's the first year of the era, not "1" as the representation? Wait: In normal Japanese representation, the first year is often written as "1" (or "元年") but here they want to output 1 for the first year as well. That means the output year number is just the count of years since start of era? Actually they say: ignore the rule (that the first year is written as first year) and output 1 as the year. That suggests that normally the first year is something like "gannen" and they would output "1" anyway? The statement is ambiguous. I think they mean: In normal Japanese calendar, the first year is often referred to as "first year" but sometimes as "gannen". But here we will simply output "1" for the first year. So output year number is just the number of years into the era, starting from 1 for the first year.
Thus we need to convert a given date (g, y, m, d) stored under the assumption that the era never ends (i.e., always Heisei) to the correct era given that Heisei ended on 2019-04-30. The date is valid in the sense that if we interpret the given Heisei year, month, day as a date in the actual calendar, what would it be? But there is twist: The stored dates are based on the Japanese calendar using Heisei era, with year numbers that follow the actual Japanese calendar rules? Or are they simply counting years from the start of Heisei (1989-01-08) with increments on the anniversary? The sample will clarify.
Let's analyze sample input and output.
Input: HEISEI 1 1 8 -> Output: HEISEI 1 1 8. This corresponds to actual Heisei 1, Jan 8, 1989. That's correct.
Input: HEISEI 31 4 30 -> Output: HEISEI 31 4 30. This is April 30, 2019? Heisei 31 started on Jan 1, 2019? Actually Heisei era: Started Jan 8, 1989. So Heisei 1 from Jan 8, 1989 to Dec 31, 1989. Heisei 2 from Jan 1, 1990 to Dec 31, 1990. So Heisei 31 would be from Jan 1, 2019 to Dec 31, 2019? But Heisei ended on April 30, 2019. So April 30, 2019 is still in Heisei 31. So it's correct to output HEISEI 31 4 30.
Input: HEISEI 31 5 1 -> Output: ? 1 5 1. So May 1, 2019 is after Heisei ended, so it becomes new era year 1. So the year number becomes 1 (first year of new era). So they treat the new era start as May 1, 2019. The output era is "?" and year 1.
So far straightforward.
Now input: HEISEI 99 12 31 -> Output: ? 69 12 31. That suggests that the date stored as Heisei 99, Dec 31. If Heisei continued, that would be a date far in future. We need to convert to actual date using correct eras. Since Heisei actually ended after 31 years? Actually Heisei lasted from 1989-01-08 to 2019-04-30. That's 31 years (Heisei 1 to 31) but only 4 months into Heisei 31. After that, new era starts. So the stored Heisei 99 Dec 31 corresponds to a date that is many years after Heisei ended. We need to compute the offset from the start of Heisei to that date, then subtract the length of Heisei (up to April 30 2019) to get the number of days into the new era, and then compute the new era year, month, day. But note: The output year for the new era is 69. So for Heisei 99 Dec 31, new era year is 69. That suggests that the new era started on May 1, 2019 (which is day 1 of new era year 1), and that by the time we reach the date corresponding to Heisei 99 Dec 31, it is the 69th year of the new era? Let's compute: Heisei years 1..31 complete? Actually Heisei 31 includes dates from Jan 1 2019 to Dec 31 2019, but Heisei ended on Apr 30. So the period from Jan 1 2019 to Apr 30 2019 is in Heisei; May 1 to Dec 31 2019 would have been Heisei 31 if Heisei continued but is now in new era. So Heisei 31 has some days that are in Heisei (Jan1-Apr30) and some that are in new era (May1-Dec31). For a date like Heisei 31 5 1, it's in new era. So effectively, the stored Heisei year count continues beyond the actual end. So we need to map each stored date to a Gregorian date. The Gregorian date for a stored Heisei y, m, d can be computed as: start of Heisei (Jan 8, 1989) + (years offset based on Japanese calendar counting). But we need to know how Japanese calendar years work: The first year (Heisei 1) is from Jan 8, 1989 to Dec 31, 1989. Then Heisei 2 from Jan 1, 1990 to Dec 31, 1990, etc. So the mapping from (y, m, d) to Gregorian date is: if y=1, then the date is in 1989, but only from Jan 8 onward. For months/days before Jan 8, they would not exist in Heisei 1 because Heisei started Jan 8, but the problem says they won't give invalid dates. So we can compute the Gregorian year as: Basically, Heisei year y corresponds to Gregorian year: 1988 + y? Let's test: Heisei 1 -> 1989 = 1988+1. Heisei 31 -> 2019 = 1988+31. Yes, seems Gregorian year = 1988 + y, for Heisei years after the first? But check: Heisei 1 includes dates from Jan 8, 1989 to Dec 31, 1989. So for any date in Heisei 1, the Gregorian year is 1989 = 1988+1. For Heisei 2, it's 1990 = 1988+2. So indeed, Gregorian year = 1988 + y. However, caution: For dates in Heisei 1 that are before Jan 8? Not given. So it's consistent.
Thus, given stored Heisei y, m, d, the Gregorian date is simply: year = 1988 + y, month = m, day = d. But is that always valid? Let's verify: For Heisei 1 Jan 8, it's 1989-01-08. Good. For Heisei 31 Apr 30, it's 2019-04-30. For Heisei 31 May 1, it's 2019-05-01. For Heisei 99 Dec 31, it's (1988+99) = 2087-12-31. That is a Gregorian date. Now, the actual era divisions: Heisei ended on 2019-04-30. So all dates on or before 2019-04-30 are Heisei; dates after are new era. For a Gregorian date after 2019-04-30, we need to compute the new era year number. The new era starts on 2019-05-01. So we need to compute the year count in the new era based on the date. But how does the new era count years? They said: "Normally, the first year of the era is written as the first year, but in the output of this problem, ignore this rule and output 1 as the year." So we need to determine the year number according to the same simplified rule as Heisei? That is, year 1 of new era is from its start (May 1, 2019) to ... when does it increment? For Heisei, they used the Gregorian year mapping: year = Gregorian year - 1988. That is, they effectively treat the era as starting at the beginning of the Gregorian year that contains the start? Wait: Heisei started Jan 8, 1989, yet Heisei 1 includes Jan 8- Dec 31 1989, and Heisei 2 starts Jan 1 1990. This is the actual Japanese system: the first year runs until the end of the Gregorian year, and then the year number increments on Jan 1. So the mapping is: For a given Gregorian date (Y, M, D), the Heisei year is: if Y < 1989 => not Heisei; else if Y == 1989 then if date >= Jan 8 -> Heisei 1 else not Heisei; else (Y > 1989) then Heisei year = Y - 1988, because from 1990 onward, the Heisei year equals Gregorian year - 1988. For 1989, it's also Gregorian year - 1988 = 1, but only after Jan 8. So that mapping works if we treat Heisei 1 as covering the whole year 1989 from Jan 8 onward. The year number increments on Jan 1 each year after the first year. That's standard.
Thus, for the new era, the same rule applies: It starts on May 1, 2019. So the first year of the new era (year 1) is from May 1, 2019 to Dec 31, 2019. Then year 2 starts on Jan 1, 2020, year 3 on Jan 1, 2021, etc. So the new era year number for a Gregorian date (Y, M, D) after the start is: if Y == 2019 and date >= May 1 -> year 1; else if Y >= 2020 -> year = Y - 2018? Let's compute: For 2020, year should be 2? Since 2019 is year 1, 2020 is year 2. So year = Y - 2018? Check: Y=2020 => 2020-2018=2 correct. Y=2021 =>3. Y=2019 => date after May1: year=1 = 2019-2018=1? 2019-2018=1, but that only works for dates after May1. However, if Y=2019 and date < May1, it's not new era. So we can compute: new_era_year = Y - 2018, provided date >= May1 for Y=2019, but for Y>2019 it's automatically >= May1? Actually for Y>2019, the whole year is in new era (since new era started in 2019). So for Y>=2020, new_era_year = Y - 2018. For Y=2019, only from May1 onward, and then new_era_year = 1. So consistent: new_era_year = Y - 2018, but only valid for dates on or after May1 2019. However, note that if we apply that to 2019-01-01, it would give 1 incorrectly. But we will only apply to dates after cutoff.
Thus, the conversion process: For input (HEISEI, y, m, d), compute Gregorian date: G_year = 1988 + y, G_month = m, G_day = d. Check if this Gregorian date is <= April 30, 2019 (i.e., before May 1, 2019). If yes, output "HEISEI" and same y, m, d? Wait careful: For Heisei dates before the cut-off, they are still Heisei, but the year number in output should be the same as input? But the output year number for Heisei era: Should it be the original y? In sample, for HEISEI 31 4 30 they output HEISEI 31 4 30. Yes. So for Heisei era, output the same y, m, d.
For dates after cutoff, they belong to new era. For these, we need to compute the new era year number. The new era year number should be computed based on the Gregorian date according to the same rules as Heisei (i.e., first year from May1 to Dec31 2019, then increment on Jan1). So we need to compute new_era_year = ?.
From sample: HEISEI 31 5 1 => Gregorian 2019-05-01 => new era year 1. Output ? 1 5 1. Good.
HEISEI 99 12 31 => Gregorian 2087-12-31 => compute new era year: Starting from 2019-05-01. 2019 year 1 (2019), 2020 year 2, 2021 year 3, ... up to 2087. Let's compute: For year Y >= 2020, new_era_year = Y - 2018. For Y=2087, Y-2018 = 69. Output ? 69 12 31. So matches.
HEISEI 38 8 30 => Gregorian year = 1988+38 = 2026. So 2026-08-30. Compute new era year: 2026 - 2018 = 8. Output ? 8 8 30. Sample output: ? 8 8 30. Yes.
HEISEI 98 2 22 => Gregorian year = 1988+98 = 2086. 2086-2018 = 68. Output ? 68 2 22. Sample shows ? 68 2 22. Yes.
HEISEI 2 3 26 => Gregorian year = 1990-03-26? Actually 1988+2 = 1990, month 3 day 26 => 1990-03-26. This is before 2019-04-30, so Heisei. Output HEISEI 2 3 26. Sample matches.
HEISEI 28 4 23 => 1988+28 = 2016-04-23, before cutoff, output HEISEI 28 4 23. Sample matches.
So the conversion is straightforward: convert stored Heisei date to Gregorian by adding 1988 to the year, keep month/day, then compare with cutoff (2019-05-01). If <= April 30, 2019 -> Heisei, else compute new era year = Gregorian year - 2018. But careful: For dates in 2019 after April, Gregorian year = 2019, new era year = 1 (since 2019-2018 = 1). So formula works for those as well, because 2019-2018=1. So we can simply compute new_era_year = G_year - 2018. But only if G_date >= 2019-05-01.
But need to consider edge cases: For dates exactly on May 1, 2019, it's new era. For dates before, it's Heisei.
Thus algorithm: For each line, if it's "#", end. else parse g, y, m, d (g is always "HEISEI"). Compute G_year = y + 1988. Then if date is less than or equal to 2019-04-30, output "HEISEI" y m d. Else output "?" (G_year - 2018) m d.
But wait: check for Heisei 31 4 30 -> G_year=2019, date=2019-04-30 which is <= cutoff, so Heisei. Good. For Heisei 31 5 1 -> G_year=2019, date=2019-05-01 > cutoff, so new era year = 2019-2018=1. Good.
But what about Heisei dates that are before the start of Heisei? The problem says: "When converted correctly as the Japanese calendar, the date on which the era before Heisei must be used is not given as a data set." So all dates are within Heisei or later, i.e., y>=1 and date >= 1989-01-08. So we don't need to handle before Heisei.
Potential nuance: The mapping from Heisei year to Gregorian year is y+1988 only if we assume that Heisei year 1 corresponds to 1989, which is true. But is there any complication about the start date Jan 8? For dates in Heisei 1 that are before Jan 8, they would not be given as input. So it's fine.
Thus the solution is simple.
But is there any scenario where the cutoff date falls within Heisei 31? Yes, April 30, 2019. So all dates in Heisei 31 before May 1 are Heisei, after are new era.
Thus we can implement.
But note: The problem mentions "The date uses the Japanese calendar ... It is saved in the database in the format of "d days"." Actually "d days" might be a misprint? Possibly they store as "g y m d" but they said "d days". Not important.
Also they say "Normally, the first year of the era is written as the first year, but in the output of this problem, ignore this rule and output 1 as the year." This aligns with using simple increment: year = G_year - base_year + 1? For Heisei, base_year = 1988? Actually Heisei year = G_year - 1988 for G_year >= 1989 and date >= Jan 8 for 1989. But they output same y as input for Heisei, which is consistent.
For new era, we output G_year - 2018. But we need to verify that for dates in 2019 after May 1, that is 2019 - 2018 = 1, correct.
But wait: What about dates in 2019 after May 1 but before the end of the year: that's year 1. For 2020, it's year 2. So indeed formula.
Thus solution: for each dataset, compute G_year = y + 1988. Compare (G_year, m, d) with (2019, 4, 30). Use standard date comparison: if G_year < 2019, then definitely Heisei. If G_year == 2019, then if m < 4, Heisei; if m == 4, then if d <= 30, Heisei; else new era. If G_year > 2019, new era.
If Heisei: output "HEISEI " + y + " " + m + " " + d.
If new era: new_y = G_year - 2018. Output "? " + new_y + " " + m + " " + d.
That's all.
But wait: Are there any dates that are exactly on 2019-04-30? That is Heisei.
Check sample: HEISEI 31 4 30 => Heisei.
Thus solution.
Let's also test other boundary: HEISEI 31 4 31? But April 31 doesn't exist. Not given.
HEISEI 31 5 1 => new era.
Ok.
Thus the problem is trivial.
But maybe there is nuance: They said "Dates that do not exist in the Japanese calendar, such as February 30, are not given as a dataset." So we don't need to validate.
Thus solution: read lines until '#', parse, compute.
But careful: The input lines may have extra spaces? Probably not.
Implementation: In C, read string for era, but era always HEISEI. Could skip checking era but we should read it.
Now, is there any case where the stored Heisei year is such that the Gregorian date would be before Heisei started? They said not given. So fine.
But wait: The problem statement says: "When converted correctly as the Japanese calendar, the date on which the era before Heisei must be used is not given as a data set." That means no dates that would be before Heisei. So y must be at least 1, and date must be >= 1989-01-08. But maybe Heisei 1 1 1? That would be Jan 1, 1989, but Heisei started Jan 8, so Jan 1 is invalid. They won't give that.
Thus no need to check.
Now, are there any tricky points about leap years? The date conversion from Heisei to Gregorian is direct because month and day are same. Since the stored date is valid in Gregorian (since it's a valid date in Japanese calendar, which uses Gregorian dates), it's safe.
Thus algorithm is correct.
But I want to double-check the mapping from Heisei year to Gregorian year. In actual Japanese calendar, Heisei 1 started on Jan 8, 1989, and ends on Dec 31, 1989. So for any date in Heisei 1, the Gregorian year is 1989. For Heisei 2, it's 1990, etc. So adding 1988 works. However, if we consider a date in Heisei 1 before Jan 8, it wouldn't exist, so fine.
Now, what about the new era? The new era started on May 1, 2019, which is within Heisei 31. So for dates that are after May 1, 2019, if they are stored as Heisei 31 or later, they map to Gregorian 2019 or later.
But is there any nuance about the new era's year numbering when crossing Gregorian year boundaries? For example, a date in new era: 2020-01-01: Gregorian year 2020, we compute new era year = 2020 - 2018 = 2. That matches expectation: first year from May 1 2019 to Dec 31 2019, second year from Jan 1 2020 to Dec 31 2020. Good.
Now, check if any stored Heisei year could be less than 31? They are given; some are less than 31, some greater. For those less than 31, we check if G_year < 2019? Since G_year = y+1988, y<=30 => G_year <=2018. So those are all before 2019, thus Heisei. For y=31, we need to check month/day. So condition is as described.
Thus the program is straightforward.
Edge Cases: The input may have trailing spaces. Use reading with scanf or similar.
Now, verify sample:
HEISEI 1 1 8 -> G_year=1989, less than 2019 -> Heisei, output HEISEI 1 1 8.
HEISEI 31 4 30 -> G_year=2019, m=4, d=30 -> <= cutoff -> Heisei, output same.
HEISEI 31 5 1 -> G_year=2019, m=5 >4 -> new era, new_y=2019-2018=1 -> ? 1 5 1.
HEISEI 99 12 31 -> G_year=2087 >2019 -> new era, new_y=2087-2018=69 -> ? 69 12 31.
HEISEI 38 8 30 -> G_year=2026 >2019 -> new_y=8 -> ? 8 8 30.
HEISEI 98 2 22 -> G_year=2086 >2019 -> new_y=68 -> ? 68 2 22.
HEISEI 2 3 26 -> G_year=1990 -> Heisei.
HEISEI 28 4 23 -> G_year=2016 -> Heisei.
Matches.
Thus solution is simple.
Potential pitfalls: The cutoff is April 30, 2019. But we must consider that Heisei ended at the end of April 30, i.e., that day is included. So condition is if (G_year < 2019) or (G_year == 2019 && (m < 4 || (m == 4 && d <= 30))) then Heisei else new era.
But also, if G_year == 2019 and m==4 and d==30 -> Heisei.
Now, what about G_year == 2019 and m==4 and d==31? Not exist.
Ok.
Thus code:
while (1) {
read line; if line starts with '#', break.
sscanf(line, "%s %d %d %d", era, &y, &m, &d);
// era is not needed, but we could assert it's "HEISEI".
int G_year = y + 1988;
if (G_year < 2019 || (G_year == 2019 && (m < 4 || (m == 4 && d <= 30)))) {
printf("HEISEI %d %d %d\n", y, m, d);
} else {
int new_y = G_year - 2018;
printf("? %d %d %d\n", new_y, m, d);
}
}
But careful: For G_year == 2019 and m == 5, it goes to new era, new_y = 1, correct. For G_year == 2019 and m > 4, new_y = 1.
Now, I think that's it.
But let's think if there is any scenario where G_year == 2019 and m == 4, d == 30? Heisei.
Now, consider date Heisei 31 4 30 is Heisei. That's correct.
Thus solution.
But wait, is there any possibility that the stored Heisei year can be something like 0? Not according to constraints (1 ≤ y ≤ 100). So fine.
Now, is there any nuance about the new era year counting starting from May 1, 2019? In the new era, the first year is from May 1, 2019 to Dec 31, 2019. The second year starts Jan 1, 2020. So the formula new_y = G_year - 2018 works because 2019-2018=1, 2020-2018=2, etc.
But is there any scenario where G_year is 2019 and month is before May but date is after April? No.
Thus correct.
Now, the problem says "The end of the input is represented by a line consisting of only one '#'." So we need to read lines and check if line == "#". Using scanf for first token: if token is "#"? But if the line is just "#", then scanf would read it as a string. We can read the first token; if it's "#", break; else we already have era and need to read the numbers. However, if we use scanf to read the era first, and if era is "#", then break. But careful: The line with "#" has only that character, no numbers. So if we try to read with scanf("%s", era) and it reads "#", then we need to detect and break before reading further. So we can do:
while (1) {
char era[10];
if (scanf("%s", era) != 1) break;
if (strcmp(era, "#") == 0) break;
int y, m, d;
scanf("%d %d %d", &y, &m, &d);
// process
}
That works because when we read "#", we break before reading numbers.
Alternatively, read whole line and parse.
Thus the solution is trivial.
But wait: There might be a nuance: The stored Heisei years are based on the assumption that the era name does not change, meaning they continue counting years with Heisei even beyond the actual end. But when we output for Heisei era, we output the same y, m, d. However, note that for dates after Heisei ended, we output a different year number for the new era. That's fine.
Now, consider a date like Heisei 31 4 30 is Heisei, but what about Heisei 31 4 30? Already considered.
Now, let's also consider the possibility of Heisei 31 5 1 being new era, which we did.
One more nuance: Could there be a date that is after the cutoff but falls in the new era but its year number might be computed incorrectly if we use simple subtraction? For example, if the new era started on May 1, 2019, then for a date in 2019 after May 1, it's year 1. That matches G_year-2018=1. For 2020, it's year 2, etc. So correct.
Thus solution is fine.
But wait, the problem says "ignore this rule and output 1 as the year." That means for the first year we output 1, not "1" as "first year"? That's what we do.
Now, also note that the input y can be up to 100, so G_year up to 2088. new_y up to 2088-2018 = 70. So small numbers.
Thus the problem is simple.
But I want to double-check if there is any trick about the mapping of Heisei years to Gregorian years when the Heisei year is 1 and the month is January before 8? They say those dates are not given. So fine.
Now, what about leap years? For February 29? They say invalid dates are not given, so if Heisei y leads to a Gregorian year that is not a leap year but date is Feb 29, they wouldn't give it. So we don't need to validate.
Thus solution.
But hold on: In the problem statement, they mention "d days" and "the date uses the Japanese calendar ... It is saved in the database in the format of "d days"." This might hint that they store the date as number of days from some epoch? Actually it says: "the date uses the Japanese calendar (the Japanese calendar that expresses the year by the era name and the number of years following it). It is saved in the database in the format of "d days"." Perhaps they meant "g y m d" format. Or maybe they store the date as the number of days since the start of the era? But the sample clearly uses "HEISEI y m d". So it's clear.
Thus answer is as above.
Now, I should produce a solution in C (since the problem is from some contest and likely expects C or C++). But we need to output exactly as sample.
I will write a program in C.
Implementation steps:
- Include standard I/O and string.h.
- While loop.
- Read era string. If era[0] == '#', break.
- Read y, m, d.
- Compute G_year = y + 1988.
- If G_year < 2019 -> Heisei.
- else if G_year == 2019:
if (m < 4) -> Heisei.
else if (m == 4):
if (d <= 30) -> Heisei.
else -> new era.
else -> new era.
- else (G_year > 2019) -> new era.
Simplify condition: if (G_year < 2019 || (G_year == 2019 && (m < 4 || (m == 4 && d <= 30))) ) -> Heisei.
Then output accordingly.
Note: The era string is "HEISEI", but we need to output "HEISEI" in that case.
Thus:
if (is_heisei) {
printf("HEISEI %d %d %d\n", y, m, d);
} else {
int new_y = G_year - 2018;
printf("? %d %d %d\n", new_y, m, d);
}
Check new_y calculation: For G_year=2019, new_y=1. For 2020, new_y=2. Good.
Now, think about potential off-by-one: The Heisei era ended on April 30, 2019, inclusive? i.e., April 30 is the last day of Heisei. So that's Heisei. So condition d <= 30 includes April 30.
Now, also consider if there is any date like Heisei 31 4 30 23:59? Not relevant.
Thus answer.
Now, is there any scenario where Heisei 31 5 1 is after the end, yes.
Now, test with some edge cases:
-
Heisei 31 4 30 -> Heisei.
-
Heisei 31 5 1 -> new era year 1.
-
Heisei 32 1 1 -> G_year=2020, new_y=2 -> output ? 2 1 1. Is that correct? Let's compute: Heisei 32 would be 2020-01-01 if Heisei continued. Since Heisei ended, this date falls in new era. New era year for 2020-01-01: According to rules, new era year 2 because first year was from May1 2019 to Dec31 2019, then Jan1 2020 starts year 2. So output ? 2 1 1. That matches formula: 2020-2018=2. Good.
-
Heisei 31 12 31 -> G_year=2019, new era? Since m=12 >4 => new era year 1, output ? 1 12 31. That is correct: Dec 31, 2019 is in new era year 1.
-
Heisei 31 4 30 is Heisei, but Heisei 31 5 1 is new era. So good.
Thus solution.
Now, about input format: The input consists of multiple datasets. Each line: g y m d. The last line is "#". So reading with scanf until EOF also works.
Implementation.
Potential pitfalls: The input might have trailing spaces or blank lines. But we can handle.
Now, finalize solution.
But wait: The problem might also require handling dates where the stored Heisei year is before Heisei started? They said not given.
Thus, answer is simple.
I'll produce code.
Now, about memory: No issues.
Thus final solution in C.
However, I'm ChatGPT generating explanation for the solution. But need to output final code? The request: "We need to output final answer." Typically we produce the solution code. So I'll produce C code.
But perhaps we need to consider the language: They accept C, C++, Java, etc. I'll produce C.
Now, I'll include necessary headers.
I'll use int for y, m, d.
Now, consider using scanf return values: For each dataset, we read era, then three ints. If era is "#", break; else we expect to read three ints. The input is well-formed.
Thus code:
#include <stdio.h>
#include <string.h>
int main() {
char era[10];
int y, m, d;
while (scanf("%s", era) == 1) {
if (strcmp(era, "#") == 0) break;
scanf("%d %d %d", &y, &m, &d);
int G_year = y + 1988;
if (G_year < 2019 || (G_year == 2019 && (m < 4 || (m == 4 && d <= 30)))) {
printf("HEISEI %d %d %d\n", y, m, d);
} else {
int new_y = G_year - 2018;
printf("? %d %d %d\n", new_y, m, d);
}
}
return 0;
}
That's it.
But ensure that for G_year == 2019 and m == 4 and d == 30, condition m<4 is false, m==4 and d<=30 true, so Heisei branch. Good.
One more check: For G_year == 2019 and m==4 and d==31 (impossible), it would go to new era because d<=30 false. But it's not given.
Thus final answer.
Now, I'll produce the final response with code and maybe explanation.
But the user requested "We need to output final answer." Possibly they want the code. I'll provide code.
But also, there might be a nuance: The problem originally in Japanese? Maybe they want to handle the case where the era name is given in uppercase? It is "HEISEI". We output "HEISEI" for Heisei era, and "?" for new era.
Thus code is correct.
One more subtlety: The Heisei era ended on April 30, 2019, but the new era began on May 1, 2019. That's exactly midnight? Yes.
Thus solution.
Now, I will produce the final answer. ```c
#include <stdio.h>
#include <string.h>
int main() {
char era[10];
int y, m, d;
while (scanf("%s", era) == 1) {
if (strcmp(era, "#") == 0) break;
scanf("%d %d %d", &y, &m, &d);
int gregorian_year = y + 1988;
// check if the date is within the Heisei period (up to 2019-04-30)
if (gregorian_year < 2019 ||
(gregorian_year == 2019 && (m < 4 || (m == 4 && d <= 30)))) {
printf("HEISEI %d %d %d\n", y, m, d);
} else {
int new_era_year = gregorian_year - 2018;
printf("? %d %d %d\n", new_era_year, m, d);
}
}
return 0;
}