We need to understand the problem: We have S' which is string with some '?'. We need to find S that satisfies:
- S contains T as contiguous substring.
- S is lexicographically smallest among strings that satisfy condition 1.
Additionally, S must be derived from S' by replacing each '?' with some lowercase letter. The original S' is the base.
So we are to find a string S that is a possible replacement of S' (i.e., for each position, if S'[i] is a letter, then S[i] must equal that letter; if '?' can be any lowercase letter) such that S contains T as substring, and among all such strings, S is lexicographically smallest.
If no such S exists, output UNRESTORABLE.
We are given S' and T in input, but the input format shows first line S, second line T. Actually description says: Input: S' (the string with ?) and T (the substring). Wait the sample input:
?tc????
coder
First line: ?tc???? is S', second: coder is T. Output: atcoder. Indeed atcoder contains "coder" as substring, and matches pattern: ? t c ? ? ? ? -> a t c o d e r? Actually "atcoder" is 7 letters: a t c o d e r. Pattern: position1: '?' -> a; pos2: t matches t; pos3: c matches c; pos4: '?' -> o; pos5: '?' -> d; pos6: '?' -> e; pos7: '?' -> r. So fits.
Second sample: ??p??d?? and abc -> output UNRESTORABLE.
Thus we need to find lexicographically smallest fill of '?' to have T as substring. Constraints: |S'|, |T| ≤ 50, so brute force possible.
Approach: We need to consider all possible positions where T can be placed as contiguous substring within S. For each possible start index i (0-indexed, such that i+ |T| <= |S'|), we check if it is compatible: For each j from 0 to |T|-1, if S'[i+j] is a letter, it must equal T[j]; otherwise it's '?' and can be set to T[j] (or possibly other letters but to satisfy substring we need at least that T appears, but we can set them to T[j] to make T substring). For positions outside the substring, we can fill '?' with 'a' (the smallest letter) to achieve lexicographically smallest string, because we want overall lex smallest. However careful: For positions inside the T placement we must set exactly the letters of T (or maybe we could set '?' to T[j] but we could also set to other letters if T appears anyway? Actually for T to be a contiguous substring, the substring from i to i+|T|-1 must exactly equal T. So at those positions, whatever we set must equal T. So if S' has a fixed letter that equals T[j], okay; if it's '?', we must set it to T[j] to make the substring T. If S' has a fixed letter that conflicts with T[j], then placement i is invalid.
Thus for each feasible placement i, we can construct a candidate S: For each position p in S':
- If p is within [i, i+|T|-1], set S[p] = T[p-i] (must).
- Else, if S'[p] is a letter, S[p] = S'[p] (fixed).
- Else (S'[p]=='?'), we can set S[p] to 'a' (since we want smallest lexicographically). However note: Could setting to 'a' ever conflict with something? No, because outside the T substring we don't have constraints, and fixed letters are given. So the smallest possible letter is 'a'.
Thus each feasible placement yields a candidate string. Among these candidates, we choose the lexicographically smallest one. If no feasible placements, output UNRESTORABLE.
But careful: Is it always optimal to set all non-substring '?' to 'a'? Yes, because we want the smallest string lexicographically; any other letter > 'a' would make it larger. However there may be interaction: The substring T itself may contain letters that are not 'a', but we have to use them anyway. So candidate from each placement is uniquely determined by that placement (since we set all other '?' to 'a').
Thus algorithm: Let lenS = |S'|, lenT = |T|. Initialize list candidates = []. For i from 0 to lenS - lenT:
valid = True
For j in 0..lenT-1:
if S'[i+j] != '?' and S'[i+j] != T[j]: valid=False; break
if valid:
construct S as list of chars:
for p in 0..lenS-1:
if i <= p < i+lenT:
S[p] = T[p-i]
else:
if S'[p] == '?': S[p] = 'a'
else: S[p] = S'[p]
candidates.append(''.join(S))
After loop, if candidates empty: print UNRESTORABLE else output min(candidates) lexicographically.
Check sample1: S'="?tc????", T="coder". lenS=7, lenT=5. Possible i: 0 to 2. Let's test:
i=0: substring positions 0-4: check: S'[0]='?' ok; S'[1]='t' vs 'c'? 't' != 'c' -> invalid.
i=1: positions 1-5: S'[1]='t' vs 'o'? No, T[0? wait mapping: i=1: j=0 -> p=1, T[0]='c'? Actually T="coder", indices: 0:c,1:o,2:d,3:e,4:r. So check: p=1: S'[1]='t' vs T[0]='c' -> invalid.
i=2: positions 2-6: S'[2]='c' vs 'c' ok; S'[3]='?' vs 'o' ok; S'[4]='?' vs 'd' ok; S'[5]='?' vs 'e' ok; S'[6]='?' vs 'r' ok. So valid. Then construct: p0: outside substring, S'[0]='?' -> 'a'; p1: outside, S'[1]='t' -> 't'; p2-6: set to coder. So string "a t c o d e r" => "atcoder". Only candidate? i=0,1 invalid, i=2 only. So output atcoder.
Second sample: S'="??p??d??", T="abc". lenS=8, lenT=3. Test possible i from 0 to 5.
Check each i:
i=0: positions 0-2: S'[0]='?' ok, S'[1]='?' ok, S'[2]='p' vs 'c'? T[2]='c', p != c -> invalid.
i=1: positions 1-3: S'[1]='?' ok, S'[2]='p' vs 'b'? T[1]='b'? Actually T indices: 0:a,1:b,2:c. So p=2: 'p' vs 'b' -> invalid.
i=2: positions 2-4: S'[2]='p' vs 'a'? T[0]='a' -> invalid.
i=3: positions 3-5: S'[3]='?' ok, S'[4]='?' ok, S'[5]='d' vs 'c'? T[2]='c' -> invalid.
i=4: positions 4-6: S'[4]='?' ok, S'[5]='d' vs 'b'? T[1]='b' -> invalid.
i=5: positions 5-7: S'[5]='d' vs 'a'? T[0]='a' -> invalid. So no valid placements, UNRESTORABLE.
Matches.
But is this always correct? Consider case where there are multiple placements, we choose lex smallest candidate. Could there be a candidate that is not constructed as described? For a given placement i, we set all other '?' to 'a'. That yields the lexicographically smallest string given that T is placed at i, because we minimize all other positions. Indeed any other fill would be lex larger if any of those '?' is set to a letter > 'a'. So for each placement, the smallest possible S is exactly that. Then among all placements, we take the global min.
But we must also consider that S might contain T as substring at multiple positions (e.g., overlapping) but that's fine; the candidate from each placement assumes T is exactly placed at i, but the resulting string might also contain T elsewhere; that's okay because condition 1 only requires it contains T as substring (at least one occurrence). So the candidate S from placement i satisfies condition, and is minimal for that placement.
Now, is there any possibility that the lexicographically smallest overall string might not correspond to setting all non-substring '?' to 'a'? Suppose we choose a placement i, but to get even smaller lexicographically overall, maybe we could set some '?' inside the substring to a smaller letter? But we cannot because they must equal T for the substring to be T. Unless the T appears somewhere else? Wait, the condition is: S contains T as a contiguous substring. It does not require that the placement we choose is the only one or that we enforce T at a specific location. So we could potentially have a string where T appears at a different location than a given i, and the fill of '?' could be different. But in our enumeration, we consider all possible placements. However, maybe the globally smallest S could have T appearing, but the way we assign letters could be such that the T appears not exactly at the positions where we forced it, but we might have filled some '?' in a different way that still yields T somewhere else while making the overall string smaller. But our enumeration for each possible placement i yields the smallest S among those that have T at that exact placement i (by setting other '?' to 'a'). If the globally smallest S has T at some placement, that placement i is considered, and we will generate that S (or maybe an even smaller S) if we set other '?' to 'a'. However, note: If the globally smallest S has T at placement i, then for positions outside the T substring, to be smallest, it must have all those positions as small as possible, i.e., 'a' for any '?'. So indeed the globally smallest S for that placement must have that property. So our construction for each placement yields the minimal string for that placement. So the global min among those is the global min overall.
But could there be a scenario where the minimal string has T appearing at multiple places, and the minimal for a particular placement i might not be minimal overall because we could get an even smaller string by having T appear at a different placement but not necessarily exactly at i? But we consider all placements, so we would consider the placement that yields the smallest string. So it's fine.
But one nuance: The string S must be derived from S' by replacing '?' with lowercase letters. That is exactly our construction: we replace '?' either with T letters or with 'a' (if outside). So yes.
Thus algorithm is straightforward.
But let's double-check some edge cases.
Case: S' contains fixed letters that are part of T and also other letters. Good.
Case: T length > S' length -> obviously impossible because cannot contain substring longer than S. So output UNRESTORABLE.
Case: There are multiple valid placements. Lexicographically smallest candidate will be chosen.
Example: S' = "a?c", T = "ab". lenS=3, lenT=2. Placements: i=0: check S'[0]='a' vs T[0]='a' ok; S'[1]='?' vs 'b' ok. So candidate: set p0: a; p1: b; p2: outside substring? i=0 covers positions 0,1. p2 outside: S'[2]='c' fixed -> c. So string "abc". i=1: positions 1-2: check S'[1]='?' vs 'a'? T[0]='a' ok; S'[2]='c' vs 'b'? c!=b -> invalid. So only candidate "abc". Output "abc". But is there any string smaller? Could we set '?' to 'a' to get "aac"? That would not contain "ab" as substring because substring "aa". So no.
Another case: S' = "?b?d", T = "bc". lenS=4, lenT=2. Placements: i=0: positions 0-1: S'[0]='?' vs 'b' ok; S'[1]='b' vs 'c'? b!=c invalid. i=1: positions 1-2: S'[1]='b' vs 'b' ok; S'[2]='?' vs 'c' ok. So candidate: p0 outside: S'[0]='?' -> 'a'; p1-2: b,c; p3 outside: S'[3]='d' -> d. So string "abcd". i=2: positions 2-3: S'[2]='?' vs 'b'? T[0]='b' ok; S'[3]='d' vs 'c' invalid. So only "abcd". Output "abcd". But maybe we could get "abac"? Not contain bc.
Multiple placements: S' = "??", T = "a". lenS=2, lenT=1. Placements: i=0: candidate: p0 = 'a' (since T[0] = a), p1 outside: '?' -> 'a' => "aa". i=1: candidate: p0 outside: '?' -> 'a', p1 = 'a' => "aa". So both yield "aa". Lex min "aa". But is there a smaller string? "aa" is smallest possible? Could we get "aa"? Yes. But could we get "a?"? Actually we must replace all '?', so we must produce a string of length 2. Could we produce "aa"? That's the lex smallest possible (since both letters are a). But maybe "aa" vs "aa" same.
Now consider S' = "?a", T = "b". lenS=2, lenT=1. Placements: i=0: check S'[0]='?' ok -> candidate: p0='b', p1='a' => "ba". i=1: check S'[1]='a' vs 'b'? invalid. So only "ba". But could we produce "aa"? That doesn't contain "b". So no.
Now consider S' = "????", T = "ab". Placements many. Let's compute candidates:
i=0: string: p0='a', p1='b', p2='a', p3='a' => "abaa".
i=1: p0='a', p1='a', p2='b', p3='a' => "aaba".
i=2: p0='a', p1='a', p2='a', p3='b' => "aaab".
Lexicographically, compare "aaba", "aaab", "abaa". Which is smallest? Compare first characters: all 'a'. Second: i=0 second is 'b', i=1 second 'a', i=2 second 'a'. So i=0 out. Between i=1 "aaba" and i=2 "aaab": compare char2: both 'a', char3: i=1 has 'b', i=2 has 'a'. So "aaab" is smaller than "aaba". So min is "aaab". That corresponds to placing T at the rightmost possible? Actually i=2 (the last placement) yields "aaab". Indeed that is lexicographically smallest because earlier placements have more leading 'a's? Actually i=2 yields first two positions as 'a' (from '?') and then 'a','b' at positions 2-3? Wait for i=2, positions 2 and 3 are set to 'a','b'? T = "ab", so positions 2='a', 3='b'. Then positions 0 and 1 are '?' set to 'a'. So string "a a a b" => "aaab". For i=1: positions 1='a',2='b', positions 0,3 set to 'a' -> "a a b a" => "aaba". For i=0: "a b a a" => "abaa". So lex smallest is indeed with T placed as far right as possible? Not always; but we just take min.
Now test: S' = "a??", T = "ab". Placements: i=0: check S'[0]='a' vs 'a' ok; S'[1]='?' vs 'b' ok; => candidate: p0='a', p1='b', p2 outside: '?'->'a' => "aba". i=1: positions 1-2: check S'[1]='?' vs 'a' ok; S'[2]='?' vs 'b' ok; => candidate: p0 fixed 'a'; p1='a'; p2='b' => "aab". Compare "aba" vs "aab". Lexicographically "aab" < "aba" (since second char 'a' vs 'b'). So output "aab". That makes sense.
Edge: Could there be a case where the candidate from some placement is not actually the lexicographically smallest overall because we could set some '?' inside the substring to something other than T but still have T appear elsewhere? For example, suppose we place T at i, but if we instead set some '?' inside the region we used for T to a smaller letter, we might get a string that still contains T because T appears in a different location. That could yield a smaller string than any candidate we constructed from a placement i (since for each placement we forced that the specific region equals T). But if we choose not to force T at that region, we could set those positions to 'a' to get smaller, but then we must ensure T appears somewhere else. So perhaps the globally smallest S could have T appear at a location that is not exactly the location we considered when we forced that region to be T? Wait, we considered all possible placements. But in constructing a candidate for placement i, we set the region i..i+|T|-1 exactly to T. However, could there be a string S that is smaller than all such candidates, which still contains T, but for which the region we would think of as the "first occurrence" or "some occurrence" is not exactly matching our forced region? That's impossible because if S contains T, there exists at least one index i such that S[i..i+|T|-1] = T. So S must match T at some region. Let that region be i. Then S, at positions i..i+|T|-1, must equal T. For all other positions, to be lexicographically smallest, we want them as small as possible, i.e., for any '?' outside the region, we set to 'a'. But what about positions inside the region that originally were '?'? They must be set to the required letters. However, there is a nuance: What if there is an overlap such that T appears due to some combination of letters that are not forced to be exactly T at a specific region because we could have multiple possible regions? Wait, if S contains T, there is a specific contiguous block equal to T. So for that block, the letters are fixed. So in that block, any '?' must become the corresponding T letters. So S must satisfy that for that block i. Thus S is exactly the candidate we would generate for that block i, except possibly for positions outside the block: we could set some '?' to letters other than 'a' but that would only make the string larger lexicographically if we set them to something > 'a'. To be smallest, we would set them to 'a'. So the smallest possible S that has T at block i is exactly our candidate for i. Therefore any S that contains T can be no smaller than the minimum over i of those candidates. So the global smallest is indeed the min over candidates.
But what if there is an S that contains T at two different blocks, and the settings for one block conflict with the settings for another? However, the S must be a single string. It could satisfy both blocks simultaneously, e.g., overlapping T occurrences. But then S still contains T. For that S, choose one block, say the leftmost occurrence, and then outside that block we could have set '?' to 'a' to be smaller, but maybe the presence of the other occurrence forces some '?' outside the leftmost block to be something else? Actually if there are two occurrences, the string must satisfy both. That imposes constraints. Our candidate for the leftmost occurrence i1 sets positions i1..i1+|T|-1 to T and all other '?' to 'a'. This candidate may not satisfy the second occurrence if the second occurrence overlaps in some way that requires some letters outside the leftmost block to be something else. However, we are not requiring that the string contain only one occurrence; it's allowed to contain more. But our candidate built from i1 might not have T at i2, but that's fine because condition only requires at least one occurrence. The candidate for i1 is valid because it contains T at i1. So it's a valid string. However, could there be a valid string that is smaller than the candidate for i1, but also contains T at i2? Possibly, but if it contains T at i2, then the candidate for i2 is defined, and if that candidate is smaller than the candidate for i1, then it would be considered. If the actual smallest string has T both at i1 and i2, it might not be exactly equal to either candidate? Let's think: Suppose we have two occurrences, i1 and i2 with i1 < i2. Then S must satisfy T at both intervals. That imposes constraints on positions in the overlap region if they overlap, or on both sets. The candidate for i1 only forces T at i1 and sets all other '?' to 'a'. It may not satisfy T at i2, but that's okay; it still satisfies condition. However, could the existence of both constraints allow a string that is lexicographically smaller than the candidate for i1? Possibly, if by satisfying both constraints we are forced to set some '?' to a letter larger than 'a' in some positions, that would increase lex order, not decrease. But maybe it allows us to set some positions to 'a' that would otherwise be forced to something else in the candidate for i1? No, because candidate for i1 already sets all non-T positions to 'a'. So candidate for i1 is the absolute minimum for any string that has T at i1, irrespective of other constraints. So any string that also has T at i1 cannot be smaller than that candidate. Therefore the candidate for i1 is the lower bound for strings with T at i1. So the global minimum among all valid strings is min over i of (lower bound for strings with T at i). And we can achieve each lower bound via our candidate (by setting other '?' to 'a'), which yields a valid string (since it has T at i). So the global minimum is exactly the minimum over our candidates. So it's correct.
Thus algorithm is fine.
Now, we must be careful with lexicographic order of strings. In Python, min(list) works lexicographically for strings as they are sequences of characters, using Unicode order, which for lowercase letters is correct.
Time complexity: O(|S'| * |T| + number_of_candidates * |S'|) which is fine for n <= 50.
Now consider edge case where T length > S' length: no placements, output UNRESTORABLE.
Now, potential pitfall: Could there be a scenario where the lexicographically smallest candidate is not unique? We just output the smallest; it's fine.
Now, sample: Input shows S and T' but description says S' and T. Actually first line is S' (the pattern) and second line is T. But they labeled as S and T'? In statement: "He also found a string S', which turns out to be the string S with some of its letters replaced with ?." So S' is the pattern. Then input: S' (maybe they call it S?) and T. So we read two strings: first is pattern (contains ?), second is T.
Thus code: read two lines: s_prime = input().strip(); t = input().strip().
Now implement.
Edge Cases to test:
-
S' = "a", T = "a". Then placement i=0 valid. Candidate: p0 = 'a' (since T). So output "a".
-
S' = "?", T = "a". i=0 valid -> candidate: 'a'. Output "a".
-
S' = "?b", T = "ab". i=0: check S'[0]='?' ok, S'[1]='b' vs 'b'? T[1]='b' okay -> candidate: p0='a', p1='b' -> "ab". i=1: not possible (len). So output "ab".
-
S' = "?a", T = "ab". i=0: check S'[0]='?' ok, S'[1]='a' vs 'b'? invalid. So no placements -> UNRESTORABLE.
-
S' = "?????a", T = "abc". Let's see: There is a fixed 'a' at the end. Could T be placed overlapping that a? Possibly. Our algorithm will check placements. Should output appropriate.
Now, ensure that when constructing candidate, we treat positions within T placement exactly as T letters. But what about positions inside the placement that originally were fixed letters? They must equal T; we already checked that. So we can just set them to T[j] (overwrites anyway). Good.
Now, possible nuance: When we set non-placement '?' to 'a', what about positions that are inside placement but are '?'? They are set to T. So fine.
Now, check overlapping placements: All good.
Now, one more nuance: Could it be that the lexicographically smallest string is not from setting all non-placement '?' to 'a'? Suppose there is a '?' at a position before the T placement. Setting it to 'a' is minimal. So yes.
What about positions after T placement: also 'a'.
Thus algorithm stands.
Now, is there any possibility to get a smaller string by not forcing T at a specific region but still having T appear because we set some '?' to something else? That would mean the T substring occurs somewhere else that we didn't consider; but we consider all possible start positions. So we cover that.
Thus solution is simple.
Now, let's implement in Python.
Pseudo:
def solve():
s_prime = input().strip()
t = input().strip()
n = len(s_prime)
m = len(t)
candidates = []
for i in range(n - m + 1):
ok = True
for j in range(m):
if s_prime[i+j] != '?' and s_prime[i+j] != t[j]:
ok = False
break
if not ok:
continue
# build candidate
s_list = list(s_prime)
for p in range(n):
if i <= p < i+m:
s_list[p] = t[p-i]
elif s_list[p] == '?':
s_list[p] = 'a'
candidate = ''.join(s_list)
candidates.append(candidate)
if not candidates:
print("UNRESTORABLE")
else:
print(min(candidates))
But careful: In building candidate, we used s_list initially = list(s_prime). Then we modify: for positions in T placement we set to t[p-i]; for others if original was '?' we set to 'a'. That's fine. But what if there is a '?' inside T placement? We set it to T, that's correct. If original is a letter and matches T (as checked), setting again to T doesn't change. So fine.
However, we could also build directly as described.
Edge: if we set s_list = list(s_prime) and then for p outside we check if s_list[p]=='?' then set to 'a'. But we must be careful: after setting T placement, s_list[p] for those p is overwritten, which is fine. So it's okay.
Alternatively, we can construct from scratch.
Now test with provided examples.
Let's run mental:
Input1: s_prime="?tc????", t="coder". i=2 valid. Build: list = ['?','t','c','?','?','?','?'].
For p in range(7):
p=0 not in [2,6]? Actually placement from 2 to 6 inclusive? i=2, m=5 => positions 2,3,4,5,6. So p=0 outside: s_list[0]='?' -> set to 'a'.
p=1 outside: s_list[1]='t' not '?', leave as 't'.
p=2 in placement: set to t[0]='c' -> 'c'.
p=3: t[1]='o' -> 'o'.
p=4: t[2]='d' -> 'd'.
p=5: t[3]='e' -> 'e'.
p=6: t[4]='r' -> 'r'.
Result "atcoder". Good.
Now candidate list only one, min is atcoder.
Second: no placements, print UNRESTORABLE.
Now test with multiple placements as earlier: s_prime="????", t="ab". n=4,m=2. i=0: valid; build: list initially ['?','?','?','?']; placement 0-1: set p0='a',p1='b'; p2 outside: '?'->'a'; p3:'?'->'a' -> "abaa". i=1: placement 1-2: p0 outside: '?'->'a'; p1='a'; p2='b'; p3:'?'->'a' -> "aaba". i=2: placement 2-3: p0:'a'; p1:'a'; p2:'a'; p3:'b' -> "aaab". min = "aaab". Works.
Now consider scenario where S' has fixed letters that are already 'a's or other. That's fine.
Now edge: T empty? Not possible because constraints |T|>=1.
Now, what about if S' has no '?' and already contains T? Then we can output S' as is? But we need lexicographically smallest among strings that satisfy condition. Since S' is fixed (no '?'), there's only one possible S: S'. If it contains T, output S'. Our algorithm: For each valid placement i where S' matches T? Actually we must check if S' contains T as substring. But we only generate candidates from placements where S'[i+j] is either '?' or equals T[j]. If S' contains T, then there exists some i where S'[i+j]==T[j] for all j. That placement will be valid because letters match. Then we build candidate: for positions inside placement, we set to T (which matches S'), for outside, if '?' we set to 'a', but since there are no '?', S' remains unchanged. So candidate equals S'. So candidates will include S'. Also if there are other placements (e.g., overlapping but also valid if S' letters match T? Only if they match). So the min among candidates could be S' if it's smallest. Could there be a candidate that is lexicographically smaller than S'? For other placements, we may set some '?' to 'a' but if there are no '?', then other placements would also produce S' because no flexibility. Actually if S' has no '?' and contains T, the only possible strings are S' itself. So answer S'. Our algorithm: candidates from valid placements will all be S' because no '?' to change. So min is S'. Good.
But what if S' contains T but also has some '?'? Then there might be a candidate that is smaller than S' by setting some '?' to 'a'? But careful: S' is the pattern, not the final string; S' contains '?'. So the actual S we output must replace '?'. So we can produce a string that differs from S' pattern. Our algorithm will produce candidate strings that replace '?'. So S' itself is not a valid final string (since it has '?') unless we treat '?' as a letter, but they must be replaced. So it's fine.
Now consider scenario where there is a valid placement that yields a candidate, but that candidate might have a '?' somewhere? No, we replaced all '?'.
Now, consider scenario where there is a placement but after constructing candidate, the resulting string does not actually contain T? But we forced the region i to be T, so it does contain T. So valid.
Now, consider scenario where S' has fixed letters that conflict with T in all placements, so no candidate.
Now, edge: If there is a placement that is valid but then we set all other '?' to 'a', but what about positions inside the placement that are '?'? They become T letters, which may be > 'a', but required. That's fine.
Now, it's correct.
Now, but I recall a known AtCoder problem: ABC 074 C? Actually there is a problem "ABC 103 B - String Rotation"? No. This is from ABC 072? Not sure. Actually it's AtCoder Beginner Contest 068? Wait there is a problem "ABC 068 B - Break Number"? Not. I think it's from AtCoder ABC 049 C? Actually there is a problem "ABC 049 C - Daydream" about reconstructing string. But this problem is from AtCoder ABC 100? Let me recall: This problem is "ABC 100 C - *"? No. The problem statement looks like from AtCoder ABC 103 C? Not.
Maybe it's from AtCoder ABC 064 C? Wait I recall a problem: "Restoring" something. Actually it's from AtCoder ABC 059 B - Comparison? Not.
Search memory: There is a known AtCoder problem "ABC 049 C - 白昼夢" but not. However, there is a problem "ABC 106 B - ..." Not.
I think it's AtCoder ABC 104 C - All Green? No.
The problem is "AtCoder ABC 108 B - Something"? Not.
Anyway.
Now, before finalizing, let's test a tricky case: S' = "?a?", T = "ab". n=3,m=2. Placements: i=0: check S'[0]='?' ok; S'[1]='a' vs 'b'? 'a' != 'b' -> invalid. i=1: check S'[1]='a' vs 'a' ok; S'[2]='?' vs 'b' ok. So candidate: p0 outside: '?'->'a'; p1='a'; p2='b' -> "aab". So output "aab". Now, is there a way to get "aaa"? That would not contain "ab". So no.
Now, another tricky: S' = "?b?c", T = "abc". n=4,m=3. Placements: i=0: check S'[0]='?' vs 'a' ok; S'[1]='b' vs 'b' ok; S'[2]='?' vs 'c'? T[2]='c'? Actually T="abc": indices 0:a,1:b,2:c. So S'[2]='?' ok. -> valid. Candidate: p0='a'; p1='b'; p2='c'; p3 outside: fixed 'c'? Wait S'[3]='c'? Actually S' = "?b?c": indices: 0:'?',1:'b',2:'?',3:'c'. So p3 is outside placement (since placement covers 0-2). p3 is fixed 'c' -> 'c'. So candidate = "abcc". i=1: placement 1-3: check S'[1]='b' vs 'a'? T[0]='a' -> invalid. So only candidate "abcc". But could we get a smaller string like "abac"? That would require p3='a', but p3 is fixed 'c', can't change. So "abcc" is only. Works.
Now, what about case with overlapping placements that yield same candidate? That's fine.
Now, verify with edge: S' length 50, T length 50, exact. Then only placement i=0. Must check all characters.
Now, consider lexicographic order: We want smallest. So min works.
Now, potential bug: Our construction sets all '?' outside placement to 'a'. But what about '?' inside placement? We set to T. That is forced. So good.
Now, are there any constraints that might lead to candidate strings that are not lexicographically comparable? No.
Now, answer will be printed as string.
Now, note: The problem statement in English sometimes uses "S" and "T'"? Actually it says "S'", but then input format shows S and T? Wait: The problem statement says:
Input
Input is given from Standard Input in the following format:
S
T'
But in the examples, they show:
?tc????
coder
First line is S'? Actually they labeled first line as S? In description they said: "He also found a string S', which turns out to be the string S with some of its letters replaced with ?. One more thing he found is a sheet of paper with the following facts ... Print the string S." So the input is S' (the pattern) and T? They wrote T'? But sample shows second line "coder", which is T. So likely input: first line S' (string with ?) and second line T (the substring). However, the input format shows S and T' maybe a mistake. We'll read two strings: first = pattern, second = T.
Now, implement solve() accordingly.
Now, consider scenario where there are multiple candidates but some may be invalid because after construction the string may not actually contain T? But we forced it, so it does.
Now, think about the scenario: The candidate constructed from placement i may also inadvertently create other occurrences of T that could affect lexicographic minimality? Not relevant.
Now, let's write code.
Time complexity: O(NMN) worst-case but N and M ≤ 50, so trivial.
Now, let's think about any corner case where the candidate from placement i might be lexicographically larger than needed because we set some '?' to 'a' that cause conflict? No conflict.
Thus solution is correct.
But wait, is there any possibility that the lexicographically smallest string that contains T does NOT correspond to forcing T at some location and setting other '?' to 'a'? Suppose we have a placement i, but we could set some '?' that are before the T region to letters smaller than 'a'? No, 'a' is the smallest.
Alternatively, could we set some '?' to a letter less than the required T letters by shifting the occurrence of T to a later position? That is accounted by considering different i.
Thus done.
Now, final answer: We'll implement the described algorithm.
Now, check a case: S' = "?????a", T = "abc". n=6,m=3. Placements i=0..3.
i=0: Check positions: 0 '?' ok, 1 '?' ok, 2 '?' ok -> valid? Need also check? Actually we need to check positions 0-2: all '?' -> ok. Candidate: set 0='a',1='b',2='c', then p3,p4: '?'->'a', p5 fixed 'a' -> 'a'. So string "abcaaa"? Actually p0='a', p1='b', p2='c', p3='a', p4='a', p5='a' => "abcaaa".
i=1: Check positions 1-3: p1 '?' ok, p2 '?' ok, p3 '?' ok -> valid. Candidate: p0 '?'->'a'; p1='a'; p2='b'; p3='c'; p4 '?'->'a'; p5='a' => "aabc aa"? Actually p0='a', p1='a', p2='b', p3='c', p4='a', p5='a' => "aabcaa".
i=2: Check positions 2-4: p2 '?' ok, p3 '?' ok, p4 '?' ok -> valid. Candidate: p0='a', p1='a', p2='a', p3='b', p4='c', p5='a' => "aaabca".
i=3: Check positions 3-5: p3 '?' ok, p4 '?' ok, p5='a' vs T[2]='c'? 'a' != 'c' -> invalid. So candidates: "abcaaa", "aabcaa", "aaabca". Lex smallest: compare: first char all 'a'. Second char: "abcaaa" has 'b', "aabcaa" has 'a', "aaabca" has 'a'. So "abcaaa" eliminated. Compare "aabcaa" vs "aaabca": second char both 'a'; third: "aabcaa" third='b', "aaabca" third='a' -> "aaabca" smaller. So answer "aaabca". This is the rightmost placement that works. So indeed often the lex smallest is the placement as far right as possible because we can set leading '?' to 'a's. But not always if there are fixed letters. Suppose fixed letters are large, maybe earlier placement yields more leading 'a's? Let's see: S' = "z??", T = "aa". n=3,m=2. Placements: i=0: check S'[0]='z' vs 'a' -> invalid. i=1: check S'[1]='?' ok, S'[2]='?' ok -> valid. Candidate: p0 fixed 'z'; p1='a'; p2='a' => "zaa". Only candidate. So it's "zaa". Lex smallest? Could there be a candidate with T placed at i=0? No. So it's fine.
Now, consider case where multiple placements, and fixed letters affect lex order. For example: S' = "a?c?", T = "ab". n=4,m=2.
i=0: check S'[0]='a' ok; S'[1]='?' ok -> valid. Candidate: p0='a', p1='b', p2 fixed 'c', p3 '?' -> 'a' => "abca".
i=1: check S'[1]='?' vs 'a' ok; S'[2]='c' vs 'b'? 'c' != 'b' invalid.
i=2: check S'[2]='c' vs 'a' invalid. So only "abca". Output "abca". Could there be a string like "aaba"? Let's see: "aaba" would have '?' at pos1 set to 'a', pos3 set to 'a', but then T "ab" would appear at positions 0-1? "aa" not "ab". At positions 1-2: "ab"? pos1='a', pos2='c'? Not "ab". So no.
Now, consider scenario where there are overlapping placements that produce same candidate? Not issue.
Thus the solution is correct.
Now, what about input where S' has length less than T? Then loop range(n - m + 1) yields empty range because n-m+1 <= 0. So candidates empty, print UNRESTORABLE. Good.
Now, implement.
Potential pitfalls: reading input may have trailing newline. Use input().strip().
Now, let's write final code.
We'll define solve() and call it if name == "main":.
Now, I'd like to test some additional cases:
-
S' = "??????", T = "a". Then placements all indices 0..5 produce candidate strings where each candidate has 'a' at that index and all other positions 'a'. Actually for i=0: set p0='a', others 'a' => "aaaaaa". Similarly i=1 yields "aaaaaa"? Let's compute: i=1: p0='a', p1='a', p2.. all 'a'? Actually if we set all '?' to 'a', all strings become "aaaaaa" regardless of i because T is "a", and we set all '?' to 'a'. So candidates all same "aaaaaa". So output "aaaaaa". That is lexicographically smallest indeed.
-
S' = "?b?d?", T = "abc". n=5,m=3. i=0: check: p0='?' ok; p1='b' vs 'b' ok; p2='?' vs 'c'? T[2]='c'? Actually T = "abc": indices: 0:a,1:b,2:c. So p2='?' ok -> valid. Candidate: set p0='a', p1='b', p2='c', p3 fixed 'd', p4 '?' -> 'a' => "abcd a"? Actually p4='a' -> "abcda". i=1: check p1='b' vs 'a'? T[0]='a' -> invalid. i=2: check p2='?' vs 'a' ok; p3='d' vs 'b'? invalid. So only "abcda". Output "abcda". Is there any possibility to get "abada"? That wouldn't contain "abc". So fine.
-
S' = "a?c?b", T = "abc". n=5,m=3. i=0: check p0='a' ok; p1='?' ok; p2='c' vs 'c'? Actually T[2]='c'? Wait T="abc": a,b,c. So p2='c' matches c; valid. Candidate: set p0='a', p1='b', p2='c', p3 '?' -> 'a', p4 fixed 'b' -> 'b' => "abcab". i=1: check p1='?' vs 'a' ok; p2='c' vs 'b'? invalid. i=2: check p2='c' vs 'a' invalid. So only "abcab". That's output.
Now, what about scenario where S' has a fixed letter that is not '?' and is part of T but also we could use another placement that yields smaller string because it allows more leading 'a's? For example, S' = "x?y?z", T = "ab". n=5,m=2. Let's examine placements. Fixed letters: x, y, z. T="ab". i=0: check p0='x' vs 'a' -> invalid. i=1: check p1='?' ok; p2='y' vs 'b'? 'y' != 'b' -> invalid. i=2: check p2='y' vs 'a' -> invalid. i=3: check p3='?' ok; p4='z' vs 'b'? 'z' != 'b' -> invalid. So none, UNRESTORABLE. That's correct because fixed letters block.
Now, consider scenario where there is a valid placement but setting all other '?' to 'a' may create conflict with the condition that S must contain T? No conflict; it's fine.
Now, is there any scenario where the candidate from placement i yields a string that does NOT contain T because we inadvertently changed a letter that was part of a fixed T elsewhere? But we forced T at i, so it's there. It might also inadvertently remove T from some other location if we changed letters, but that's irrelevant; we only need at least one.
Thus algorithm stands.
Now, let's think about the possibility of a smaller string that does not place T at any exact location because we could place T partly overlapping with fixed letters that are not contiguous? No, substring is contiguous, so it must be exactly a block.
Thus solution is final.
Now, let's also consider the nuance: In lexicographic order, we compare strings from leftmost character. So we want to minimize the first differing character. So placing T as far right as possible often yields more leading 'a's, which is good. But sometimes if there are fixed letters before the placement that are not '?' and are > 'a', then moving the placement earlier might yield a smaller fixed letter earlier? Actually if we have a fixed letter early, it's fixed and cannot be changed. So for lex order, the earliest character is crucial. If the first character is fixed to, say, 'b', then any candidate will start with 'b'. But if there is a placement that allows the first character to be 'a' (by setting a '?' to 'a'), that could be better than placements that start with fixed 'b'. But our enumeration includes all placements, so we'll get candidates with first char 'a' if possible.
For example: S' = "?b", T = "ab". n=2,m=2. i=0: check p0='?' ok; p1='b' vs 'b' ok -> candidate: p0='a', p1='b' => "ab". i=1: none. So output "ab". First char 'a'.
If S' = "b?b", T = "ab". n=3,m=2. Placements: i=0: check p0='b' vs 'a' invalid. i=1: check p1='?' ok; p2='b' vs 'b' ok -> candidate: p0 fixed 'b', p1='a', p2='b' => "bab". So first char 'b'. That's the only candidate. So "bab". Could we get a string starting with 'a'? No, because first char is fixed 'b'.
So algorithm works.
Now, potential issue: The candidate from placement i might set some '?' to 'a', but those '?' might be part of overlapping region with another potential T occurrence that we could have used to get a smaller string? But again, candidate from i is minimal given that T is at i. If T appears elsewhere as well, that doesn't make the string any smaller. So it's fine.
Now, code.
But wait: The problem statement says: "Print the string S. If such a string does not exist, print UNRESTORABLE." They mention "S is the lexicographically smallest string among the ones that satisfy Condition 1." So we must output that.
Now, are there any hidden tricks? Possibly the '?' can be replaced with any lowercase letters, but we considered only 'a' for non-substring. However, what if we set some '?' to a letter smaller than required for T? That would break the T occurrence at i. But maybe we could get a string that contains T but not forced at i, and have some '?' set to a letter smaller than T at positions that would have been part of T in placement i, but because we moved T elsewhere, those positions can be smaller. That is exactly covered by considering placements other than i. So we need to consider all placements.
Thus correct.
Now, confirm with a more complex example: S' = "?a?b?", T = "ab". Let's compute placements:
S' length 5, T length 2.
Indices: 0:'?',1:'a',2:'?',3:'b',4:'?'.
Placement i=0: check p0='?' ok; p1='a' vs 'b'? No -> invalid.
i=1: check p1='a' vs 'a' ok; p2='?' vs 'b' ok -> valid.
i=2: check p2='?' vs 'a' ok; p3='b' vs 'b' ok -> valid.
i=3: check p3='b' vs 'a' invalid.
Candidates:
i=1: set p0='?'->'a'; p1='a'; p2='b'; p3='b' fixed; p4='?'->'a' => "aabba"? Wait: p0='a', p1='a', p2='b', p3='b', p4='a' => "aabba".
i=2: set p0='a', p1 fixed 'a'; p2='a'; p3='b'; p4='?'->'a' => "aaaba"? Actually: p0='a', p1='a' (fixed), p2='a', p3='b', p4='a' => "aaaba". Compare "aabba" and "aaaba". Lexicographically: compare first char both 'a'. second: "aabba" second 'a', "aaaba" second 'a'. third: "aabba" third 'b', "aaaba" third 'a' => "aaaba" smaller. So output "aaaba". That's placement i=2. That's consistent with the intuition that the later placement yields more leading 'a's? Not exactly: i=2 gives first two positions 'a' and 'a', while i=1 gives first two 'a','a' as well? Actually i=1 also gives first two 'a','a'? Wait i=1 candidate: p0='a', p1='a' -> also 'a','a'. So the third character different: i=1 third is 'b', i=2 third is 'a'. So i=2 yields smaller third char. So later placement gave more 'a's at early positions because the third position becomes 'a' (since it's outside the T region? Actually for i=2, p2 is inside T (since i=2, p2 is index 2, which is start of T? Wait T length 2, i=2 covers indices 2 and 3. So p2 is part of T, must be 'a' (since T[0]='a')? Actually T="ab": T[0]='a', T[1]='b'. So for i=2, p2 = T[0] = 'a', and p3 = T[1] = 'b'. So p2 is forced to 'a', not 'b'. So p2 is 'a', which is smaller than the 'b' from i=1 where p2 was forced to 'b' (since T[1] at i=1 gave p2='b'). So indeed placement later can allow a position that was forced to be 'b' in earlier placements to become 'a' because it's outside the T region? Actually in i=2, p2 is inside T and is 'a', which is small; in i=1, p2 is inside T and is 'b', which is larger. So later placement can change the character at that position to a smaller one because T's first letter 'a' is smaller than second letter 'b'. So the lex min candidate is not simply the rightmost placement; it depends on the characters of T. So we must compute all.
Thus algorithm that enumerates all placements is correct.
Now, one more nuance: Could there be a candidate that is not from a single placement? That is, we could have a string that contains T but the placement we think of might not be the one we considered because we forced the substring exactly at that placement, but what if the string contains T at multiple positions and the minimal representation could be thought of as having T at one of them; but our candidate for that placement is the minimal for that placement. So it's included.
Thus we are fine.
Now, confirm that we set all '?' to 'a' outside placement. But is it possible that setting a '?' to 'a' could create an unintended occurrence of T that might cause the string to not be lex smallest? No, it creates an occurrence of T only if T is all 'a's? But T is arbitrary; if T is all 'a's, then setting other '?' to 'a' might create many occurrences, but that doesn't affect the validity or the lexicographic order; it's still a valid string. And the candidate we generate is the minimal for that placement; if T is all 'a', then maybe we could have a different placement with even more 'a's? But we consider all placements.
Now, consider a scenario where we have two placements i and j, and the candidate from i might actually be identical to candidate from j. That's okay.
Now, implement.
Now, note: The input might have extra spaces? Usually not.
Now, final code.
But let's be careful: In Python, string comparisons like min() work as expected.
Now, perhaps we can do more efficient: Instead of storing all candidates, we can keep current best and compare as we generate. But it's fine.
Now, let's write.
We'll define:
def solve():
s = input().strip()
t = input().strip()
n = len(s)
m = len(t)
best = None
for i in range(n - m + 1):
ok = True
for j in range(m):
if s[i+j] != '?' and s[i+j] != t[j]:
ok = False
break
if not ok:
continue
# construct candidate
candidate_list = list(s)
for p in range(n):
if i <= p < i+m:
candidate_list[p] = t[p-i]
elif candidate_list[p] == '?':
candidate_list[p] = 'a'
candidate = ''.join(candidate_list)
if best is None or candidate < best:
best = candidate
if best is None:
print("UNRESTORABLE")
else:
print(best)
Now, test with examples.
Example 1: s="?tc????", t="coder". i=2 valid. candidate = "atcoder". best = "atcoder". Output.
Example 2: s="??p??d??", t="abc". no valid, best None, print UNRESTORABLE.
Now, test with multiple placements as earlier: s="????", t="ab". Loop i=0..2.
i=0: candidate "abaa". best = "abaa".
i=1: candidate "aaba", compare with best "abaa": "aaba" < "abaa"? Let's compare: 'a' vs 'a' equal; second: 'a' vs 'b' -> 'a' < 'b', so best becomes "aaba".
i=2: candidate "aaab", compare with best "aaba": compare: first both 'a'; second both 'a'; third: 'a' vs 'b' -> 'a' < 'b', so best becomes "aaab". So final "aaab". Good.
Now, test with earlier example: s="?a?b?", t="ab". i=1 candidate "aabba"? Let's compute correctly: s = "?a?b?"; indices 0:'?',1:'a',2:'?',3:'b',4:'?'. For i=1: T[0]='a' goes to p1, matches 'a'; T[1]='b' goes to p2, so p2 set to 'b'. Then others: p0='?' -> 'a'; p3 fixed 'b'; p4='?' -> 'a'. So string: p0='a',p1='a',p2='b',p3='b',p4='a' -> "aabba". i=2: T[0]='a' to p2, so p2='a'; T[1]='b' to p3, matches 'b'. Others: p0='?' -> 'a'; p1='a' fixed; p4='?' -> 'a'. So "aaaba". Compare "aabba" vs "aaaba": third char: 'b' vs 'a', so "aaaba" smaller. Best final "aaaba". Good.
Now, think about scenario where there is a valid placement but candidate string contains '?'? We replaced all '?', so no.
Now, done.
Potential oversight: The candidate string must be derived from S' by replacing '?' with lowercase letters. In our construction, we set all '?' (including those inside the placement) to some letter. So it's fine.
Now, maybe there is a nuance: The problem asks to print the string S, not the candidate. Our candidate is S. So fine.
Now, I think that's it.
But wait, there is a known AtCoder problem "ABC 049 C - Daydream" where you reconstruct string from end. However, this one is "ABC 068 B"? Not. Actually I've seen this problem: It's AtCoder Beginner Contest 105 B? Not. It's from AtCoder ABC 064 C? Let's recall: The problem "Restoring" appears in AtCoder ABC 103? Actually there is AtCoder ABC 105 C - Base -2? Not.
Anyway, it's fine.
Double-check constraints: 1 ≤ |S'|, |T| ≤ 50. So both strings length at least 1. T consists of lowercase letters, S' consists of lowercase letters and '?'. So it's fine.
Now, implement solve() accordingly.
Now, I'll produce final answer.
But before final, ensure we read both lines correctly: They might include blank lines? Probably not.
Now, I'll output the answer.
One more nuance: In lexicographic order, we compare characters; Python uses Unicode code points, where 'a' < 'b' < ... < 'z'. So correct.
Now, I think we're done.
But let's consider possible tricky case: When there are multiple placements, but the candidate from placement i might be invalid because after setting all '?' to 'a', the string might not contain T due to some weird reason? No, because we forced T at i. So it's always present.
Edge: If S' contains uppercase? No, only lowercase.
Thus final solution.
Now, I'll write final code.
One more potential improvement: Instead of constructing candidate as list(s) and then replacing, we can build directly using comprehension, but the current is fine.
Time complexity: at most 50 placements, each constructing a string of length ≤ 50, trivial.
Now, answer.
But hold on: There is another nuance: The problem says: "S is the lexicographically smallest string among the ones that satisfy Condition 1." This means among all strings that satisfy condition 1 (i.e., contain T), we need the smallest lexicographically. But we considered only strings that are derived from S' by replacing '?'. However, are we allowed to replace '?' with any lowercase letter? Yes. So our set of possible S is exactly those. So the lexicographically smallest among those that also satisfy condition 1 is indeed the min over our candidates.
Now, is it possible that the lexicographically smallest string among those that satisfy condition 1 is not attained by setting all non-substring '?' to 'a'? Let's argue formally: For any valid string S (i.e., derived from S' and containing T), let i be some occurrence of T. Consider the string S' constructed by taking S and for every position p not in the occurrence i, if S'[p] is '?' we can possibly change it to a smaller letter, but S might already have some letters not 'a'. Could there be a valid S that is smaller than our candidate for i but does not have all non-occurrence '?' set to 'a'? Suppose there is a position p (outside occurrence) where S'[p] is '?' and S[p] is not 'a'. Then we could change that to 'a' and get a smaller string (since all previous characters same, at position p we get smaller). But would the new string still be valid? Possibly, if changing that character does not affect the occurrence of T (since it's outside), and does not break the match to fixed letters (since it's '?' originally, we can set to any letter). So we could get a smaller valid string. Therefore, any minimal valid string must have all such '?' set to 'a'. So our construction is necessary for minimality given that occurrence. So yes.
Now, what about positions inside the occurrence: they must be exactly T. So the minimal string given occurrence i is exactly our candidate. So the global minimal over all valid strings is min_i candidate_i.
Thus solution stands.
Now, code final.
But let's also think about one more nuance: If there are multiple possible occurrences in the same candidate (i.e., after building, the string might have T at some other index as well, but that doesn't matter). Could it be that for a given i, the candidate string does not have T at i because we changed some letter inadvertently? No, we set exactly T at i.
Now, we should be careful about the case where S' has a '?' at a position that is inside the T occurrence and we set it to T[j]. That's fine.
Now, final code.
I will now write the final solution. ```python
import sys
def solve() -> None:
s_prime = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
n = len(s_prime)
m = len(t)
best = None
for i in range(n - m + 1):
# check if T can be placed starting at i
ok = True
for j in range(m):
ch = s_prime[i + j]
if ch != '?' and ch != t[j]:
ok = False
break
if not ok:
continue
# construct the smallest string for this placement
candidate = list(s_prime)
for p in range(n):
if i <= p < i + m:
candidate[p] = t[p - i]
elif candidate[p] == '?':
candidate[p] = 'a'
cand_str = ''.join(candidate)
if best is None or cand_str < best:
best = cand_str
if best is None:
print("UNRESTORABLE")
else:
print(best)
if name == "main":
solve()