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: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    s = S()
    t = S()
    ans = "UNRESTORABLE"
    for i in range(len(s)-len(t),-1,-1):
        part = s[i:i+len(t)]
        for schar, tchar in zip(part, t):
            if schar!=tchar and schar!="?":
                break
        else:
            ans = s[:i] + t + s[i+len(t):]
            ans = ans.replace("?","a")
            break
    print(ans)
main()

Submission status: Accepted

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)-len(T), -1, -1):
    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: Accepted

Solution code:
S=input()
T=input()
if len(S)<len(T):
  print('UNRESTORABLE')
  exit()
else:
  list=[]
  for i in range(len(S)-len(T)+1):
    count=0
    for j in range(len(T)):
      if S[i+j]==T[j] or S[i+j]=='?':
        count+=1
    if count==len(T):
      SS=S[:i]+T+S[i+len(T):]
      for m in range(len(S)):
        if SS[m]=='?':
          SS=SS[:m]+'a'+SS[m+1:]
      list.append(SS)
list.sort()
if list==[]:
  print('UNRESTORABLE')
else:
  print(list[0])

Submission status: Accepted

Solution code:
s = input()
t = input()
match_index = 51
for i in range(len(s) - len(t) + 1):
    s_tmp = s[i:i + len(t)]
    for j in range(len(t)):
        if s_tmp[j] == t[j] or s_tmp[j] == "?":
            pass
        else:
            break
    else:
        match_index = i

if match_index == 51:
    print("UNRESTORABLE")
else:
    s_replace = ""
    for i in range(match_index):
        s_replace += s[i]
    s_replace += t
    for i in range(match_index + len(t), len(s)):
        s_replace += s[i]

    # s[match_index:match_index + len(t)] = t
    res = ""
    for i in s_replace:
        if i == "?":
            res += "a"
        else:
            res += i
    print(res)

Submission status: Accepted

Solution code:
import re
S,T=open(0)
S=S.replace('?','.')
T=T[:-1]
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: Accepted

Solution code:
s=list(input())
t=list(input())
if len(t)>len(s):
    print("UNRESTORABLE")
    exit()
n=len(s)
m=len(t)
ans=[]
for i in range(n-m+1):
    x=s.copy()
    f=0
    for j in range(m):
        if x[i+j]==t[j]:
            f+=1
        elif x[i+j]=="?":
            x[i+j]=t[j]
            f+=1
    if f==m:
        for j in range(n):
            if x[j]=="?":
                x[j]="a"
        ans.append("".join(x))
if len(ans)==0:
    print("UNRESTORABLE")
else:
    ans=sorted(ans)
    print(ans[0])

Submission status: Accepted

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

s_length = len(s)
t_length = len(t)

ans = ''
keep = ''
remind = 0
checker = False
for i in range(s_length):
    if (i + t_length) <= s_length:
        check = True
        for j in range(t_length):
            if (s[i + j] != t[j]) & (s[i + j] != '?'):
                check = False
        if check:
            checker = True
            ans = keep + t
            remind = i + t_length
    if s[i] == '?':
        keep += 'a'
    else:
        keep += s[i]
ans = ans + keep[remind:]

if checker:
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
# 解説を読みながら自作

S = input()
T = input()

s_len = len(S)
t_len = len(T)

res = []

init_K = "?" * s_len
for i in range(s_len - t_len + 1):
    K = list(init_K[:i] + T + init_K[i + t_len:])
    # KをSにしたがって書き換えていく。書き換えられない→正解ではない
    for j in range(s_len):
        if S[j] == "?" or S[j] == K[j]:  # Sが"?"またはSとKが一致、書き換える必要なし
            pass
        elif K[j] == "?":  # Sが"?"ではなくてKが"?"、書き換える
            K[j] = S[j]
        else:  # それ以外、書き換えられない
            break
    else:  # breakしなかった
        # 辞書順で最小になるように、残ったKの"?"を"a"に変える
        res.append("".join(K).replace("?", "a"))

if res:
    res.sort()
    print(res[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
#abc076 C
from copy import deepcopy
s = list(input())
t = list(input())
ls = len(s)
lt = len(t)

rev_s = s[::-1]
rev_t = t[::-1]

for x in range(ls-lt+1):
    if rev_s[x:x+lt] == ["?"] * lt:
        for y in range(lt):
            rev_s[x+y] = rev_t[y]
        ns = rev_s[::-1]
        S = "".join(ns)
        ans = S.replace("?", "a")
        print(ans)
        exit()


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: Accepted

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

for i in range(len(t) - 1, len(s))[-1::-1]:
    if s[i] == "?" or t[-1]:
        jud = True
        for j in range(len(t)):
            if s[i - j] == t[-1 - j] or s[i - j] == "?":
                continue
            jud = False
            break
        if jud:
            for j in range(len(t)):
                s[i - j] = t[-1 - j]
            for j in range(len(s)):
                if s[j] == "?":
                    s[j] = "a"
            print("".join(s))
            exit()

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s,t=input(),input()
ans='z'*len(s)
for i in range(len(s)-len(t)+1):
    pre=s[:i].replace("?",'a')
    
    for x,y in zip(list(s[i:]),list(t)):
        if x!="?"and x!=y:break
        pre+=y
    else:
        pre+=s[i+len(t):].replace("?",'a')
        ans=min(ans,pre)
print(ans if ans!='z'*len(s) else 'UNRESTORABLE')

Submission status: Accepted

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

ans = 'z' * len(s)
tmp = 'z' * len(s)

x = False
for i in range(len(s)-len(t)+1):
    ok = True
    for j in range(len(t)):
        if not (s[i+j] == '?' or s[i+j] == t[j]):
            ok = False
    
    if ok:
        x = True
        tmp = s[:i] + t + s[i+len(t):]
        if ans > tmp:
            ans = tmp

if x:
    ans = ans.replace("?", "a")
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

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[-1]]+T+S_p[k[-1]+len(T):]
	print(S.replace("?", "a"))
else:
	print("UNRESTORABLE")

Submission status: Accepted

Solution code:
def main():
    s=input()
    t=input()
    ans=[]
    for i in range(len(s)-len(t)+1):
        for j in range(len(t)):
            if len(s) <= i+j or (s[i+j] != t[j] and s[i+j] != "?"):
                break
        else:
            ans.append((s[:i]+t+s[i+j+1:]).replace("?", "a"))
    ans.sort()
    print(ans[0] if len(ans) > 0 else "UNRESTORABLE")
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(len(s) - len(t), -1, -1):
    for j in range(len(t)):
        if s[i+j] != t[j] and s[i+j] != '?':
            break
    else:
        for j in range(len(t)):
            s[i+j] = t[j]
        for j in range(len(s)):
            if s[j] == '?':
                s[j] = 'a'
        print(''.join(s))
        exit()

print('UNRESTORABLE')

Submission status: Accepted

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

lt=len(T)
ls=len(S)
if ls<lt:f=0
else:
 for i in range(ls-lt,-1,-1):
  f=1
  for s,t in zip(S[i:i+lt],T):
    if s=='?' or s==t: continue
    f=0
  if f:break
print((S[0:i]+T+S[i+lt:ls]).replace('?','a') if f else 'UNRESTORABLE')

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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,-1,-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: Accepted

Solution code:
t = input()
s = input()
ans = ""
l = len(s)
for i in range(len(t)-len(s)+1):
    p = t[i:i+l]
    count = 0
    for j in range(l):
        if s[j] == p[j] or p[j] == "?":
            count += 1
        else:
            break
    if count == l:
        ans = t[:i]+s+t[i+l:]
if ans == "":
    print("UNRESTORABLE")
    exit()

for i in range(len(ans)):
    if ans[i] == "?":
        print("a", end="")
    else:
        print(ans[i], end="")
print()

Submission status: Accepted

Solution code:

s_o = str(input().strip())
t_o = str(input().strip())
dic = []

for i in range(len(s_o)-len(t_o)+1):
    s = s_o
    t = t_o
    flg = 0
    for j in range(len(t_o)):
        if s[i+j] == t[j] or s[i+j] == "?":
            s = s[:i+j] + t[j] + s[i+j+1:]
        else:
            flg = 1
            break
    if flg:
        continue
    s = s.replace("?", "a")
    dic.append(s)

if len(dic) == 0:
    print("UNRESTORABLE")
else:
    dic.sort()
    print(dic[0])

Submission status: Accepted

Solution code:
import re
Sd = input()
T = input()
Sdls = list(Sd)
Sdls.reverse()
Sdre = ''.join(Sdls)
Tls = list(T)
Tls.reverse()
Tre = ''.join(Tls)
st = -1
for i in range(0,len(Sd)-len(T)+1):
    f = 1
    for j in range(len(T)):
        if Tre[j] == Sdre[i+j] or Sdre[i+j] == '?':
            continue
        else:
            f = 0
            break
    if f == 1:
        st = i
        break
if st == -1:
    print('UNRESTORABLE')
else:
    Sdrels = list(Sdre)
    for j in range(len(T)):
        Sdrels[j+st] = Tre[j]
    for j in range(len(Sd)):
        if Sdrels[j] == '?':
            Sdrels[j] = 'a'
    ans = ''.join(reversed(Sdrels))
    print(ans)

Submission status: Accepted

Solution code:
S_ = list(input())
T = input()
#後ろから探索する

n = len(S_)
m = len(T)
nonexist = 1
for i in range(n-1, m-2, -1):
    flg = True
    for j in range(m):
        if (S_[i-j] != T[-j-1]) and (S_[i-j] != "?"):
            flg = False
            break
    if flg:
        nonexist = 0
        index = i
        break

if nonexist:
    print("UNRESTORABLE")
    exit()

index -= m-1
for i in range(m):
    S_[index+i] = T[i]

for i in range(n):
    if S_[i] == "?":
        S_[i] = "a"
print("".join(S_))

Submission status: Accepted

Solution code:
s = list(input())
t = list(input())
num = len(t)

fail = 'UNRESTORABLE'
flag = False
ans = []
count_out = 0

for i in range(len(s)):
    start = i
    end = i + len(t)
    if (end > len(s)):
        break
    tar = s[start:end]
    # print(tar)
    index = 0
    name_flag = True

    for a, b in zip(tar, t):
        # print(tar[index])
        if (a == b):
            pass
        elif (a == '?'):
            tar[index] = str(b)
        else:
            name_flag = False
            break

        index += 1

    if (name_flag is False):
        ans.append(['z'] * len(s))
        count_out += 1
    else:
        pre_org = s[:start]
        # pre = pre_org.replace('?', 'a')
        post_org = s[end:]
        # post = post_org.replace('?', 'a')

        # tar_mod = pre + tar + post
        tar_mod = pre_org + tar + post_org
        ans.append(tar_mod)

# print(ans)
if (count_out == (len(s) - len(t) + 1)):
    print(fail)
else:
    ans_s = sorted(ans)
    aa = ans_s[0]
    a2 = ''.join(aa)
    a3 = a2.replace('?', 'a')
    print(a3)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    S = SI()
    T = SI()
    ls = len(S)
    lt = len(T)
    R = []
    for i in range(ls-lt+1):
        SS = S[i:i+lt]
        is_ok = True
        for s, t in zip(SS, T):
            if s != "?" and s != t:
                is_ok = False
        if not is_ok:
            continue
        r = ""
        for j, s in enumerate(S):
            if j == i:
                r += T
            elif i < j < i+lt:
                pass
            elif s == "?":
                r += "a"
            else:
                r += s
        R.append(r)
    R.sort()
    if R:
        print(R[0])
    else:
        print("UNRESTORABLE")



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
t = input()
n = len(s)
m = len(t)
ans = []

for i in range(n-m+1):
  	#tと同じ文字数をsから抜き出す
    c = 0
    for j in range(m):
        if s[i+j] == '?' or s[i+j] == t[j]:
          	#sの文字が"?"またはt[j]と同じ場合
            c += 1
    if c == m:
      	#文字の変換
        s_ = s[0:i] + t + s[i+m:]
        s_ = s_.replace('?', 'a')
        ans.append(s_)
if len(ans)>0:
    print(sorted(ans)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

n = len(sd)
m = len(t)
s = []

# sd を後ろから見ていき、 t の入りそうな場所を探す
for i in range(n - m, -1, -1):
    t_kamo = sd[i:i + m]
    for j in range(m + 1):
        # 1文字ずつ順に入りうるか調べ、最後まで入るなら "?" を "a" に置き換えて出力
        if j == m:
            print((sd[:i] + t + sd[i + len(t):]).replace("?", "a"))
            exit()
        if t_kamo[j] == "?":
            continue
        elif t_kamo[j] != t[j]:
            break

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import math

sd = list(input())
t = list(input())

dic = []

slen = len(sd)
tlen = len(t)
for i in range(slen - tlen + 1):
    for j in range(tlen):
        if not (t[j] == sd[i + j] or sd[i + j] == "?"):
            break
    else:
        s = sd[:i] + t + sd[i + tlen:]
        for k in range(len(s)):
            if s[k] == "?":
                s[k] = "a"
        dic.append("".join(s))

if len(dic) == 0:
    print("UNRESTORABLE")
    exit()
print(sorted(dic)[0])

Submission status: Accepted

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:
		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: Accepted

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)+1):
        #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: Accepted

Solution code:
import re

s = input().replace('?', '.')
t = input()

for i in range(len(s)-len(t), -1, -1):
    if re.match(s[i:i+len(t):], t):
        s = s.replace('.', 'a')
        print(s[:i:]+t+s[i+len(t)::])
        exit()
print('UNRESTORABLE')

Submission status: Accepted

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

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

    for j in range(len(T)):

        if S[i + j] != "?" and S[i + j] != T[j]:
            matched = False
            break
    
    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: Accepted

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

flag = False
for i in range(len(S) - len(T) + 1):
    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: Accepted

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

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

    for j in range(len(T)):
        # うーん...
        if i == len(S) - len(T) + 1:
            break

        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: Accepted

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 = [x if x != '?' else 'a' for x in s]
        s.reverse()
        ans = ''.join(s)
        break
else:
    ans = 'UNRESTORABLE'
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
t = list(input())
ls =len(s)
lt = len(t)
s.reverse()
t.reverse()
for i in range(ls):
    for j in range(lt):
        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 = [x if x != '?' else 'a' for x in s]
        s.reverse()
        ans = ''.join(s)
        break
else:
    ans = 'UNRESTORABLE'
print(ans)

Submission status: Accepted

Solution code:
s = input()
t = input()
n = len(s)
m = len(t)
ans = []
for i in range(n-m+1):
    c = 0
    for j in range(m):
        if s[i+j] == '?' or s[i+j] == t[j]:
            c += 1
    if c == m:
        s_ = s[0:i] + t + s[i+m:]
        s_ = s_.replace('?', 'a')
        ans.append(s_)
if len(ans)>0:
    print(sorted(ans)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
def main():
    s = input()
    t = input()
    n = len(s)
    m = len(t)
    ans = []
    for i in range(n-m+1):
        c = 0
        for j in range(m):
            if s[i+j] == '?' or s[i+j] == t[j]:
                c += 1
        if c == m:
            s_ = s[0:i] + t + s[i+m:]
            s_ = s_.replace('?', 'a')
            ans.append(s_)
    if len(ans)>0:
        print(sorted(ans)[0])
    else:
        print('UNRESTORABLE')

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S_ = input()
T = input()
len_T = len(T)
len_S = len(S_)

A = []
for i in range(len_S - len_T + 1):
    flg = True
    for t, s in zip(T, S_[i:i + len_T]):
        if s != '?' and s != t:
            flg = False
    if flg == True:
        S = ''
        for j in range(len_S):
            if i <= j < i + len_T:
                S += T[j - i]
            elif S_[j] == '?':
                S += 'a'
            else:
                S += S_[j]
        A.append(S)

ans = 'z' * (len_S + 1)
for S in A:
    if S < ans:
        ans = S
if ans == 'z' * (len_S + 1):
    print('UNRESTORABLE')
else:
    print(ans)

Submission status: Accepted

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

ans=''
loc=None
for i in range(ls-lt,-1,-1):
  flag=0
  for j in range(lt):
    if not (S[i+j] == T[j] or S[i+j] == '?'):
      flag=1
      break
  if flag==0:
    loc=i
    break

if loc == None:
  print('UNRESTORABLE')
else:
  i=0
  while i<ls:
    if i==loc:
      ans += T
      i += lt
    elif S[i] == '?':
      ans += 'a'
      i += 1
    else:
      ans += S[i]
      i += 1
  print(ans)

Submission status: Accepted

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_T = len(t)
ans = ''
for i in range(LEN_S - LEN_T + 1):
    sub = s[i:i+LEN_S]
    if match(sub, t):
        ans = s[:i] + t + s[i+LEN_T:]
        ans = ans[::-1].replace('?', 'a')
        break
else:
    print('UNRESTORABLE')
    exit()

print(ans)

Submission status: Accepted

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

l = len(S)
k = len(T)
f = 0


if k > l:
    f = 0
else:
    for i in reversed(range(0,l-k+1)):
        #print(i)
        for j in range(k):
            #print(S[i+j], T[j])
            if S[i + j] != T[j] and S[i + j] != "?":
                #print(i+j)
                break
            if j == k-1:
                f = 1
                s = i
        if f == 1:
            break

if f == 0:
    print("UNRESTORABLE")
else:
    S = S[:s] + T + S[s+k:]
    for i in range(l):
        if S[i] == "?":
            S = S[:i] + "a" + S[i+1:]
    print(S)

Submission status: Accepted

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

def isOK(s):
    res=[]
    if len(s)<len(T):
        return res
    for i in range(len(s)-len(T)+1):
        ns=s[i:i+len(T)]
        for j in range(len(T)):
            if ns[j]!='?' and not ns[j]==T[j]:
                break
        else:
            res.append(s[:i]+T+s[i+len(T):])
    return res

OK_list=isOK(S)
if OK_list:
    OK_list.sort()
    print(OK_list[0].replace('?','a'))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
Sp=list(input())
Tp=list(input())
L=len(Sp)
K=len(Tp)
for i in reversed(range(L-K+1)):
  for j in range(K):
    a=Sp[i+j]
    b=Tp[j]
    if a!='?' and a!=b:
      break
  else:
    for j in range(K):
      Sp[i+j]=Tp[j]
    break
else:
  print('UNRESTORABLE')
  exit()
for i in range(L):
  if Sp[i]=='?':
    Sp[i]='a'
print(''.join(Sp))

Submission status: Accepted

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

soln = ""

for i in range(len(s)-len(t)+1):
    sol = True
    for j in range(len(t)):
        if not (s[i+j] == t[j] or s[i+j] == "?"):
            sol = False
            break

    if sol:
        soln = s[:i] + t + s[i+len(t):]

print(soln.replace("?", "a") if soln != "" else "UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
t_len = len(T)
s_len = len(S)
index = 0
if t_len > s_len:
    print("UNRESTORABLE")
    exit()
for i in range((s_len - 1) - (t_len - 1), -1, -1):
    for j in range(t_len):
        if S[i + j] == "?":
            continue
        if S[i + j] != T[j]:
            break
    else:
        index = i
        break
    continue
else:
    print("UNRESTORABLE")
    exit()
S = list(S)
for i in range(s_len):
    if S[i] == "?":
        S[i] = 'a'
S[index:index + t_len] = T
print("".join(S))
#print(S)

Submission status: Accepted

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

unrestorable = 'z'*50
ans = unrestorable
for i in range(ls-lt+1):
    for j, t in enumerate(T):
        s = S[i+j]
        if s != t and s != '?':
            break
    else:
        key = (S[:i]+T+S[i+lt:]).replace('?', 'a')
        ans = min(ans, key)

print(ans if ans != unrestorable else 'UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

s = readline().decode().rstrip()
t = list(readline().decode().rstrip())

L = []
for i in range(len(s)-len(t)+1):
    s1 = list(s)[:i:]
    s2 = list(s)[i::]
    is_ok = True

    for j in range(len(t)):
        if s2[j] == '?' or s2[j] == t[j]:
            s2[j] = t[j]
        else:
            is_ok = False
            break
    if is_ok:
        a = "".join(s1) + "".join(s2)
        a = a.replace('?', 'a')
        L.append(a)
L.sort()

print('UNRESTORABLE' if len(L) == 0 else L[0])

Submission status: Accepted

Solution code:
s = input()
t = input()
n, m = len(s), len(t)

for i in range(n - m, -1, -1):
    match = True
    for j in range(m):
        if s[i + j] != '?' and s[i + j] != t[j]:
            match = False
            break
    if match:
        print(s[:i].replace('?', 'a') + t + s[i + m:].replace('?', 'a'))
        exit()
print("UNRESTORABLE")

Submission status: Accepted

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]

    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: Accepted

Solution code:
S = input().strip()
N = len(S)
T = input().strip()
K = len(T)
flag = 0
ind = -1
for i in range(N-K,-1,-1):
    A = list(S[i:i+K])
    flag = 0
    for j in range(K):
        if A[j]!="?" and A[j]!=T[j]:
            flag = 1
            break
    if flag == 0:
        ind = i
        break
if ind<0:
    print("UNRESTORABLE")
else:
    S = list(S)
    for j in range(K):
        if S[ind+j]=="?":
            S[ind+j] = T[j]
    for i in range(N):
        if S[i]=="?":
            S[i]="a"
    print("".join(S))

Submission status: Accepted

Solution code:
S = input()
T = input()
anslist = []
for i in range(len(S)-len(T)+1):
    kouho = [0]*len(S)
    ok = 1
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == "?":
            kouho[i+j] = T[j]
        else:
            ok = 0
            break
    if ok == 1:
        for k in range(len(S)):
            if kouho[k] == 0:
                if S[k] != "?":
                    kouho[k] = S[k]
                else:
                    kouho[k] = "a"
        kouho = ''.join(kouho)
        anslist.append(kouho)
if len(anslist) == 0:
    ans = "UNRESTORABLE"
else:
    anslist.sort()
    ans = anslist[0]
print(ans)

Submission status: Accepted

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

n = len(s)
m = len(t)

for i in range(n-m, -1, -1):
  x = s[i:i+m]
  for j in range(m+1):
    if j == m:
      print((s[:i]+t+s[i+m:]).replace("?","a"))
      exit()
    if x[j] == "?": continue
    elif x[j] != t[j]: break
    
print("UNRESTORABLE")

Submission status: Accepted

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

n = len(s)
m = len(t)
if m > n:
    print('UNRESTORABLE')
    exit()

ans = []
for i in range(n-m+1):
    flag = True
    for j in range(m):
        if s[i+j] != t[j]:
            if s[i+j] != '?':
                flag = False
    if flag:
        p = s[:i]
        q = s[i+m:]
        temp = p.replace('?', 'a')+t+q.replace('?', 'a')
        ans.append(temp)

ans.sort()
if ans:
    print(ans[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s = input()[::-1]
t = input()[::-1]
x = ""
for i in range(0,len(s)-len(t)+1):
    if x == "": x = s[i:i+len(t)]
    else:x = x[1:] + s[i+len(t)-1]
    flag = True
    for j in range(len(t)):
        if x[j] == "?" or x[j] == t[j]: continue
        else: flag = False
    if flag:
        s = (s[:i] + t + s[i+len(t):])[::-1]
        print(s.replace("?", "a"))
        exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s = input()
t = input()
n, m = len(s), len(t)

for i in range(n - m, -1, -1):
    match = True
    for j in range(m):
        if s[i + j] == '?' or s[i + j] == t[j]:
            continue
        else:
            match = False
            break

    if match:
        ans = ""
        for j in range(i):
            if s[j] == '?':
                ans += 'a'
            else:
                ans += s[j]
        ans += t
        for j in range(i + m, n):
            if s[j] == '?':
                ans += 'a'
            else:
                ans += s[j]
        print(ans)
        exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
for k in range(len(S)-len(T),-1,-1):
    f = 1
    for i in range(len(T)):
        if S[k+i] != "?" and S[k+i] != T[i]:
            f = 0
            break
    if f == 1:
        print(S[:k].replace("?","a")+T+S[k+len(T):].replace("?","a"))
        exit(0)
print("UNRESTORABLE")