Problem:


Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.  

One more thing he found is a sheet of paper with the following facts written on it:  

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.  

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.  

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.



Sample input:
?tc????
coder


Sample output:
atcoder

Submission status: Wrong Answer

Solution code:
# coding=utf-8
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heappushpop, heapify
import copy
import sys
INF = float('inf')
mod = 10**9+7
sys.setrecursionlimit(10 ** 6)


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


def main():
    s = S()
    t = S()
    ans = ""
    flg = 1

    for i in range(len(s) - len(t), -1, -1):
        flg = 1
        clipped = s[i:i+len(t)]
        for j in range(len(t)):
            if clipped[j] != t[j] and clipped[j] != "?":
                flg = 0
                break
        if flg:
            ans = s[:i].replace("?", "a") + t + s[i + len(t):]
            break

    if not flg:
        print("UNRESTORABLE")
        return
    print(str(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding=utf-8
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heappushpop, heapify
import copy
import sys
INF = float('inf')
mod = 10**9+7
sys.setrecursionlimit(10 ** 6)


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


def main():
    s = S()
    t = S()
    ans = ""
    flg = 1

    for i in range(len(s) - len(t), -1, -1):
        clipped = s[i:i+len(t)]
        for j in range(len(t)):
            if clipped[j] != t[j] and clipped[j] != "?":
                flg = 0
                break
        if flg:
            ans = s[:i].replace("?", "a") + t + s[i + len(t):]
            break

    if not flg:
        print("UNRESTORABLE")
        return
    print(str(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding=utf-8
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heappushpop, heapify
import copy
import sys
INF = float('inf')
mod = 10**9+7
sys.setrecursionlimit(10 ** 6)


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


def main():
    s = S()
    t = S()
    ans = ""
    flg = 1

    for i in range(len(s) - len(t), -1, -1):
        clipped = s[i:i+len(t)]
        for j in range(len(t)):
            if clipped[j] != t[j] and clipped[j] != "?":
                flg = 0
                break
        if flg:
            ans = s[:i].replace("?", "a") + t + s[i+len(t):]

    if not flg:
        print("UNRESTORABLE")
        return
    print(str(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
S = input()
T = input()

n = len(S)
l = len(T)

ans = -1

for i in range(n-l+1):
  check = True
  for j, k in enumerate([a for a in range(i,i+l)]):
    if T[j] == S[k] or S[k] == "?":
      continue
    check = False
    break

  if check:
    ans = i
    
if ans != -1:
  S = list(S[:i] + T + S[i+l:])
  for i in range(n):
    if S[i] == "?":
      S[i] = "a"
  print("".join(S))
  
else:
  print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
s = input()
t = input()

flag = False
for i in range(len(s) - len(t) + 1)[::-1]:
    cnt = 0
    q = 0
    for j in range(len(t)):
        if s[i + j] == '?':
            q += 1
        elif s[i + j] == t[j]:
            cnt += 1
    
    if cnt == len(t) - q:
        idx = i
        flag = True
        s = s[:idx] + t + s[idx + len(t):]

if flag == True:
    for k in range(len(s)):
        if s[k] == '?':
            s = s[:k] + 'a' + s[k + 1:]
    print(s)
else:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
s = input()
t = input()

flag = False
for i in range(len(s) - len(t) + 1):
    cnt = 0
    q = 0
    for j in range(len(t)):
        if s[i + j] == '?':
            q += 1
        elif s[i + j] == t[j]:
            cnt += 1
    
    if cnt == len(t) - q:
        idx = i
        flag = True
        s = s[:idx] + t + s[idx + len(t):]

if flag == True:
    for k in range(len(s)):
        if s[k] == '?':
            s = s[:k] + 'a' + s[k + 1:]
    print(s)
else:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
sd = input()
t = input()

for i in range(len(sd) - len(t) + 1):
    cnt = 0
    q = 0
    for j in range(len(t)):
        if sd[i + j] == '?':
            q += 1
            continue
        if sd[i + j] == t[j]:
            cnt += 1
    if cnt == len(t) - q:
        idx = i
        s = sd[:idx] + t + sd[idx + len(t):]
        for k in range(len(sd)):
            if s[k] == '?':
                s = s[:k] + 'a' + s[k + 1:]
                print(s)
        break
else:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
s = list(input())
t = list(input())
s.reverse()
t.reverse()
l = len(t)
ld = len(s)-len(t)+1
for i in range(ld):
    cnt = 0
    for j in range(l):
        if s[j+i] == "?" or s[j+i] == t[j]:
            cnt += 1
    if cnt == l:
        ans = []
        for p in range(i):
            if s[p] == "?":
                ans.append("a")
            else:
               ans.append(s[p])
        for p in t:
            ans.append(p)
        for p in range(ld-i-1):
            if s[l+p+i] == "?":
                ans.append("a")
            else:
                ans.append(s[l+i+p])
        ans.reverse()
        for p in ans:
            print(p,end = "")
        break

Submission status: Wrong Answer

Solution code:
s = list(input())
t = list(input())
l = len(t)
ld = len(s)-len(t)+1
for i in range(ld):
    cnt = 0
    for j in range(l):
        if s[j+i] == "?" or s[j+i] == t[j]:
            cnt += 1
    if cnt == l:
        for p in range(i):
            if s[p] == "?":
                print("a",end = "")
            else:
                print(s[p],end = "")
        for p in t:
            print(p,end = "")
        for p in range(ld-i-1):
            if s[l+p+i] == "?":
                print("a",end = "")
            else:
                print(s[l+p+i],end = "")
        break
            
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:

s = list(input())
t = list(input())
l = len(t)
ld = len(s)-len(t)+1
for i in range(ld):
    cnt = 0
    for j in range(l):
        if s[j+i] == "?" or s[j+i] == t[j]:
            cnt += 1
    if cnt == l:
        for p in range(i):
            if s[p] == "?":
                print("a",end = "")
            else:
                print(s[p],end = "")
        for p in t:
            print(p,end = "")
        for p in range(ld-i-1):
            if s[l+p] == "?":
                print("a",end = "")
            else:
                print(s[l+p],end = "")
        break
            
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    s = input() # 暗号
    t = input() # 含まれる文字

    s_len = len(s)
    t_len = len(t)
    flg = False

    for i in range(s_len - t_len,-1,-1):
        flg = True
        for j in range(t_len):
            if s[i+j] != t[j] and s[i+j] != "?":
                flg = False
                break
        if flg == True:
            flg = i
            break

    if flg == False:
        print("UNRESTORABLE")
        exit()

    ans = ""
    for i in s[:flg]:
        if i == "?":
            ans = ans + "a"
        else:
            ans = ans + i
    ans = ans + t

    for i in s[(flg+t_len):]:
        if i == "?":
            ans = ans + "a"
        else:
            ans = ans + i

    print(ans)














if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

sp = input()
t = input()

n = len(sp)
m = len(t)

ok = False
ii = 0 
for i in range(n-m+1):
    for j in range(m):
        if sp[i+j] != '?' and sp[i+j] == t[j]:
            ok = True
            ii = i 
            break
    if ok: 
        break

if not ok: 
    print('UNRESTORABLE')
    exit()

sp = sp[:ii] + t + sp[ii+m:]
sp = list(sp)
for i in range(n):
    if sp[i] == '?':
        sp[i] = 'a' 

sp = ''.join(sp)
print(sp)

Submission status: Wrong Answer

Solution code:
S = input()
T = input()
def match(s,t):
    for c,d in zip(s,t):
        if c == "?":
            continue
        elif c == d:
            continue
        else:
            return False
    return True
for i in range(len(S)):
    s = S[i:i+len(T)]
    if match(s, T):
        print((S[:i]+T+S[i+len(T):]).replace("?","a"))
        exit(0)
print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
S = input()
T = input()
ls = len(S)
lt = len(T)

i = ls - 1
j = lt - 1
tmp = [s for s in S]
while i > -1 and j > -1:
    if S[i] == T[j]:
        i -= 1
        j -= 1
    elif S[i] == '?':
        tmp[i] = T[j]
        i -= 1
        j -= 1
    else:
        i -= 1
        j = lt - 1
        tmp = [s for s in S]

if j == -1:
    ans = ''.join(tmp)
    print(ans.replace('?', 'a'))
else:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
import re
S,T=open(0)
S=S.replace('?','.')
s=len(S)
t=len(T)
for i in range(s-t,-1,-1):
 if re.match(S[i:i+t],T):
  print((S[:i]+T+S[i+t:]).replace('.','a'))
  exit()
print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
import re
S,T=open(0)
S=S.replace('?','.')
s=len(S)
t=len(T)
for i in range(s-t,-1,-1):
 if re.match(S[i:i+t],T):print((S[:i]+T+S[i+t:]).replace('.','a'));exit()
print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
import re
S,T=open(0)
S=S.replace('?','.')
s=len(S)
t=len(T)
for i in range(s-t,-1,-1):
 if re.search(S[i:i+t],T):print((S[:i]+T+S[i+t:]).replace('.','a'));exit()
print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
#abc076 C
from copy import deepcopy
s = list(input())
t = list(input())
#s in t
ls = len(s)
lt = len(t)
rev_s = s[::-1]
rev_t = t[::-1]

for i in range(lt):
    for j in range(i, ls):
        if rev_t[i] == rev_s[j]:
            flag = True
            t_temp = deepcopy(rev_t)
            s_temp = deepcopy(rev_s)
            for k in range(-i, lt-i):
                if j+k >= ls or j+k < 0:
                    flag = False
                    break
                elif t_temp[i+k] == s_temp[j+k]:
                    pass
                elif s_temp[j+k] == "?":
                    s_temp[j+k] = t_temp[i+k]
                elif t_temp[i+k] != s_temp[j+k]:
                    flag = False
                    break
            if flag:
                ns = s_temp[::-1]
                S = "".join(ns)
                ans = S.replace("?", "a")
                print(ans)
                exit()

print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
S_p=input()
T=input()

k=[]
for i in range(len(S_p)-len(T)+1):
	S_sub = S_p[i:i+len(T)]
	if all(s in (t, "?") for s, t in zip(S_sub, T)):
		k.append(i)


if k:
	S=S_p[:k[0]]+T+S_p[k[0]+len(T):]
	print(S.replace("?", "a"))
else:
	print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
idx =-1

for i in range(len(s)-len(t)+1):
    for j in range(len(t)):
        bl = True
        if s[i+j]!="?" and s[i+j] != t[j]:
            bl = False
            break
    if bl:
        idx = i

if idx == -1:
    print("UNRESTORABLE")
else:
    s = s.replace("?", "a")
    ans = s[:i] + t + s[i+len(t):]
    print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
T=input()

lt=len(T)
ls=len(S)
S+='%'*lt

if ls<lt:
    flg=0
else:
    for i in range(ls-lt+1):
        # print('***',S[i:i+lt])
        flg=1
        for j in range(i,i+lt):
            s=S[j]
            # print(s,T[j-i])
            if s=='?': continue
            if s=='%' or s!=T[j-i]:
                flg=0
                break
        if flg==1:
            break
if flg:
    print((S[0:i]+T+S[i+lt:ls]).replace('?','a'))
else:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
S = list(input())
T = list(input())
flag = False

for i in reversed(range(len(S) - len(T) + 1)):
  T_flg = S[i: i + len(T)]
  print(T_flg)
  for j in range(len(T)):
    if T_flg[j] != T[j] and T_flg[j] != "?":
      break
  else:
    flag = True
    S[i:i + len(T)] = T
    break
if flag == False:
  print("UNRESTORABLE")
else:
  S = "".join(map(str,S))
  print(S.replace("?","a"))

Submission status: Wrong Answer

Solution code:
sd = input()
t = input()

sd_len = len(sd)
t_len = len(t)

sd_list = list(sd)
t_list = list(t)

#print(t_list)

for i in range(sd_len):
  if sd[i] == "?":
    continue
  for j in range(t_len):
    sd_len_re = sd_len - i
    if sd[i] == t[j] and sd_len_re >= t_len:
      sd_list[i:] = t[:]
      sd = "".join(sd_list)
      print(sd.replace("?","a"))
      exit()
    elif t_len > sd_len_re:
      break
print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
Sp = input()
T = input()

if len(T) > len(Sp):
	print("UNRESTORABLE")
	exit()
	
oklist = []
for i in range(len(Sp) - len(T), -1, -1):
	for j in range(len(T)):
		if Sp[i + j] != "?" and Sp[i + j] != T[j]:
			break
	else:
		print(i)
		okpre = ["a" if Sp[k] == "?" else Sp[k] for k in range(i)]
		oksuf = ["a" if Sp[k] == "?" else Sp[k] for k in range(i + len(T), len(Sp))] 
		oklist.append("".join(okpre) + T + "".join(oksuf))

if oklist:
	print(min(oklist))
else:
	print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
Sp = input()
T = input()

if len(T) > len(Sp):
	print("UNRESTORABLE")
	exit()
if T in Sp:
	print(Sp.replace("?", "a"))
	exit()
	
oklist = []
for i in range(len(Sp) - len(T), -1, -1):
	for j in range(len(T)):
		if Sp[i + j] != "?" and Sp[i + j] != T[j]:
			break
	else:
		anspre = ["a" if Sp[k] == "?" else Sp[k] for k in range(i)]
		anssuf = ["a" if Sp[k] == "?" else Sp[k] for k in range(len(Sp) - j - i - 1)] 
		oklist.append("".join(anspre) + T + "".join(anssuf))

if oklist:
	print(min(oklist))
else:
	print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
Sp = input()
T = input()

if len(T) > len(Sp):
	print("UNRESTORABLE")
	exit()
if T in Sp:
	print(Sp.replace("?", "a"))
	exit()
for i in range(len(Sp) - len(T), -1, -1):
	for j in range(len(T)):
		if Sp[i + j] != "?" and Sp[i + j] != T[j]:
			break
	else:
		anspre = ["a" if Sp[k] == "?" else Sp[k] for k in range(i)]
		anssuf = ["a" if Sp[k] == "?" else Sp[k] for k in range(len(Sp) - j - i - 1)] 
		print("".join(anspre), T, "".join(anssuf), sep = "")
		exit()
print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
Sp = input()
T = input()

if len(T) > len(Sp):
	print("UNRESTORABLE")
	exit()

for i in range(len(Sp) - len(T), -1, -1):
	for j in range(len(T)):
		if Sp[i + j] != "?" and Sp[i + j] != T[j]:
			break
	else:
		anspre = ["a" if Sp[k] == "?" else Sp[k] for k in range(i)]
		anssuf = ["a" if Sp[k] == "?" else Sp[k] for k in range(len(Sp) - j - i - 1)] 
		print("".join(anspre), T, "".join(anssuf), sep = "")
		exit()
print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
import sys
import math
import re

#https://atcoder.jp/contests/agc008/submissions/15248942
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

s2 = input()
t = input()

#辞書順の小さいものを作るためにパターンマッチは後ろから実施したい
#ので入力を反転させる
s2_r = s2[::-1]
t_r = t[::-1]

ans_flag = False
#全文字の組み合わせを試す
for i in range(len(s2)):
    for j in range(i+1,len(s2)):
        #tと同じ文字数の時だけ部分一致するか判定する
        if (j-i) == len(t_r):
            flag = True
            #一文字ごとに判定する
            for k,l in enumerate(range(i,j)):
                if s2_r[l] != t_r[k] and s2_r[l] != '?':
                    flag = False
            if flag == True:
                #一致した位置を置換
                ans = s2_r[:i] + t_r + s2_r[j:]
                #残りの?を辞書順最小のaに置換
                ans = ans.replace('?','a')
                #元の順番に反転
                ans = ans[::-1]
                print(ans)                
                ans_flag = True
                break

    #forをbreakしないとcontinueする
    else:
        continue
    break
            
if ans_flag == False:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
import sys
import math
import re

#https://atcoder.jp/contests/agc008/submissions/15248942
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

#ランレングス圧縮
def rle_comp(S):
    rle = []
    pre = 'X'
    chain = 1
    for c in S:
        if c == pre:
            chain += 1
        else:
            if pre != 'X':
                rle.append([pre,chain])
            
            pre = c
            chain = 1
    rle.append([pre,chain])
 
    #print(rle)
    return rle

s2 = input()
t = input()

#辞書順の小さいものを作るためにパターンマッチは後ろから実施したい
#ので入力を反転させる
s2_r = s2[::-1]
t_r = ''
t_r += t[::-1]
#ランレングス圧縮
rle = rle_comp(s2_r)
p = ''

#ランレングス圧縮結果を用いて正規表現パターン作成
for c in rle:
    if c[0] == '?':
        p+='.{1,'
        p+=str(c[1])
        p+='}'
    else:
        p+=str(c[0])
        p+='{'
        p+=str(c[1])
        p+='}'

#print(p)
flag = False
#正規表現パターンを部分一致で全探索させていく
for iter_st in re.finditer('{', p):
    mojisuu = 0
    open_index = iter_st.span()[1]-2
    geta_mojisuu = 0
    for iter_end in re.finditer('}', p[:open_index]):
        geta_mojisuu += int(p[iter_end.span()[1]-2])

    for iter_end in re.finditer('}', p[open_index:]):
        close_index = open_index+iter_end.span()[1]
        #print(p[open_index:close_index])
        #正規表現パターンの文字数がtの文字数以上のときのみ判定する
        mojisuu += int(p[close_index-2])
        #print(mojisuu)
        if mojisuu >= len(t_r):
            m = re.search(p[open_index:close_index],t_r)
            if m != None:
                #print(m)
                #print(s2_r)
                #print(mojisuu)
                ans = s2_r[:geta_mojisuu+m.span()[0]] + t_r + s2_r[geta_mojisuu+m.span()[1]:]
                ans = ans.replace('?','a')
                ans = ans[::-1]
                print(ans)
                flag = True
                break
    #forをbreakしないとcontinueする
    else:
        continue
    break


if flag == False:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
import sys
import math
import re

#https://atcoder.jp/contests/agc008/submissions/15248942
sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

#ランレングス圧縮
def rle_comp(S):
    rle = []
    pre = 'X'
    chain = 1
    for c in S:
        if c == pre:
            chain += 1
        else:
            if pre != 'X':
                rle.append([pre,chain])
            
            pre = c
            chain = 1
    rle.append([pre,chain])
 
    #print(rle)
    return rle

s2 = input()
t = input()

#辞書順の小さいものを作るためにパターンマッチは後ろから実施したい
#ので入力を反転させる
s2_r = s2[::-1]
t_r = ''
t_r += t[::-1]
#ランレングス圧縮
rle = rle_comp(s2_r)
p = ''

#ランレングス圧縮結果を用いて正規表現パターン作成
for c in rle:
    if c[0] == '?':
        p+='.{1,'
        p+=str(c[1])
        p+='}'
    else:
        p+=str(c[0])
        p+='{'
        p+=str(c[1])
        p+='}'

flag = False
mojisuu = 0
#正規表現パターンを部分一致させていく
for iter_m in re.finditer('}', p):
    clo_index = iter_m.span()[1]
    #正規表現パターンの文字数がtの文字数以上のときのみ判定する
    mojisuu += int(p[clo_index-2])
    if mojisuu >= len(t_r):
        m = re.search(p[:clo_index],t_r)
        if m != None:
            ans = s2_r[:m.span()[0]] + t_r + s2_r[m.span()[1]:]
            ans = ans.replace('?','a')
            ans = ans[::-1]
            print(ans)
            flag = True


if flag == False:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
S = input()[::-1]
T = input()[::-1]

flag = False
for i in range(len(S) - len(T)):
    matched = True

    for j in range(len(T)):
        if S[i + j] != "?" and S[i + j] != T[j]:
            matched = False
    
    if matched:
        S = S[:i] + T + S[i + len(T):]
        flag = True
        break

if flag:
    print(S.replace("?", "a")[::-1])
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
s = list(input())
t = list(input())
ls, lt = len(s), len(t)
#リストを反転させてケツから調べる事で辞書順最小を求める
s.reverse()
t.reverse()
for i in range(ls):
    for j in range(lt): #このfor文が全部回ればt in sである
        if i + j >= ls:
            break
        if s[i+j] != '?' and s[i+j] != t[j]:
            break
    else: 
        for j in range(lt):
            s[i+j] = t[j]
        s.reverse()
        ans = ''.join(s)
        ans.replace('?','a')
        break
else:
    ans = 'UNRESTORABLE'
print(ans)

Submission status: Wrong Answer

Solution code:
Sd=input()
T=input()
sd=len(Sd)
t=len(T)
if sd>=t:
    q=Sd.count('?')
    for i in range(sd-t+1):
        x=Sd[i:i+t]
        if all(x[i]=='?' or x[i]==T[i] for i in range(len(x))):
            ans=[]
            Sd=Sd[0:i]+T+Sd[i+t:]
            for s in Sd:
                if s=='?':
                    ans.append('a')
                else:
                    ans.append(s)
            print(''.join(ans))
            exit()
    print('UNRESTORABLE')
else:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
Sd=input()
T=input()
sd=len(Sd)
t=len(T)
if sd>=t:
    q=Sd.count('?')
    for i in range(sd-t+1):
        x=Sd[i:i+t]
        if all(x[i]=='?' or x[i]==T[i] for i in range(len(x))):
            ans=[]
            Sd=Sd[0:i]+T+Sd[i+t:]
            for s in Sd:
                if s=='?':
                    ans.append('a')
                else:
                    ans.append(s)
            print(''.join(ans))
            exit()
print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
Sd=input()
T=input()
sd=len(Sd)
t=len(T)
if sd>=t:
    q=Sd.count('?')
    for i in range(sd-t+1):
        x=Sd[i:i+t]
        if all(x[i]=='?' or x[i]==T[i] for i in range(len(x))):
            ans=[]
            Sd=Sd[0:i]+T+Sd[i+t:]
            for s in Sd:
                if s=='?':
                    ans.append('a')
                else:
                    ans.append(s)
            print(''.join(ans))
            exit()
print('UNRESTOABLE')

Submission status: Wrong Answer

Solution code:
s = input()[::-1]
t = input()[::-1]

def match(string,substring):
    for a,b in zip(string, substring):
        if a == '?':
            continue
        if a != b:
            return False
    return True

# なるべく後方で一致させる
LEN_S = len(s)
LEN_S = len(t)
ans = ''
for i in range(LEN_S - LEN_S + 1):
    sub = t[i:i+LEN_S]
    if match(s, sub):
        ans = s[:i] + sub + s[i+LEN_S:]
        ans = ans[::-1].replace('?', 'a')
        break
else:
    print('UNRESTORABLE')
    exit()

print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
T=input()

keys=[]
for i in range(2**(len(T))):
    key=''
    for j in range(len(T)):
        if (i>>j)&1:
            key+='?'
        else:
            key+=T[j]
    keys.append(key)
    
res=''
for key in sorted(keys)[::-1]:
    if S.find(key)>-1:
        res=(S[::-1]).replace(key[::-1],T[::-1],1)[::-1]
        break
else:
    res='UNRESTORABLE'
    
print(res.replace('?','a'))

Submission status: Wrong Answer

Solution code:
S=input()
T=input()

keys=[]
for i in range(2**(len(T))):
    key=''
    for j in range(len(T)):
        if (i>>j)&1:
            key+='?'
        else:
            key+=T[j]
    keys.append(key)
    
res=''
for key in sorted(keys)[::-1]:
    if S.find(key)>-1:
        res=S.replace(key,T,1)
        break
else:
    res='UNRESTORABLE'
    
print(res.replace('?','a'))

Submission status: Wrong Answer

Solution code:
S=input()
T=input()

for j in reversed(range(len(T)+1)):
    key=T[:j]+'?'*(len(T)-j)
    key_index=S.find(key)
    if key_index>-1:
        S=S.replace(key,T,1)
        break
else:
    for j in range(len(T)+1):
        key='?'*j+T[j:]
        key_index=S.find(key)
        if key_index>-1:
            S=S.replace(key,T,1)
if key_index>-1:
    print(S.replace('?','a'))
else:
    print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
S = list(input())
T = list(input())
flag = False
C = []
for i in range(len(S)):
  flag = False
  ii = len(S) - i
  for j in range(len(T)):
    if (ii+j >= len(S)):
      flag = False
      break
    elif S[ii+j]==T[j] or S[ii+j] == "?":
      flag = True
    else:
      flag = False
      break
  if (flag):
    for k in range(len(T)):
      S[ii+k] = T[k]
    C.append("".join(S).replace("?","a"))
if (C != []):
  print(min(C))
else:
  print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
S = input()
T = input()

for i in range(len(S) - len(T)+1):
    target = S[len(S) - len(T) - i:len(S) - i]
    print(target)

    can_search = True
    for key in range(len(T)):
        if target[key] != '?':
            if target[key] != T[key]:
                can_search = False
                break

    if can_search:
        new_key = S[:len(S) - len(T) - i]
        new_key += T
        new_key += S[len(new_key):]
        print(new_key.replace('?', 'a'))
        exit()

print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
S = input()
T = input()

for i in range(len(S) - len(T)):
    target = S[len(S) - len(T) - i:len(S) - i]

    can_search = True
    for key in range(len(T)):
        if target[key] != '?':
            if target[key] != T[key]:
                can_search = False
                break

    if can_search:
        new_key = S[:len(S) - len(T) - i]
        new_key += T
        new_key += S[len(new_key):]
        print(new_key.replace('?', 'a'))
        exit()

print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
S = list(input())
T = list(input())
flag = False
for i in range(len(S)):
  ii = len(S) - i
  for j in range(len(T)):
    if (ii+j >= len(S)):
      flag = False
      break
    elif S[ii+j]==T[j] or S[ii+j] == "?":
      flag = True
    else:
      flag = False
      break
  if (flag):
    for k in range(len(T)):
      S[ii+k] = T[k]
    break
if (flag):
  print("".join(S).replace("?","a"))
else:
  print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
S = list(input())
T = list(input())
flag = False
for i in range(len(S)):
  ii = len(S) - i
  for j in range(len(T)):
    if (ii+j >= len(S)):
      flag = False
      break
    elif S[ii+j]==T[j] or S[ii+j] == "?":
      flag = True
    else:
      flag = False
      break
  if (flag):
    for k in range(len(T)):
      S[ii+k] = T[k]
    break
print(S)
if (flag):
  print("".join(S).replace("?","a"))
else:
  print("UNRESTORABLE")