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:
s_prime=str(input())
t=str(input())
def solve(s_prime,t):
  ls=len(s_prime)
  lt=len(t)
  for i in range(ls-lt+1):
    temp='?'*(ls-lt-i)+t+'?'*i
    # print(temp)
    f=True
    for j in range(ls):
      if s_prime[j]=='?':
        continue
      elif temp[j] in ['?',s_prime[j]]:
        continue
      f=False
    if f==True:
      ans=''
      for j in range(ls):
        if temp[j]=='?':
          if s_prime[j]=='?':
            ans+='a'
          else:
            ans+=s_prime[j]
        else:
          ans+=temp[j]
      return ans
  return 'UNRESTORABLE'
  
print(solve(s_prime,t))

Submission status: Accepted

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

for i in reversed(range(len(S)-len(T)+1)):
  c=0
  for j in range(len(T)):
    if S[i+j]==T[j] or S[i+j]=="?":
      c+=1
    else:
      break
  if c==len(T):
    for j in range(len(T)):
      S[i+j]=T[j]
    for k in range(len(S)):
      if S[k]=="?":
        S[k]="a"
    print("".join(S))
    exit()

print("UNRESTORABLE")

Submission status: Accepted

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

# Tを含み条件を満たす文字列全てのリストを作る
S_list = []
for T_first_idx in range(len(S_prime) - len(T) + 1):
    ok_flg = 1
    for i in range(len(T)):
        if S_prime[T_first_idx+i] != "?" and S_prime[T_first_idx+i] != T[i]:
            ok_flg = 0
    if ok_flg == 1:
        S_list.append(S_prime[:T_first_idx] + T + S_prime[T_first_idx+len(T):])
        
if len(S_list) == 0:
    print("UNRESTORABLE")
else:
    print(min(S_list).replace("?", "a"))

Submission status: Accepted

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

rs = list(S[::-1])
rt = list(T[::-1])

idx = None
for i in range(len(rs)-len(rt)+1):
  flg = True
  for j in range(len(rt)):
    si = i + j
    if rs[si] != rt[j] and rs[si] != '?':
      flg = False
  if flg:
    idx = i
    break

if idx is None:
  print('UNRESTORABLE')
else:
  for i in range(idx, idx+len(rt)):
    rs[i] = rt[i-idx]
  for i in range(len(rs)):
    if rs[i] == '?':
      rs[i] = 'a'
  print("".join(rs[::-1]))

Submission status: Accepted

Solution code:

S = input()
T = input()

if len(T)>len(S):
    ans = "UNRESTORABLE"
else:
    Slr = list(S[::-1])
    Tlr = list(T[::-1])
    for i in range(len(S)-len(T)+1):
        flag = True
        for j in range(len(T)):
            if Slr[i+j]==Tlr[j] or Slr[i+j]=="?":
                pass
            else:
                flag = False
                break
        if flag:
            for j in range(len(T)):
                Slr[i+j] = Tlr[j]
            for j in range(len(S)):
                if Slr[j]=="?":
                    Slr[j] = "a"
            ans = ("".join(Slr[::-1]))
            break
        else:
            if i==len(S)-len(T):
                ans = "UNRESTORABLE"
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def check(in_str,out_str):
  for i,j in zip(in_str,out_str):
    if j == "?" or i==j:
      continue
    else:
      return False
  else:
    return True
  
for i in range(len(S)-len(T),-1,-1):
  if check(T, S[i:i+len(T)]):
    print((S[:i]+T+S[i+len(T):]).replace("?","a"))
    break
else:
  print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = str(input())  # ?が入っている文字列
T = str(input())  # 部分文字列

# 2重ループで大丈夫

index = None
for i in range(len(S)):
    if S[i] == T[0] or S[i] == '?':
        # ループを始める
        start = i
        cnt = 0
        for j in range(i, min(len(T)+i, len(S))):
            if S[j] == T[j-i] or S[j] == '?':
                cnt += 1
            else:
                break

        if cnt == len(T):
            index = start  # 保存しておく

if index != None:
    i = 0
    ans = ""
    while len(S) > i:
        if i != index:
            if S[i] == '?':
                ans += 'a'
            else:
                ans += S[i]
            i += 1
        elif i == index:
            ans += T
            i += len(T)

    print(ans)
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:

def is_match(s1,s2):
  if len(s1)!=len(s2):
    return False
  count=0
  for i in range(len(s1)):
    if s1[i] == s2[i] or s1[i]=='?' or s2[i]=='?':
      count+=1
  if count==len(s1):
    return True
  else:
    return False        

rs = list(reversed(input()))
rt = list(reversed(input()))  
  
si = 0
while si <len(rs):  
  tmp = rs[si:si+len(rt)]
  
  if is_match(tmp,rt):  
    rs[si:si+len(rt)] = rt
    break
  else:
    si+=1

if si==len(rs):
  print('UNRESTORABLE')
else:
  print(''.join(rs).replace('?','a')[::-1])

Submission status: Accepted

Solution code:
s=input()
t=input()
a=[]
for i in range(len(s)-len(t)+1):
    f=True
    for j in range(len(t)):
        if s[i+j] != '?' and s[i+j] != t[j]:
            f=False
    if f == True:
        a.append(i)
s=s.replace('?','a')
if len(a) != 0:
    print(s[:a[-1]]+t+s[a[-1]+len(t):])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
Sprime = input() # S'の入力
T = input() # Tの入力
S = [] # Sの候補をlistで管理
if len(Sprime) < len(T): # S'の文字列長がTの文字列長よりも短い場合はそもそもSが存在しえない
    print("UNRESTORABLE")
    exit()
for i in range(len(Sprime) - len(T) + 1): # 左からTを入れられるか見ていく
    flag = 0 # 文字が一致しえないことのflagを初期化
    if Sprime[i] == T[0] or Sprime[i] == "?":
        for j in range(len(T)):
            if Sprime[i + j] != T[j] and Sprime[i + j] != "?": # 一致しえない場合
                flag += 1
                break
        if flag != 1:
            S.append(Sprime[0 : i].replace("?", "a") + T + Sprime[i + len(T) :].replace("?", "a")) # 辞書順最小を求めるので?は全てaにする
S.sort() # Sの候補をさらに辞書順に並べる
if S == []: # Sの候補が無い場合 "UNRESTORABLE" を出力
    print("UNRESTORABLE")
else: # Sの候補のうち辞書順最小のものを出力
    print(S[0])

Submission status: Accepted

Solution code:
#76c
Sdash = list(str(input()))
T = list(str(input()))

#文字列 S′ を見つけ、これは文字列 S の 0 個以上 |S| 個以内の文字が ? に置き換わった文字列であることも分かりました。

#条件1：文字列 S の中に連続する部分文字列として英小文字から成る文字列 Tが含まれている。
#条件2：S は、条件1を満たす文字列の中で辞書順最小の文字列である。

#そのとき、鍵となる文字列 Sを出力しなさい。
#ただし、そのような文字列 S が存在しない場合は代わりに UNRESTORABLE と出力しなさい。

#後ろから探る
presence_flag = 0

for i in range(len(Sdash)-len(T),-1,-1):
    pickuped = Sdash[i:i+len(T)]
    for j in range(len(T)):
        if T[j] != pickuped[j] and pickuped[j] != "?":
            break
    else:
        presence_flag = 1
        if Sdash[i:i+len(T)] != T: #文字列に?を含んでいる 
            Sdash[i:i+len(T)]=T
            break
        
if presence_flag == 1:
    for i in range(len(Sdash)):
        if Sdash[i]=="?":
            Sdash[i]="a"
    print(''.join(map(str, Sdash)))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=list(input())
t=list(input())
n=len(s)
m=len(t)
if m>n:
    print('UNRESTORABLE')
    exit()
af=0
for i in range(n-m+1):
    k=s[n-m-i:n-i]
    f=0
    for j in range(m):
        if k[j]=='?' or k[j]==t[j]:
            f+=1
    if f==m:
        af+=1
        s[n-m-i:n-i]=t
        break
if af>0:
    ans=''
    for i in range(n):
        if s[i]=='?':
            ans+='a'
        else:
            ans+=s[i]
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

for i in range(len(S)-len(T),-1,-1):
  flag = True
  j = 0
  while (j<len(T)):
    if S[i+j] == '?' or S[i+j] == T[j]:
      j += 1
    else:
      flag = False
      break
  if flag:
    S = list(S)
    for j in range(len(T)):
      S[i+j] = T[j]
    print(''.join(S).replace('?','a'))
    exit()
print('UNRESTORABLE')

Submission status: Accepted

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

ans=''
for i in range(len(S)-len(T)+1):
  for j in range(i,i+len(T)):
    if S[j]!=T[j-i] and S[j]!='?':
      break
  else:
    ans=S[:i]+T+S[i+len(T):]
if ans=='':
  print('UNRESTORABLE')
else:
  for i in range(len(ans)):
    if ans[i]=='?':
      ans[i]='a'
  print(*ans,sep='')

Submission status: Accepted

Solution code:
s=str(input())
t=str(input())
ans=0
for j in range(len(s)-len(t)+1):
    e=s[j:j+len(t)]
    #print(s[j:j+len(t)])
    for i in range(len(t)):
        if t[i]!=e[i] and e[i]!='?':
            break
    else:
        ans=s[:j]+t+s[j+len(t):]
if ans:
    for i in range(len(ans)):
        if ans[i]=='?':
            ans=ans[:i]+'a'+ans[i+1:]
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import copy
S = list(input())
T = list(input())
ans = "UNRESTORABLE"

for i in range(len(S)-len(T),-1,-1):
    flag = 1
    s = copy.deepcopy(S)
    x = S[i:i+len(T)]
    for j in range(len(x)):
        if x[j] == "?":
            s[i+j] = T[j]
        elif x[j] != T[j]:
            flag = 0
            break
    if flag:
        ans = "".join(s).replace("?", "a")
        break
print(ans)

Submission status: Accepted

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

res = ""
for i in range(len(S)-len(T),-1,-1):
    tmp = list(S)
    flag = True
    for j in range(i,i+len(T)):
        if S[j] != T[j-i] and S[j] != "?":
            flag = False 
    if flag:
        for j in range(i,i+len(T)):
            tmp[j] = T[j-i]
        for i in range(len(tmp)):
            if tmp[i] == "?":
                tmp[i] = "a"
        res = "".join(tmp)
        break
else:
    res = "UNRESTORABLE"

print(res)

Submission status: Accepted

Solution code:
import re

s = input()
t = input()

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

res = s.replace('?', 'a')

print(res)

Submission status: Accepted

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

t_len = len(T)
found = False
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:
        if i + t_len == len(S):
            S = S[:i] + T
        else:
            S = S[:i] + T + S[i+t_len:]
        found = True
    if found:
        print(S.replace('?','a'))
        break
else:
    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()
T = input()

for i in range(len(S_)-len(T), -1, -1):
    for j in range(len(T)):
        if S_[i+j] not in (T[j], '?'):
            break
    else:
        print(S_[:i].replace('?', 'a')+T+S_[i+len(T):].replace('?', 'a'))
        exit()
print('UNRESTORABLE')

Submission status: Accepted

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

if T in S:
    print(S.replace('?', 'a'))
    exit()

SL = len(S)
TL = len(T)

proposition = []
for i in range(SL-TL, -1, -1):
    S2 = S[i:i+TL]
    for j in range(TL):
        if S2[j] == T[j] or S2[j] == '?':
            continue
        else:
            break
    else:
        answer = S[:i] + T + S[i+TL:]
        proposition.append(answer.replace('?', 'a'))
if proposition:
    print(sorted(proposition)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

if T in S:
    print(S.replace('?', 'a'))
    exit()

SL = len(S)
TL = len(T)

for i in range(SL-TL, -1, -1):
    S2 = S[i:i+TL]
    for j in range(TL):
        if S2[j] == T[j] or S2[j] == '?':
            continue
        else:
            break
    else:
        answer = S[:i] + T + S[i+TL:]
        print(answer.replace('?', 'a'))
        exit()
print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s=input()
t=input()
u='UNRESTORABLE'
a=[]
if len(s)>=len(t):
  for i in range(len(s)-len(t)+1):
    f=0
    for j in range(i,i+len(t)):
      if s[j]!='?' and s[j]!=t[j-i]:
        f=1
    if f==0:
      m=s[:i]+t+s[i+len(t):]
      m=m.replace('?','a',50)
      a.append(m)
if len(a)==0:
  print(u)
else:
  a.sort()
  print(a[0])

Submission status: Accepted

Solution code:
s=input()
t=input()
strs=[]
S=len(s)
T=len(t)
for i in range(S-T+1):
  flag=0
  for j in range(T):
    if s[i+j]=="?":
      continue
    if s[i+j]!=t[j]:
      flag=1;break
  if flag==0:
    posstr=s[:i]+t+s[i+T:]
    strs.append(posstr.replace("?","a",50))
strs.sort()
if strs==[]:print("UNRESTORABLE")
else:print(strs[0])

Submission status: Accepted

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

L = []

if len(s) >= len(t):
  for i in range(len(s)-len(t)+1):
    if all([s[i+j]==t[j] or s[i+j]=='?' for j in range(len(t))]):
      l = str()
      for k in range(len(s)):
        if i <= k < i+len(t):
          l += t[k-i]
        elif s[k] == '?':
          l += 'a'
        else:
          l += s[k]
      L += [l]
      
if L:
  print(min(L))
else:
  print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()
T = input()
lenS=len(S)
lenT=len(T)
ans=["a"]*lenS
for i in range (lenS):
    if(S[i]!="?"):
        ans[i]=S[i]
flag=0
for j in range(lenS-lenT+1):
    flag=1
    for i in range (lenS):
        if(S[i]=="?"):
            ans[i]="a"

    for i in range(1,lenT+1):
        if((S[lenS-i-j]!="?")&(S[lenS-i-j]!=T[lenT-i])):
            flag=0
            break
        else:
            ans[lenS-i-j]=T[lenT-i]
    
    if(flag==1):
        break

if(flag==1):
    print("".join(ans))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
# C
sd = input()
scnt = len(sd)
t = input()
tcnt = len(t)

point = 0
unrestorable = True
while point <= scnt - tcnt:
    oneroopcorres = True
    for i in range(tcnt):
        anyone = sd[point+i] == '?'
        correspond = sd[point+i] == t[i]
        if not (anyone or correspond):
            oneroopcorres = False

    if oneroopcorres:
        unrestorable = False
        good_point = point

    point += 1

if unrestorable:
    print('UNRESTORABLE')
    exit()
else:
    sd = list(sd)
    for i in range(tcnt):
        sd[good_point + i] = t[i]
    sd = ''.join(sd)

sd = sd.replace('?', 'a')
print(sd)

Submission status: Accepted

Solution code:
SS = input()
T = input()
N = len(SS) - len(T)
key = []
key_t = []
for i in range(N,-1,-1):
  tmp = i
  cnt = 0
  cnt_t = 0
  for j in range(len(T)):
    if SS[tmp] != T[j] and SS[tmp] != "?":
      pass
    else:
      if SS[tmp] == T[j]:
        cnt += 1
        cnt_t += 1
      else:
        cnt += 1
    tmp += 1
  if cnt == len(T):
    key.append(i)
  if cnt_t == len(T):
    key_t.append(i)
SS = list(SS)
if len(key_t) >= 1:
  for i in SS:
    if i == "?":
      print("a",end="")
    else:
      print(i,end="")
elif len(key) >= 1:
    g = key[0]
    for i in range(len(T)):
      SS[g] = T[i]
      g += 1
    for s in SS:
      if s == "?":
        print("a",end="")
      else:
        print(s,end="")
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if T[j] != S[i+j] != '?':
            break
    else:
        break
else:
    print('UNRESTORABLE')
    exit()

ans = (S[:i] + T + S[i+len(T):])[::-1]
ans = ans.replace('?', 'a')
print(ans)

Submission status: Accepted

Solution code:
s = input()
t = input()
def check(A, B):
    for a, b in zip(A, B):
        if a != '?' and a != b:
            return False
    return True


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

Submission status: Accepted

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

Submission status: Accepted

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

LS=len(S)
LT=len(T)
ans=[]

for i in range(LS-LT+1):
    for j in range(LT):
        if not (T[j]==S[i+j] or S[i+j]=="?"):
            break
        if j==LT-1:
            ans.append(list(S[0:i]+T+S[i+LT:]))

Lans=len(ans)

if Lans==0:
    print("UNRESTORABLE")
    exit()


for i in range(Lans):
    for j in range(LS):
        if ans[i][j]=="?":
            ans[i][j]="a"
    ans[i]="".join(ans[i])

ans.sort()

print(ans[0])

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

SD = list(input().rstrip())
T = list(input().rstrip())

lenSD = len(SD)
lenT = len(T)

def match(i):
    s = SD[i:i+lenT]
    for i in range(len(s)):
        if s[i] == '?':
            continue
        if s[i] != T[i]:
            return False

    return True




match_i = -1
for i in range(lenSD):
    if (lenSD - lenT) < i:
        break

    if match(i):
        match_i = i

if match_i == -1:
    print('UNRESTORABLE')
    exit()

# 場所が決まった
i = match_i
for j in range(lenT):
    if SD[i+j] == '?':
        SD[i+j] = T[j]

for i in range(lenSD):
    if SD[i] == '?':
        SD[i] = 'a'

print(*SD, sep="")

Submission status: Accepted

Solution code:
import re


def main():
    s = input().replace("?", ".")
    t = input()
    ls = len(s)
    lt = len(t)
    for i in range(ls - lt, -1, -1):
        f = re.match(s[i: i + lt], t)
        if f != None:
            if i == ls - lt:
                x = s[:i]+t
            else:
                x = s[:i] + t + s[i + lt:]
            print(x.replace(".", "a"))
            return
    print("UNRESTORABLE")
    return


main()

Submission status: Accepted

Solution code:
s = input()
t = input()
def check(a, b):
  if a == '?':
    return True
  else:
    return a == b
  
cand = []
for i in range(len(s)-len(t)+1):
  flag = True
  c = list(s)
  for j in range(len(t)):
    if not check(s[i+j], t[j]):
      flag = False
      break
    c[i+j] = t[j]
  if flag:
    cs = ''.join(c)
    cand.append(cs.replace('?', 'a'))
if len(cand):
  cand.sort()
  print(cand[0])
else:
  print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=input()
t=input()
ns=len(s)
nt=len(t)
li=[]
for i in range(ns-nt+1):
    for j in range(nt):
        if s[i]==t[j] or s[i]=='?':
            i+=1
            continue
        else:
            break
    else:
        li.append(s[:i-nt]+t+s[i:])

li2=[]
for i in li:
    li2.append(i.replace('?','a'))
li2.sort()
print(li2[0] if li2 else 'UNRESTORABLE')

Submission status: Accepted

Solution code:
import copy

s = list(input())
t = list(input())
if len(s) < len(t):
    print("UNRESTORABLE")
    exit()

ans_list = []
for i in range(len(s) - len(t) + 1):
    tmps = s[i:i + len(t)]
    ok = True
    for j in range(len(tmps)):
        if t[j] == tmps[j] or tmps[j] == '?':
            pass
        else:
            ok = False
            break
    if ok == True:
        cp_s = copy.deepcopy(s)
        for k in range(len(t)):
            cp_s[k+i] = t[k]
        ans_list.append(cp_s)

if len(ans_list) == 0:
    print("UNRESTORABLE")
else:
    # ?をaに書き換え
    for i in range(len(ans_list)):
        for j in range(len(s)):
            if ans_list[i][j] == '?':
                ans_list[i][j] = 'a'
    ans_list.sort()
    for c in ans_list[0]:
        print(c, end="")
    print()

Submission status: Accepted

Solution code:
def resolve():
    S = input()
    T = input()
    can_insert = False

    for start in range(len(S)-len(T),-1,-1):
        can_insert = True
        for i,c in enumerate(T):
            if S[start+i] != c and S[start+i] != "?":
                can_insert = False
        if can_insert:
            list_s = list(S)
            list_s[start:start+len(T)] = T
            S = "".join(list_s)
            print(S.replace("?","a"))
            break
    if not can_insert:
        print("UNRESTORABLE")
resolve()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()
  t=S()

  for i in range(len(s)-len(t)+1)[::-1]:
    f=True
    for j in range(len(t)):
      # print(s[i+j],t[j])
      if s[i+j]!=t[j] and s[i+j]!='?':
        f=False
        break
    if f:
      return (s[:i]+t+s[i+len(t):]).replace('?','a')
  return 'UNRESTORABLE'

# main()
print(main())

Submission status: Accepted

Solution code:
S = input()
T = input()
ls = len(S)
lt = len(T)
a = ''
for i in range(ls):
    try:
        s = S[ls-i-lt:ls-i]
        for j in range(lt):
            if not (s[j] == T[j] or s[j] == '?'):
                break
        else:
            a += S[:ls-i-lt].replace('?', 'a') + T + S[ls-i:].replace('?', 'a')
            break
    except:
        pass
print(a if a else 'UNRESTORABLE')

Submission status: Accepted

Solution code:
s = input()
t = input()
chancenum = []

for i in range(0,len(s) - len(t)+1):
    if s[i] == t[0] or s[i] == "?":
        n = 1
        while n < len(t) and (s[i+n] == t[n] or s[i+n] == "?"):
            n += 1
        if n == len(t):
            chancenum.append(i)

if chancenum == []:
    print("UNRESTORABLE")
else:
    tright = max(chancenum)
    for i in range(0,len(t)):
        s = s[:tright] + t + s[tright+len(t):]
    print(s.replace('?', 'a'))

Submission status: Accepted

Solution code:
def main():
    s = "".join(reversed(list(input())))
    t = "".join(reversed(list(input())))
    anses = []

    for i in range(len(s)):
        for j in range(len(t)):
            if i+j < len(s) and (s[i+j] == t[j] or s[i+j] == '?'):
                continue
            else:
                break
        else:
            # 合っていた
            ans = (s[:i]+t+s[i+len(t):]).replace('?', 'a')
            anses.append("".join(reversed(list(ans))))
            continue
    anses.sort()
    print(anses[0] if anses else "UNRESTORABLE")

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s = input()
    t = input()
    if t in s:
        print(s.replace('?', 'a'))
        return
    s = "".join(reversed(list(s)))
    t = "".join(reversed(list(t)))
    anses = []

    for i in range(len(s)):
        for j in range(len(t)):
            if i+j < len(s) and (s[i+j] == t[j] or s[i+j] == '?'):
                continue
            else:
                break
        else:
            # 合っていた
            ans = (s[:i]+t+s[i+len(t):]).replace('?', 'a')
            anses.append("".join(reversed(list(ans))))
            continue
    anses.sort()
    print(anses[0] if anses else "UNRESTORABLE")

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s = input()
    t = input()
    if t in s:
        print(s.replace('?', 'a'))
        return
    s = "".join(reversed(list(s)))
    t = "".join(reversed(list(t)))
    anses = []

    for i in range(len(s)):
        for j in range(len(t)):
            sw1, sw2 = False, False
            if i+j < len(s) and (s[i+j] == t[j] or s[i+j] == '?'):
                continue
            else:
                # '?' でもなく文字が合わなかった
                sw2 = True
                break
        else:
            # 合っていた
            ans = (s[:i]+t+s[i+len(t):]).replace('?', 'a')
            anses.append("".join(reversed(list(ans))))
            continue
    anses.sort()
    print(anses[0] if anses else "UNRESTORABLE")

if __name__ == '__main__':
    main()

Submission status: Accepted

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


li=[]
for i in range(len(s)-1,len(t)-2,-1):
    if s[i]==t[-1] or s[i]=='?':
        j=-1
        while s[i]==t[j] or s[i]=='?':
            i-=1
            j-=1

            if -j>len(t):
                ss=s[:i+1]+t+s[i+len(t)+1:]
                li.append(ss.replace('?','a'))
                break
li.sort()

print(li[0] if li else 'UNRESTORABLE')

Submission status: Accepted

Solution code:
s, t = [s.strip() for s in open(0)]

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

Submission status: Accepted

Solution code:

S = input()
T = input()
N = len(S)
K = len(T)

ans = []
flag = False
for i in range(N-K+1):
    for j in range(K):
        if not(S[i+j] == T[j] or S[i+j] == '?'):
            break
    else:
        flag = True
        tmp = list(S)
        for j in range(K):
            tmp[i+j] = T[j]
        for j in range(N):
            if tmp[j] == '?': tmp[j] = 'a'
        tmp = ''.join(tmp)
        ans.append(tmp)
        
if flag:
    ans.sort()
    print(ans[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    s=input().replace('?','.')
    t=input()
    import re
    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):])
            return
    print("UNRESTORABLE")
resolve()

Submission status: Accepted

Solution code:
s = input()[::-1]
t = input()[::-1]
for i in range(len(s)-len(t)+1):
    for j in range(len(t)):
        if t[j] != s[i+j] != "?":break
    else: break
else:
    print("UNRESTORABLE")
    exit()
s = s[:i] + t + s[i+len(t):]
print(s.replace("?","a")[::-1])

Submission status: Accepted

Solution code:
ss=input()[::-1]
t=input()[::-1]
# ssの出来るだけ後ろに t を入れ、それ以外は "a" を入れる.

for i in range(len(ss)-len(t)+1):
    match_len=0
    for j in range(len(t)):
        if ss[i+j]=="?" or ss[i+j]==t[j]:
            match_len+=1
        else:
            break
        if match_len==len(t):
            ss=ss[:i]+t+ss[i+len(t):]
            ss=ss.replace("?","a")
            print(ss[::-1])
            exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
s = len(S)
t = len(T)
l = []
ans = []

if s < t:
    print('UNRESTORABLE')
    exit()

for i in range(s - t + 1):
    for j in range(t):
        if T[j] == S[i + j] or S[i + j] == '?':
            if j == t - 1:
                l.append(i)
            continue
        else:
            break

if l:
    for i in l:
        if i != 0 and i != s - 1:
            ans.append((S[:i] + T + S[i + t:]).replace('?', 'a'))
        elif i == 0:
            ans.append((T + S[t:]).replace('?', 'a'))
        else:
            ans.append((S[:i] + T).replace('?', 'a'))
    ans.sort()
    print(ans[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

sw=0
for i in range(-len(t),-len(s)-1,-1):
    ss=s[i:]
    cnt=0
    for j in range(len(t)):
        if ss[j]==t[j] or ss[j]=='?':
            cnt+=1
    if cnt==len(t):
        sw=1
        s=s[:i]+t+ss[len(t):]
        break
a=''.join(s)
aa=a.replace('?','a')
print(aa if sw==1 else 'UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

Sd = input()
T = input()
N = len(Sd)
M = len(T)
for i in range(N-M, -1, -1):
    t_ = Sd[i:i+M]
    for j in range(M + 1):
        if j == M:
            print((Sd[:i] + T + Sd[i+M:]).replace('?', 'a'))
            exit()
        if t_[j] == '?':
            continue
        elif t_[j] != T[j]:
            break
print('UNRESTORABLE')

Submission status: Accepted

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

switch=0
for i in range(-len(t),-len(s)-1,-1):
    ss=s[i:]
    cnt=0
    for j in range(len(t)):
        if ss[j]==t[j] or ss[j]=='?':
            cnt+=1
    if cnt==len(t):
        switch=1
        s=s[:i]+t+ss[len(t):]
        break
for k in range(len(s)):
    if s[k]=='?':
        s[k]='a'
if switch==1:
    print(''.join(s))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
# ABC076c
import sys
import re
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

ss = input()[:-1].replace('?', '.')
t = input()[:-1]

ans = []
for i in range(len(ss) - len(t) + 1):
    m = re.match(ss[i:i+len(t)], t)
    if (m != None):
        ans.append(ss[:i] + t + ss[i + len(t):])
if (len(ans) == 0):
    print('UNRESTORABLE')
    exit()
for i in range(len(ans)):
    ans[i] = ans[i].replace('.', 'a')
ans.sort()
print(ans[0])

Submission status: Accepted

Solution code:
s=input()
t=input()
l=len(s)
m=len(t)
L=[]
for i in range(l-m+1):
    if all([s[i+j]=='?' or s[i+j]==t[j] for j in range(m)]):
        L.append(i)
if L:
    k=max(L)
    print((s[:k]+t+s[k+m:]).replace('?','a'))
else:
    print('UNRESTORABLE')