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 = list(input())
S = [char if char != '?' else None for char in S]
T = list(input())

# print(S)
# print(T)
# Tを部分文字列として含み、尚且つその中でもっとも辞書順で小さいものを選ぶ。
# 1<=S<=50なので、Sを全列挙することはできない(24**50)
# Sのうち、どこをTの部分文字列にするかを考えると、50文字あったとしても問題ないのでは？

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

    is_fullfilled = True
    text = []
    for j, char in enumerate(T):
        if S[i + j] == char:
            text.append(char)
        elif S[i + j] is None:
            text.append(char)
        else:
            is_fullfilled = False
            break
    if is_fullfilled:

        text = ''.join(text)
        left = ''
        for char in S[:i]:
            if char is None:
                left += 'a'
            else:
                left += char
        right = ''
        for char in S[i + len(T):]:
            if char is None:
                right += 'a'
            else:
                right += char

        texts.append(left + text + right)
texts = sorted(texts)
if len(texts) == 0:
    print('UNRESTORABLE')
else:
    print(texts[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
S = input()
T = input()

candidates = []
for i in range(len(S) - len(T) + 1):
    is_ok = True
    for j in range(len(T)):
        if S[i+j] == '?' or S[i+j] == T[j]:
            pass
        else:
            is_ok = False

    # print(is_ok)
    if is_ok:
        candidate = S[:i] + T + S[i+len(T):]
        candidate = candidate.replace('?', 'a')
        candidates.append(candidate)

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

Submission status: Accepted

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

for i in range(len(s)-len(t), -1, -1):
    flag = True
    for j in range(len(t)):
        if not(s[i+j] == t[j] or s[i+j] == '?'):
            flag = False 
    if flag :
        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 = input()
T = input()
s,t = len(S),len(T)
for i in range(s-t,-1,-1):
    for j in range(t):
        if S[i+j] != T[j] and S[i+j] != "?":
            break
    else: 
        S = S[:i] + T + S[i+t:]
        break
else:
    print("UNRESTORABLE")
    exit()

S = list(S)
for i in range(s):
    if S[i] == "?":
        S[i] = "a"
print(*S, sep = "")

Submission status: Accepted

Solution code:
import copy
S = list(input())
T = list(input())
N = len(S)
M = len(T)
lst = []

for i in range(N-M+1):
    for j in range(M):
        if (T[j] == S[i+j]) or (S[i+j] == "?"):
            exist = True
        else:
            exist = False
            break
    if exist:
        ref = copy.copy(S)
        for k in range(M):
            ref[i+k] = T[k]
        s = "".join(ref)
        s = s.replace("?","a")
        lst.append(s)

lst = sorted(lst)

if lst == []:
    print("UNRESTORABLE")
else:
    print(lst[0])

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
S,T = input(),input()
N,M = len(S),len(T)
maximum = 0
maxstr = ""
for i in range(0,N-M+1):
    str = S[:i] + T + S[i+M:]

    n = 0
    for j in range(i,i+M):
        if (S[j] != str[j])&(S[j]!="?"):
            n = 1
            # print(S[j],str[j])
            break
    if n==1:continue

    match = 0
    for j in range(N):
        if (S[j] == str[j])|(S[j]=="?"):match += 1
    if maximum<=match:
        maximum = match
        maxstr = str
    # print(str)
# print(maximum)
if maximum == 0:print("UNRESTORABLE")
else:print(maxstr.replace("?","a"))

Submission status: Accepted

Solution code:
S = input()
T = input()
S_LEN = len(S)
T_LEN = len(T)
ans_li = []
if S_LEN - T_LEN < 0:
    print("UNRESTORABLE")
else:
    for i in range(S_LEN - T_LEN + 1):
        is_ok = True
        for j in range(T_LEN):
            if S[i + j] != "?" and S[i + j] != T[j]:
                is_ok = False
                break
        if is_ok:
            tmp = S[:i] + T + S[i + T_LEN:]
            ans_li.append(tmp.replace("?", "a"))
    print(sorted(ans_li)[0]) if ans_li != [] else print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S=input()
T=input()
l_s=len(S)
l_t=len(T)
ans=[]
i=0
while i+l_t<=l_s:
  j=0
  while j<l_t:
    if S[i+j]=="?" or S[i+j]==T[j]:
      j+=1
    else:
      break
  if j==l_t:
    A=S[:i]+T+S[i+l_t:]
    A=A.replace("?","a")
    ans.append(A)
  i+=1
print(sorted(ans)[0] if ans else "UNRESTORABLE")

Submission status: Accepted

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

ins = -1#挿入場所(できるだけうしろにして前はAでつめる)
for i in range(0,len(S)-len(T)+1,1):
    
    isOk = True
    for j in range(i,i+len(T),1):
        if not(T[j-i]==S[j] or S[j]=="?"):
            isOk = False
            break
        
    if isOk:
        ins = i

if ins == -1:
    print("UNRESTORABLE")
    exit()

ans = list(S)
for i in range(len(T)):
    ans[ins+i]=T[i]

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

print("".join(ans))

Submission status: Accepted

Solution code:
S = input()
N = len(S)
T = input()
M = len(T)

st = -1

for i in range(N-M, -1, -1):
  if all(S[i+j] in (T[j], "?") for j in range(M)):
    st = i
    break

newS = S.replace("?", "a")

if st == -1:
  print("UNRESTORABLE")
else:
  ans = ""
  for i in range(N):
    if st <= i <= st+M-1:
      ans += T[i-st]
    else:
      ans += newS[i]
  print(ans)

Submission status: Accepted

Solution code:
sd = input()
t = input()
ls = len(sd)
lt = len(t)
ans=[]
for i in range(ls-lt,-1,-1):
  for j, k in zip(sd[i:i+lt], t):
    if j != "?" and j != k:
      break
  else:
    temp = sd[:i] + t + sd[i+lt:]
    ans.append(temp.replace("?","a"))

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

Submission status: Accepted

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

def Ispossible(a,b):
  #print(a,b)
  if len(a) != len(b):
    return False
  flag = True
  for i in range(len(a)):
    if not(a[i]==b[i] or a[i]=="?"):
      flag = False
      break
  return flag

for i in range(1,len(S)-len(T)+2):
  q = S[-i:-i-len(T):-1].copy()
  r = T[-1:-len(T)-1:-1].copy()
  if Ispossible(q, r):
    for j in range(len(T)):
      S[-i-j] = T[-1-j]
      #print(S)
      
    for j in range(len(S)):
      if S[j] == "?":
        S[j] = "a"
    print("".join(S))
    sys.exit()
print("UNRESTORABLE")

Submission status: Accepted

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

pos = False
rep = list(range(len(s)-len(t)+1))
rep.reverse()
for i in rep:
    flag = True
    for j in range(len(t)):
        if s[i+j] == '?':
            pass
        elif s[i+j] == t[j]:
            pass
        else:
            flag = False

    if flag == False:
        continue
    for j in range(len(t)):
        s[i+j] = t[j]
    pos = True
    break

if pos == False:
    print('UNRESTORABLE')
else:
    for i in range(len(s)):
        if s[i] == '?':
            s[i] = 'a'
    print(''.join(s))

Submission status: Accepted

Solution code:
S=input()
T=input()
ns=len(S)
nt=len(T)
ans=[]
i=0
while i+nt<=ns:
    j=0
    while j<nt:
        if S[i+j]=='?' or S[i+j]==T[j]:
            j+=1
        else:
            break
    if j==nt:
        A=S[:i]+T+S[i+nt:]
        A=A.replace('?','a')
        ans.append(A)
    i+=1
    
if ans:
    print(sorted(ans)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s=list(input())
t=list(input())
ans='U'
for i in range(len(s)-len(t),-1,-1):
  if s[i]==t[0] or s[i]=='?':
    cnt=1
    for j in range(1,len(t)):
      if s[i+j]==t[j] or s[i+j]=='?':
        cnt+=1
    if cnt==len(t):
      s[i:i+len(t)]=t
      u=''
      for k in s:
        u+=k
      u=u.replace('?','a')
      print(u)
      exit()
      
print('UNRESTORABLE')

Submission status: Accepted

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

anss = []
for tpos in range(len(sd) - len(t) + 1):
    check = True
    for i in range(len(t)):
        if t[i] != sd[(i + tpos)] and sd[(i + tpos)] != '?':
            check = False
            break
    if check == True:
        cand = sd[:tpos] + t + sd[(tpos + len(t)):]
        anss.append(cand.replace('?', 'a'))

if len(anss) == 0:
    print('UNRESTORABLE')
else:
    print(sorted(anss)[0])

Submission status: Accepted

Solution code:
import sys
sp=input()
t=input()

m=len(sp)
n=len(t)
for i in range(m-n,-1,-1):
  cand=sp[:i].replace('?','a')+t+sp[i+n:].replace('?','a')
  for j,w in enumerate(t):
    if sp[j+i]!=w and sp[j+i]!='?':
      break
  else:
    print(cand)
    sys.exit()
print('UNRESTORABLE')

Submission status: Accepted

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

S_len = len(S)
T_len = len(T)

if T_len > S_len:
    print("UNRESTORABLE")
    sys.exit()

#条件１に関して
candidate = []
for i in range(0, S_len-T_len+1):

    isOkay = True
    for j in range(T_len):
        if S[i+j] == "?":
            continue

        if S[i+j] == T[j]:
            continue
        else:
            isOkay = False

    if isOkay:
        candidate.append((S[0:i] + T + S[i+T_len:]).replace("?", "a"))

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readline().strip()
    T = readline().strip()

    N, M = len(S), len(T)
    ans = []

    for i in range(N - M + 1):
        ok = True
        s = list(S)
        for j in range(M):
            if S[i + j] == '?':
                s[i + j] = T[j]
            elif S[i + j] != T[j]:
                ok = False
                break
        if ok:
            ans.append(''.join(s).replace('?', 'a'))

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

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readline().strip()
    T = readline().strip()

    N, M = len(S), len(T)
    ans = []

    for i in range(N - M + 1):
        ok = True
        s = list(S)
        for j in range(M):
            if S[i + j] == '?':
                s[i + j] = T[j]
            elif S[i + j] != T[j]:
                ok = False
                break
        if ok:
            ans.append(''.join(s).replace('?', 'a'))

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

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

lst=[]
for i in range(len(s)-len(t)+1):
  ss=s[i:i+len(t)]
  f=True
  for a,b in zip(ss,t):
    if a!=b and a!="?":
      f=False
      break
  if f:
    st=s[:i]+t+s[i+len(t):]
    lst.append(st.replace("?","a"))
if lst==[]:
  print("UNRESTORABLE")
else:
  lst.sort()
  print(lst[0])

Submission status: Accepted

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

ans = []
for i in range(n - m + 1):
    flag = True
    for j in range(m):
        if s[i+j] == t[j] or s[i+j] == '?':
            continue
        flag = False
    if flag:
        ans.append("".join(s[:i] + t + s[i+m:]).replace('?', 'a'))

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

Submission status: Accepted

Solution code:
S = input()[::-1]
T = input()[::-1]
ns, nt = len(S), len(T)
ans = None
for i in range(ns-nt+1):
    _S = S[i:i+nt]
    f = True
    for s,t in zip(_S,T):
        if not (s=='?' or s==t):
            f = False
            break
    if f:
        ans = S[:i].replace('?', 'a')+T+S[i+nt:].replace('?', 'a')
        break
print("UNRESTORABLE" if not ans else ans[::-1])

Submission status: Accepted

Solution code:
s,t=open(0)
m=len(t:=t[:-1])
for i in range(51):
 if all(c in'?'+d for c,d in zip(s[-i-m:],t)):s=s.replace(*'?a');exit(print(s[:-i-m]+t+s[-i:]))
print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s,t=open(0)
m=len(t:=t[:-1])
for i in range(len(s)):
 if all(c in'?'+d for c,d in zip(s[-i-m:],t)):s=s.replace(*'?a');exit(print(s[:-i-m]+t+s[-i:]))
print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s,t=input(),input();l,m=len(s),len(t)
for i in range(l-m+1):
 if all(c in"?"+d for c,d in zip(s[-i-m:],t)):s=s.replace(*"?a");exit(print(s[:-i-m]+t+s[l-i:]))
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import re
s,t=input().replace(*"?."),input()
l,m,*c=len(s),len(t)
for i in range(l-m+1):
 if re.match(s[i:i+m],t):c+=(s[:i]+t+s[i+m:]).replace(*".a"),
print(c and min(c)or"UNRESTORABLE")

Submission status: Accepted

Solution code:
import re
s, t = input().replace(*"?."), input()
l, m = len(s), len(t)

can = []
for i in range(l - m + 1):
    if re.match(s[i:i+m], t):
        can += (s[:i] + t + s[i+m:]).replace(*".a"),
print(min(can) if can else "UNRESTORABLE")

Submission status: Accepted

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

found = False
res = ""
len_S = len(S)
len_T = len(T)
found = False

for i in range(len_S):
    cnt = 0
    if i == len_S - len_T + 1: break
    for j in range(len_T):
        if T[j] == S[i+j] or S[i+j] == "?":
            cnt+=1
        if cnt == len_T:
            tmps = S[:i]
            tmpt = S[i+len_T:]
            res = S[:i] + T + S[i+len_T:]
            found = True
            break

if found: print(res.replace("?", "a"))
else: print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=list(list(input())[::-1])
t=list(list(input())[::-1])
s_len=len(s)
t_len=len(t)
for i in range(s_len-t_len+1):
    for j in range(t_len):
        if s[i+j]!="?" and t[j]!=s[i+j]:
            break
    else:
        s[i:i+t_len]=t 
        for k in range(s_len):
            if s[k]=="?":
                s[k]="a"
        print("".join(s[::-1]))
        exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
f = "UNRESTORABLE"

# 後ろから見ていって、部分文字列を当てはめる
S_r = S[::-1]
T_r = T[::-1]
for i in range(len(T),len(S)+1):
    S_ = S_r[i-len(T):i]
    flg = True
    for t,s in zip(T_r, S_):
        if s != "?" and s != t:
            flg = False
    if flg:
        ans = S_r[:i-len(T)] + T_r + S_r[i:]
        print(ans[::-1].replace("?","a"))
        exit()
print(f)

Submission status: Accepted

Solution code:
def resolve():
    Sd = list(input())
    T = input()
    res = ""
    for i in range(len(Sd)):
        for j in range(len(T)):
            if i + j >= len(Sd):
                break
            if Sd[i + j] != "?" and Sd[i + j] != T[j]:
                break
        else:
            tmp = "".join(Sd[:i]) + T + "".join(Sd[i + len(T):])
            if res == "":
                res = tmp
            else:
                if res > tmp:
                    res = tmp
    if res == "":
        print("UNRESTORABLE")
        return
    for s in res:
        if s == "?":
            s = "a"
        print(s, end="")
    print()

resolve()

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
T = input()

ls = len(S)
lt = len(T)
ans = []
for i in range(ls -lt, -1, -1):
    ss = list(S)
    ok = True
    for j in range(lt):
        if S[i + j] != '?' and S[i + j] != T[j]:
            ok = False
            break
        if S[i + j] == '?':
            ss[i + j] = T[j]
    if not ok:
        continue

    for j in range(ls):
        if ss[j] == '?':
            ss[j] = 'a'
    print("".join(ss))
    break
else:
    print("UNRESTORABLE")

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:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    S = input()
    T = input()
    s = len(S)
    t = len(T)
    if s < t:
        print("UNRESTORABLE")
        return
    for i in range(s - t, -1, -1):
        correspond = True
        for j in range(t):
            if S[i + j] != '?' and S[i + j] != T[j]:
                correspond = False
                break
        if correspond:
            ans = [S[k] for k in range(s)]
            for j in range(t): ans[i + j] = T[j]
            for k in range(s):
                if ans[k] == '?': ans[k] = 'a'
            print("".join(ans))
            return
    print("UNRESTORABLE")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
sd=[c for c in input()]
t=[c for c in input()]
tl=len(t)
for i in range(len(sd)-tl,-1,-1):
    s=sd.copy()
    m=sd[i:i+tl]
    for j in range(tl):
        if m[j]=='?':
            s[i+j]=t[j]
        elif m[j]==t[j]:
            pass
        else:
            break
    if s[i:i+tl]==t:
        print(''.join(['a' if c=='?' else c for c in s]))
        exit()
print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys


Sp = [c for c in input()]
T = [c for c in input()]

sn = len(Sp)
tn = len(T)

match_i = 51
for i in range(sn - tn + 1):
    partial = Sp[i:i+tn]
    for j in range(len(partial)):
        if partial[j] == '?':
            partial[j] = T[j]
    if partial == T:
        match_i = i

if match_i > 50:
    print('UNRESTORABLE')
    sys.exit()

Sp[match_i:match_i+tn] = T
for i in range(sn):
    if Sp[i] == '?':
        Sp[i] = 'a'
S = ''.join(Sp)

print(S)

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()
s = len(S)
T = input()
t = len(T)

key = 0
for i in range(s-t,-1,-1):
    for j,k in enumerate(S[i:i+t]):
        if k == T[j] or k =="?":
            pass
        else:
            break
    else:
        key = i
        break
    key = 100
if key == 100 or s<t:
    print("UNRESTORABLE")
else:
    ans = S[0:i]+T+S[i+t:]
    ans = ans.replace("?","a")
    print(ans)

Submission status: Accepted

Solution code:
import re

S = input().replace('?', '.')
T = input()

n = len(S)
m = len(T)
j = -1
for i in range(n-m, -1, -1):
    if re.match(S[i:i+m], T):
        j = i
        break

if j == -1:
    print('UNRESTORABLE')
else:
    ans = ''.join(['a' if t == '.' else t for t in S])
    ans = ans[:j] + T + ans[j+m:]
    print(ans)

Submission status: Accepted

Solution code:
import sys, math
from itertools import product
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]


def main():
    S = input()
    T = input()
    l = []
    N, M = len(S), len(T)
    for i in range(N-M+1):
        if all(T[j] == S[i+j] or S[i+j] == '?' for j in range(M)):
            w = S[:i]+T+S[i+M:]
            w = w.replace('?', 'a')
            l.append(w)
    if l:
        print(sorted(l)[0])
    else:
        print('UNRESTORABLE')


if __name__ == '__main__':
    main()

Submission status: Accepted

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


# S'よりTが大きかったら無理
if len(Sd) < len(T):
    print("UNRESTORABLE")
    exit()

head_list = []
# 部分文字列の頭のindex
for i in range(len(Sd) - len(T) + 1):
    sdd = Sd[i:i + len(T)]
    f = True
    for j, sddj in enumerate(sdd):
        if sddj == "?":
            continue
        elif sddj != T[j]:
            f = False
            break
    if f:
        head_list.append(i)

# 候補が見つからなかったら無理
if not head_list:
    print("UNRESTORABLE")
    exit()
else:
    ans_list = []
    for head in head_list:
        ans_list.append((Sd[:head] + T + Sd[head + len(T):]).replace("?", "a"))

    ans_list.sort()
    print(ans_list[0])

Submission status: Accepted

Solution code:
sd = input()
t = input()
oks = []
for i in range(len(sd)-len(t)+1):
    sdd = sd[i:i+len(t)]
    f = True
    for j,sddj in enumerate(sdd):
        if sddj == "?":
            continue
        elif sddj != t[j]:
            f = False
            break
    if f:
        oks.append(i)
if oks:
    k = oks[-1]
    print((sd[:k]+t+sd[k+len(t):]).replace("?","a"))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
sd = input()
t = input()
oks = []
if len(sd) < len(t):
    print("UNRESTORABLE")
else:
    for i in range(len(sd)-len(t)+1):
        sdd = sd[i:i+len(t)]
        f = True
        for j,sddj in enumerate(sdd):
            if sddj == "?":
                continue
            elif sddj != t[j]:
                f = False
                break
        if f:
            oks.append(i)
    if oks:
        k = oks[-1]
        print((sd[:k]+t+sd[k+len(t):]).replace("?","a"))
    else:
        print("UNRESTORABLE")

Submission status: Accepted

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

for i in range(s-t+1):
  if all(S[i+j]==T[j] or S[i+j]=="?" for j in range(t)):
    A.append(S[:i]+T+S[i+t:])

A = [a.replace("?","a") for a in A]

if len(A)==0:
  print("UNRESTORABLE")
else:
  print(min(A))

Submission status: Accepted

Solution code:
S = input()
T = input()
l = len(S)
m = len(T)

for n in range(l-m+1):
  if all(c in "?"+d for c,d in zip(S[-n-m:],T)):
    S = S.replace("?","a")
    print(S[:-n-m]+T+S[l-n:])
    exit()

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S=input()
T=input()
s=len(S)
t=len(T)
cand=[[] for _ in range(s-t+1)]
#Tの入る場所によって場合分け
for i in range(s-t+1):
  S_can=[S[k] for k in range(s)]
  for j in range(t):
    if S[i+j]!=T[j] and S[i+j]!='?':
      break
  else:
    for k in range(t):
      S_can[i+k]=T[k]
    for k in range(s):
      if S_can[k]=='?':
        S_can[k]='a'
    cand[i]=S_can
ans=[i for i in cand if i!=[]]
if ans==[]:
  print('UNRESTORABLE')
else:
  ans.sort()
  print(*ans[0],sep='')

Submission status: Accepted

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

lst = []
for i in range(len(s2)-len(t)+1):
    moji = ''
    if s2[i] == t[0] or s2[i] == '?':
        moji = s2[:i]+t[0]

        flg = 0
        for j in range(1,len(t)):
            if s2[i+j] == t[j] or s2[i+j] == '?':
                moji += t[j]
            else:
                flg = 1
                break
        if flg == 0:
            lst.append(moji+s2[i+len(t):])

if not lst:
    print('UNRESTORABLE')
else:
    print(min(lst).replace('?','a'))

Submission status: Accepted

Solution code:
S = input()
T = input()
s, t = len(S), len(T)
c = "z" * s
for i in range(s - t + 1):
    if all(S[i + j] in (T[j], "?") for j in range(t)):
        c = min(c, S[:i] + T + S[i + t:])
print("UNRESTORABLE" if c == "z" * s else c.replace("?", "a"))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
t = input()
n = len(t)
m = len(s)
c = "z" * m
for i in range(m - n + 1):
    if all(j in (t[k], "?") for k, j in enumerate(s[i:n + i])):
        c = min(c, s[:i] + t + s[i + n:])
print("UNRESTORABLE" if c == "z" * m else c.replace("?", "a"))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import re
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect

sys.setrecursionlimit(10**6)


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


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()[:-1]


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


def is_match(a, b):
    for _a, _b in zip(a, b):
        if (_a == _b or _a == '?'):
            continue
        else:
            return False
    return True


s = S()
t = S()

# s = s.replace('?', '.')
# pattern =   ''

if (t in s):
    print(s.replace('?', 'a'))
    exit()
if (len(s) < len(t)):
    print('UNRESTORABLE')
    exit()


if (is_match(s[-len(t):], t)):
    ans = s[:-len(t)] + t
    ans = ans.replace('?', 'a')
    print(ans)
    exit()
i = 0

for _s in reversed(s[len(t)-1:-1]):
    i += 1
    if (_s == t[-1] or _s == '?'):
        if (is_match(s[-i - len(t): - i], t)):
            ans = s[:-i - len(t)] + t + s[-i:]
            ans = ans.replace('?', 'a')
            print(ans)
            exit()


print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()
T = input()
s, t = len(S), len(T)
L = []
for i in range(s-t+1):
  if all(S[i+j]==T[j] or S[i+j]=="?" for j in range(t)):
    L.append(S[:i]+T+S[i+t:])
L = [l.replace("?", "a") for l in L]
print(min(L) if len(L)>0 else "UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
#区間を決める
for i in range(len(S)-len(T),-1,-1):
    S_ = S[i:i+len(T)]
    frag = 1
    for j in range(len(T)):
        if S_[j] == T[j] or S_[j] == '?' :
            #print(S_[j],T[j])
            if not j == len(T)-1:
                continue
        else:
            break
            frag = 0
        if frag == 1:
            #print(S_)
            #print(S[:i] ,T ,S[len(T)+2:])
            ans = S[0:i] + T + S[len(T)+i:]
            ans = ans.replace('?','a')
            print(ans)
            exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
s = len(S)
t = len(T)
for i in range(s - t, -1, -1):
    SS = S[i : i + t]
    for j in range(t):
        if SS[j] != '?' and SS[j] != T[j]:
            break
    else:   # 見つかった
        ans = S.replace('?', 'a')
        ans = ans[0 : i] + T + ans[i + t :]
        break
else:   # なかった
    ans = 'UNRESTORABLE'

print(ans)

Submission status: Accepted

Solution code:
S = input()
T = input()
sl = len(S)
tl = len(T)
ans = "z" * 100
for i in range(sl-tl+1):
    if S[i] == T[0] or S[i] == '?':
        bf = True
        for j in range(tl):
            if S[i+j] != T[j] and S[i+j] != '?':
                bf = False
                break
        if bf:
            new = S[:i] + T + S[i+tl:]
            new = new.replace("?", "a")
            ans = min(ans, new)
if ans == "z" * 100:
    print("UNRESTORABLE")
else:
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[29]:


Sdash = input()
T = input()


# In[30]:


length_S = len(Sdash)
length_T = len(T)
if length_S >= length_T:
    for i in range(length_S-length_T+1):
        for j in range(length_T):
            if Sdash[j+length_S-length_T-i] != T[j] and Sdash[j+length_S-length_T-i] != "?":
                break
        else:
            ans = Sdash[:length_S-length_T-i]+T+Sdash[length_S-i:]
            ans = [x if x != "?" else "a" for x in ans]
            print("".join(ans))
            break
    else:
        print("UNRESTORABLE")
else:
    print("UNRESTORABLE")


# In[ ]:

Submission status: Accepted

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

s = len(S)
t = len(T)

import re

if t > s:
	print("UNRESTORABLE")
	exit()

pos = -1
wani = 0

for i in range (0, s-t+1):
	wani = 0
	for j in range (0,t):
		if	S[i+j] != T[j] and S[i+j] !='?':
			wani = 1
	if wani == 0:
		pos = i        
        
if pos == -1:
	print("UNRESTORABLE")
	exit()

A = []
for i in range (0, s):
	A.append(S[i])

for i in range (0, t):
	A[pos+i] = T[i]

for i in range (0, s):
	if A[i] == '?':
		A[i] = 'a'

print(*A, sep='')

Submission status: Accepted

Solution code:
def main():
    s = input()
    t = input()
    lis = []
    for i in range(len(s)-len(t)+1):
        f=True
        for j in range(len(t)):
            if(s[i+j]==t[j] or s[i+j]=="?"):
                continue
            else:
                f=False
                break
        if(f):
            tmp = s[0:i]+t+s[i+len(t):]
            res = ""
            for k in range(len(tmp)):
                if(tmp[k]=="?"):
                    res += "a"
                else:
                    res+=tmp[k]
            lis.append(res)
    if(len(lis)==0):
        print("UNRESTORABLE")
    else:
        print(sorted(lis)[0])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import re
Sp = input().replace('?','.')
T = input()
flag = 0
for i in range(len(Sp)-len(T),-1,-1):
    if re.match(Sp[i:i+len(T)],T):
        Sp = Sp[:i]+T+Sp[i+len(T):]
        flag = 1
        break
if flag == 1:
    print(Sp.replace('.','a'))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = list(input())
T = list(input())
s = len(S)
t = len(T)
X = False
k = -1
for i in range(s - t + 1):
    OK = True
    for j in range(t):
        if S[i + j] != '?' and S[i + j] != T[j]:
            OK = False
            break
    if OK:
        k = i
if k != -1:
    for j in range(t):
        S[k + j] = T[j]
    X = True
if not X:
    print('UNRESTORABLE')
else:
    for i in range(s):
        if S[i] == '?':
            S[i] = 'a'
        print(S[i], end='')
    print('')