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 = input()
t = input()
ls = len(s)
lt = len(t)

a = []
for i in range(ls-lt+1):

    f=True
    sa = list(s.replace('?', 'a'))
    for j in range(lt):
        sa[i+j] = t[j]
        if s[i+j]!='?' and s[i+j]!=t[j]:
            f=False
            break
    if f:
        a.append(''.join(sa))

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

Submission status: Accepted

Solution code:
S = input()
T = input()
count = 0
flg = False
if len(S) < len(T):
    print('UNRESTORABLE')
    exit()
while True:
    flg = False
    for i in range(1,len(T)+1):
        if S[-i-count] != T[-i] and S[-i-count] != '?':
            count += 1
            flg = True
            break
    if count == len(S) - len(T) + 1:
        print('UNRESTORABLE')
        exit()
    if not flg:
        if count == 0:
            ans = S[0:len(S)-len(T)] + T
        elif count == len(S)-len(T):
            ans = T + S[len(T):]
        else:
            ans = S[0:len(S)-len(T)-count] + T + S[-count:]
        print(ans.replace('?','a'))
        exit()

Submission status: Accepted

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

ANS = False
save_left = 0
save_right = 0
for i in range(len(S) - len(T) + 1):
    count = 0
    left = i
    right = i + len(T)

    for sv, tv in zip(S[left:right], T):
        if sv != tv and sv != '?':
            break
        count += 1

    if count == len(T):
        save_left = left
        save_right = right
        ANS = True
     
if ANS:
    S[save_left:save_right] = T
    for w in S:
        if w == '?':
            print('a', end='')
        else: 
            print(w, end='')
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()
T = input()
ans = "AAAA"
flag = 1
str_list = []
for i in range(len(S)-len(T)+1):
    # print(S[i:i+len(T)],T)
    cur_s = ""
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == "?":
            cur_s += T[j]
    if cur_s == T:
        flag = 0
        cur_s = S[:i]+T+S[i+len(T):]
        str_list.append(cur_s)

if flag:
    print('UNRESTORABLE')
    exit()

print(min(str_list).replace("?","a"))

Submission status: Accepted

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

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

for i in range(n-m,-1,-1):
    t_kamo=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_kamo[j]=="?":
            continue
        elif t_kamo[j]!=t[j]:
            break
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
# ABC076C
S = input()
T = input()
check = -1
for i in range(len(S) - len(T) + 1):
    if S[i] == "?" or S[i] == T[0]:
        k = 1
        flag = True
        while k < len(T):
            if S[i+k] == "?":
                k += 1
            else:
                if S[i + k] == T[k]:
                    k += 1
                else:
                    flag = False
                    break
        if k == len(T) and flag == True:
            check = i

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

Submission status: Accepted

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

Sa = []

if m >= n:
    for i in range(m+1-n):
        for j in range(n):
            if S[i+j] != T[j] and S[i+j] != "?":
                break
        else:
            s = S[:i] + T + S[i+n:]
            Sa.append(s.replace("?", "a"))
            

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

Submission status: Accepted

Solution code:
from collections import deque
s = input()
t = input()
ans = 'UNRESTORABLE'

def match_str(s, t):
    for i, c in enumerate(s):
        if t[i] != c and c != '?':
            return False
    return True

t_len = len(t)
end = len(s) - len(t) +1

# コーナーケース大丈夫？
for i in reversed(range(end)):
#     print(i, s[i : i+t_len])
    if match_str(s[i : i+t_len], t):
        ans = s[:i].replace('?', 'a') + t +s[i+t_len:].replace('?', 'a')
        break
        
print(ans)

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    s = str(readline().rstrip().decode('utf-8'))
    t = str(readline().rstrip().decode('utf-8'))
    lt = len(t)
    for i in range(len(s) - lt, -1, -1):
        ts = s[i:i+lt]
        b = True
        for j in range(len(ts)):
            if ts[j] != "?" and ts[j] != t[j]:
                b = False
                break
        if b:
            ans = []
            for j in range(len(s)):
                if i <= j < i + lt:
                    ans.append(t[j-i])
                else:
                    if s[j] == "?":
                        ans.append("a")
                    else:
                        ans.append(s[j])
            print("".join(ans))
            exit()
    print("UNRESTORABLE")


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

flg=False

if s_len >= t_len:
    for head in range(len(S)-t_len,-1,-1):
        flg=True
        for s,t in zip(S[head:head+t_len],T):
            if not(s==t or "?"==s):
                flg=False
        if flg:
            break


if flg:
    S=list(S)
    S[head:head+t_len] = T
    S="".join(S)
    S=S.replace("?","a")
    print(S)
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def MAP(): return [int(s) for s in input().split()]

S=input().rstrip()
T=input().rstrip()

ls=len(S)
lt=len(T)

cand=[]

for i in range(ls-lt+1):
	#print(S[i:i+lt])
	for j in range(lt):
		#print(S[i+j], T[j])
		if S[i+j]!=T[j] and S[i+j]!='?':
			break
	else:
		candidate=S[:i]+T+S[i+lt:]
		cand.append(candidate.replace('?','a'))

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
def MAP(): return [int(s) for s in input().split()]

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline

S = input().rstrip()
T = input().rstrip()

ans = [s for s in S]
end = len(S)

if S.rfind('?'*len(T)) != -1:
	j = S.rfind('?'*len(T))
	for i in range(len(T)):
		ans[i+j] = T[i]
	print(''.join(ans).replace('?','a'))
	sys.exit()
else:
	for x in range(len(T)-1, -1, -1):
		while S.rfind(T[x],0,end) != -1:
			for i in range(len(T)-1, -1, -1):
				if S.rfind(T[i],0,end) != -1 and S.rfind(T[i],0,end)+len(T)-i<=len(S):
					j = S.rfind(T[i],0,end)
					flag = True
					for k in range(len(T)):
						#print(S[j+k-i], T[k])
						if S[j+k-i] == T[k] or S[j+k-i] == '?':
							continue
						else:
							flag = False
							break
					else:
						if flag:
							for k in range(len(T)): ans[j+k-i] = T[k]
							print(''.join(ans).replace('?','a'))
							sys.exit()
			end -= 1
	print('UNRESTORABLE')

Submission status: Accepted

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

    # K個ずれたところ始めてマッチするかどうか
    def match(k):
        ans = True
        for i in range(len(T)):
            if S[k+i] != T[i] and S[k+i] != '?':
                ans = False
            # print(i, S[k+i], T[i], ans)
        return ans

    def construct(k):
        s = list(S)
        for i in range(len(T)):
            s[k+i] = T[i]
        return "".join(s)


    back = None
    for i in range(len(S) - len(T) + 1):
        if match(i):
            back = i

    if back is None:
        print('UNRESTORABLE')
    else:
        s = construct(back)
        s = s.replace('?', 'a')
        print(s)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a=input()
b=input()
c=len(a)
d=len(b)

for i in range(c-d+1):
  e=a[c-d-i:c-i]
  for j in range(d+1):
    if j==d:
      print((a[:c-d-i]+b+a[c-i:]).replace("?", "a"))
      exit()
    if e[j]=='?':
      continue
    elif e[j]!=b[j]:
      break
      
print("UNRESTORABLE")

Submission status: Accepted

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

tmp_i=0
tmp_j=0
ans_lst=[]
if len(s)<len(t):
    ans='UNRESTORABLE'
else:
    for i in range(len(s)-len(t)+1):
        cnt=0
        for j in range(len(t)):
            if s[i+j]==t[j] or s[i+j]=='?':
                cnt+=1
            if cnt==len(t):
                ans_s=s[:i]+t+s[i+len(t):]
                ans_lst.append(ans_s.replace('?','a'))
    else:
        ans='UNRESTORABLE'
ans_lst=sorted(ans_lst)
if len(ans_lst)==0:
    print(ans)
else:
    print(ans_lst[0])

Submission status: Accepted

Solution code:
def main():

    S = input()
    T = input()
    n = len(S)
    m = len(T)

    cand = []
    for i in range(n-m+1):
        flag = True
        for j in range(i, i+m):
            if S[j] != T[j-i] and S[j] != '?':
                flag = False
                break
        if flag:
            cand.append(S[:i] + T + S[i+m:])

    if not cand: return "UNRESTORABLE"
    ans = ""
    for U in cand:
        V = ""
        for c in U:
            if c == '?': V += "a"
            else: V += c
        if ans == "" or ans > V:
            ans = V
    return V

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
S_d,T = list(input()),list(input())

if len(S_d) < len(T):
  print("UNRESTORABLE");exit()

flag = 1
for i in range(-len(T),-len(S_d)-1,-1):
  key = 0
  for j in range(len(T)):
    if S_d[i+j]=="?" or S_d[i+j]==T[j]:
      key+=1
    else:
      break
  if key == len(T):
    st=len(S_d)+i
    break
  else:
    st=-1

if st==-1:
  print("UNRESTORABLE")
else:
  for i in range(len(S_d)):
    if st <= i and i < st+len(T):
      print(T[i-st],end="")
    elif S_d[i]=="?":
      print("a",end="")
    else:
      print(S_d[i],end="")

Submission status: Accepted

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

    SR = S[::-1]
    TR = T[::-1]

    restore = False
    pos = 0
    r = ""
    for i in range(len(S)):
        r += SR[:i]
        for j, si in enumerate(SR[i:]):
            if si == '?':
                r += TR[pos]
                pos += 1
            elif si == TR[pos]:
                r += TR[pos]
                pos += 1
            else:
                break
            # print(i, si, pos)
            if pos == len(T):
                restore = True
                break
        if restore:
            r += SR[i+j+1:]
            break
        pos = 0
        r = ""

    if restore:
        r = r.replace('?', 'a')
        r = r[::-1]
        print(r)
    else:
        print('UNRESTORABLE')


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    def check(i):
        for j in range(tn):
            if s[i + j] != "?" and s[i + j] != t[j]:
                return False
        return True

    s = list(input())
    t = list(input())
    sn = len(s)
    tn = len(t)
    for i in range(sn - tn, -1, -1):
        if check(i):
            s = s[:i] + t + s[i + tn:]
            break
    else:
        print("UNRESTORABLE")
        exit()
    for i in range(sn):
        if s[i] == "?": s[i] = "a"
    print(*s, sep="")

main()

Submission status: Accepted

Solution code:
s, t = list(input()[::-1]), list(input()[::-1])
for i in range(len(s) - len(t) + 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]
        break
else: print("UNRESTORABLE");exit()
print("".join(s).replace("?", "a")[::-1])

Submission status: Accepted

Solution code:
import sys
Sd = input()
T = input()
candidate = []
ans = ""

for i in range(len(Sd) - len(T) + 1):
    flag = True
    for j in range(len(T)):
        if Sd[i+j] != T[j] and Sd[i+j] != "?":
            flag = False
    if flag:
        candidate.append(Sd[:i] + T + Sd[i+len(T):])

if not candidate:
    print("UNRESTORABLE")
    sys.exit(0)

for i in candidate:
    s = ""
    for j in i:
        if j =="?":
            s += "a"
        else:
            s += j
    if ans =="":
        ans = s
    else:
        ans = min(ans, s)
print(ans)

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    s = str(readline().rstrip().decode('utf-8'))
    t = str(readline().rstrip().decode('utf-8'))
    k = len(t)
    for i in range(len(s) - k, -1, -1):
        ts = s[i:i+k]
        b = True
        for j in range(k):
            if ts[j] != "?" and ts[j] != t[j]:
                b = False
                break
        if b:
            a = []
            f = True
            for j in range(len(s)):
                if j < i or i + k <= j:
                    if s[j] == "?":
                        a.append("a")
                    else:
                        a.append(s[j])
                else:
                    if f:
                        a = a + list(t)
                        f = False
            print("".join(a))
            exit()
    print("UNRESTORABLE")


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
Sd = input()
T = input()
candidate = []
ans = ""

for i in range(len(Sd) - len(T) + 1):
    flag = True
    for j in range(len(T)):
        if Sd[i+j] != T[j] and Sd[i+j] != "?":
            flag = False
    if flag:
        candidate.append(Sd[:i] + T + Sd[i+len(T):])

if not candidate:
    print("UNRESTORABLE")
    sys.exit(0)

for i in candidate:
    s = ""
    for j in i:
        if j =="?":
            s += "a"
        else:
            s += j
    if ans =="":
        ans = s
    else:
        ans = min(ans, s)
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc076/tasks/abc076_c
S = input()
T = input()

"""
アルゴリズム
Ｓの文字を一つずつずらしていって、Ｔが代入できるかどうかみる。
代入できるものなら代入してそのなかで辞書最小の文字列を保存する
全インデックスを試してみてできた文字列のなかで最小のものを出力
配列がなければＵＮ・・・を出力
"""
min_list = []

for i in range(len(S) - len(T) + 1):
    cut_s = S[i:i+len(T)]
    flg = True
    for s in range(len(cut_s)):
        if cut_s[s] == "?":
            continue
        if cut_s[s] == T[s]:
            continue
        else:
            flg = False
            break

    if flg:
        ano_s = S[:i] + T + S[i+len(T):]
        ano_s = ano_s.replace("?","a")
        min_list.append(ano_s)

# min_listの中で辞書順に最小のものを出力
if len(min_list) > 0:
    min_list.sort()
    print(min_list[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

sl=len(s)
tl=len(t)

for i in range(sl-tl+1):
  #print(i,"番目の固定の検査を開始")
  first=sl-tl-i
  flag="True"
  for j in range(tl): #固定されたTのそれぞれの文字に対して検査をする
    if s[first+j]!="?": #Tのj文字目に該当するsの文字が?でない場合、
      if s[first+j]!=t[j]:
        flag="False" #一つでも一致しない場合は外側のfor文を次のループへ。
        break
  if flag=="False":
    #print("この番目ではtはむり。")
    continue
  else: #全てのTの文字に対して、この番目でありえるならそれが答え。
    #print("この番目")
    s=list(s)
    s[first:first+tl]=t
    for j,k in enumerate(s):
      if k=="?":
        s[j]="a"
    print("".join(s))
    exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
from sys import stdin
import math
import bisect
import heapq
import numpy as np
from math import factorial

s = stdin.readline().rstrip()
t = stdin.readline().rstrip()

atama = t[0]
lent = len(t)
lens = len(s)

lin = []

for i in range(lens-lent+1):
    flag = 0
    for j in range(lent):
        if s[i+j] != t[j] and s[i+j] != "?":
            flag = 1
    if flag == 0:
        lin.append((s[:i]+t+s[i+lent:]).replace("?","a"))
if lin == []:
    print("UNRESTORABLE")
    exit()

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

Submission status: Accepted

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

len_t = len(t)
ans = []
for i in range(len(s)-len(t)+1):
  flag = True
  for j in range(i, i+len_t):
    if s[j] != '?' and s[j] != t[j-i]:
      flag = False

  if flag:
    tmp = s[:i] + t + s[i+len_t:]
    for k in range(len(tmp)):
      if tmp[k] == '?':
        tmp[k] = 'a'
    ans += [''.join(tmp)]
      
if len(ans) == 0:
  print('UNRESTORABLE')
  exit()
ans = sorted(ans)
print(ans[0])

Submission status: Accepted

Solution code:
s = input()
hint = input()
s_l = len(s)
h_l = len(hint)
not_q_ind = [i for i in range(s_l) if s[i] != "?"]
t_li = [ "?"*i + hint + "?"*(s_l-h_l - i) for i in range(s_l-h_l+1)]
t_li_l = len(t_li)

new_t_li = []
for i in range(t_li_l):
  f = 0
  for ind in not_q_ind:
    if (t_li[i][ind] == "?") or (t_li[i][ind] == s[ind]):
      t_li[i] = t_li[i][:ind] + s[ind] + t_li[i][ind+1:]
    else:
      f = 1
  if f == 0:
    new_t_li.append(t_li[i])
    
length = len(new_t_li)
if length == 0:
  print("UNRESTORABLE")
else:
  for i in range(length):
    new_t_li[i] = new_t_li[i].replace("?", "a")
  print(sorted(new_t_li)[0])

Submission status: Accepted

Solution code:
import copy

S = list(input())
T = list(input())

ans = 'UNRESTORABLE'
len_s = len(S)
len_t = len(T)

is_matched = False

for i in range(len_s - len_t + 1):
    s = copy.deepcopy(S)
    for j in range(len_t):
        sc = s[(-1) - i - j]
        tc = T[(-1) - j]
        if sc == tc or sc == '?':
            s[(-1) - i - j] = tc
            if len_t == (j + 1):
                is_matched = True
        else:
            break
    if  is_matched:
        ans = ''.join(s).replace('?', 'a')
        break
print(ans)

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]=="?" or s[i+j]==t[j]:
            continue
        else:
            break
    else:
        s = s[:i] + t + s[i+len(t):]
        for k in range(len(s)):
            if s[k]=="?":
                s = s[:k] + "a" + s[k+1:]
        print(s)
        exit()

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import heapq as h
import math
import numpy as np
import copy
import itertools

 
# x,y = map(int,input().split())
s = input()
t = input()

candidates = []
count = 0
for i in range(len(s)):
    if ((s[i] == t[0]) or (s[i] == '?')) and ((len(s)-i) >= len(t)):
        count = 1
        for j in range(1,len(t)):
            if (s[i+j] == t[j]) or (s[i+j] == '?'):
                count += 1
            else:
                break
        if count == len(t):
            new = s[:i] + t
            if i+len(t) < len(s):
                new += s[i+len(t):]
            candidates.append(new.replace('?','a'))
if candidates != []:
    print(sorted(candidates)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s=input()
t=input()
found=False
st=0
for i in range(len(s)-len(t)+1):
  for j in range(len(t)):
    if s[i+j]!=t[j] and s[i+j]!="?":
      break
  else:
    found=True
    st=i

if not found:
  print("UNRESTORABLE")
else:
  ans=s[:st]+t+s[st+len(t):]
  print(ans.replace("?","a"))

Submission status: Accepted

Solution code:
s = list(input())
t = list(input())
ans = "UNRESTORABLE"
len_s = len(s)
len_t = len(t)
for i in range(len_s-len_t+1):
  j = len_s-len_t-i
  for S,T in zip(s[j:j+len_s],t):
    if S != T and S != "?":
      break
  else:
    ans = ""
    for k,TT in enumerate(t):
      s[j+k] = TT
    break
if ans == "UNRESTORABLE":
  print(ans)
else:
  for i in s:
    if i == "?":
      ans += "a"
    else:
      ans += i
  print(ans)

Submission status: Accepted

Solution code:
def examC():
    St = S(); T = S()
    N = len(St) - len(T)
    ans = -1
    for i in range(N,-1,-1):
        cur = 0
        for j in range(len(T)):
            if St[i+j]=="?" or St[i+j]==T[j]:
                cur +=1
                continue
            else:
                break
        if cur==len(T):
            ans =i
            break
    if ans == -1:
        ans="UNRESTORABLE"
    else:
        newS = ""
        for i in range(ans):
            if St[i]=="?":
                newS +="a"
            else:
                newS +=St[i]
        for i in range(ans,ans+len(T)):
            newS += T[i-ans]
        for i in range(ans+len(T),len(St)):
            if St[i]=="?":
                newS +="a"
            else:
                newS +=St[i]
        ans = newS
    print(ans)


import sys,copy,bisect,itertools
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Accepted

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

ans = "z" * 100
for i in range(len(S) - len(T) + 1):
    s = list(S)
    for j in range(len(T)):
        if S[i+j] != "?" and S[i+j] != T[j]:
            break
        s[i+j] = T[j]
    else:
        s = "".join(s).replace("?", "a")
        if s < ans:
            ans = s
if ans == "z" * 100:
    print("UNRESTORABLE")
else:
    print(ans)

Submission status: Accepted

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

for i in range(len(s)-len(T)+1):
    F=0
    for j in range(len(T)):
        if s[len(s)-1-i-j]==T[len(T)-j-1] or s[len(s)-1-i-j]=="?":
            continue
        else:
            F=1
            break
    if F==0:
        for j in range(len(T)):
            if s[len(s)-1-i-j]=="?":
                s[len(s)-1-i-j]=T[len(T)-j-1]
        
        for j in range(len(s)):
            if s[j]=="?":
                s[j]="a"
        print("".join(s))
        exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import copy
import sys
S = input()
T = input()
if len(T) > len(S):
    print("UNRESTORABLE")
    sys.exit()
I = []
S1 = []
for k in range(len(S)):
    if S[k] == '?':
        S1.append("a")
    else:
        S1.append(S[k])
for i in range(len(S)-len(T) + 1):
    S_ = copy.deepcopy(S1)
    flag = 1
    for j in range(i, i + len(T)):
        if S[j] != T[j-i] and S[j] != "?":
            flag = 0
            break
        else:
            S_[j] = T[j-i]     
    if flag:
        temp = ''.join(S_)
        I.append(temp)
S_ = S_.sort()
if len(I) > 0:
    print(I[-1])
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S=input()
T=input()
result = False
j=0
S=''.join(list(reversed(S)))
T=''.join(list(reversed(T)))

for i in range(len(S) - len(T) + 1):
    tmp_s=S
    if(S[i] == T[j] or S[i] == '?'):
        w=0
        for n in range(len(T)):
            if(S[i+n] == T[j+n] or S[i+n] == '?'):
                tmp_s = tmp_s[:i+n] + T[j+n] + tmp_s[i+n+1:]
                w+=1
            else:
                break
        if(w == len(T)):
            result = True
    if(result == True):
        break
    j=0
if(result == True):
    tmp_s=''.join(list(reversed(tmp_s)))
    print(tmp_s.replace('?','a'))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
def check(ii):
    j=0
    for i in range(ii,ii+len(t)):
        if s[i]!="?" and s[i]!=t[j]:
            return False
        j+=1
    return True

s=input()
t=input()
flag=False
for i in range(len(s)-len(t),-1,-1):
    if check(i):
        res=s[:i]+t+s[i+len(t):]
        print(res.replace("?","a"))
        flag=True
        break
if not flag:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
def agree(s1, t1):
    for i in range(len(t1)):
        if not(s1[i] == t1[i] or s1[i] =='?'):
            return False
    return True

s = input()
t = input()  
ls, lt  = len(s), len(t)

ss =[]
for i in range(ls - lt +1):
    #print(s[i:i+lt], t)
    if agree(s[i:i+lt], t):
        ss.append( (s[:i] + t + s[i+lt:]).replace('?', 'a') )
#print(ss)

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

Submission status: Accepted

Solution code:
s = input()
t = input()
ls = len(s)
lt = len(t)

for i in range(ls, lt-1, -1):
  a = s[i-lt:i]
  if(all([a[j] == t[j] or a[j] == "?" for j in range(lt)])):
    s = s[:i-lt] + t + s[i:]
    print(s.replace("?","a"))
    exit()
    
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s = input()
t = input()
sl = len(s)
tl = len(t)
case = []

for i, si in enumerate(s):
    fault = False
    for j, tj in enumerate(t):
        if i + j >= sl:
            fault = True
            break
        if not s[i+j] in [tj, "?"]:
            fault = True
            break
    if not fault:
        case.append((s[0:i] + t + s[i+tl:]).replace("?", "a"))

if len(case) == 0:
    print("UNRESTORABLE")
else:
    print(sorted(case)[0])

Submission status: Accepted

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


for i in range(len(S) - len(T) + 1):
    for index, char in enumerate(S[i:i + len(T)], 0):
        if char != "?" and char != T[index]:
            break
    else:
        print("".join((S[:i] + T + S[i + len(T):])[::-1]).replace("?", "a"))
        break
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

a = len(s)
b = len(t)
#最小の文字列となるためには前にaを固めたほうが良いため後ろから入れる
for i in range(a-b,-1,-1):
    t_kamo = s[i:i+b]
    count = 0
    for j in range(b):
        if t_kamo[j] == t[j] or t_kamo[j] == "?":
            count += 1
    if count == b:
        #replaceメソット(リストには使えない,strに直接使う？)
        #条件分で記述
        ans = "".join(s[:i]+t+s[i+b:])
        print(ans.replace("?","a"))
        sys.exit()
        #ans = []"a" if "?" for i in ans]
        #print("join.(ans)")でもいけるはず
        
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
#Dubious Document 2
S1 = input()
T = input()
n = len(S1)-len(T)
S = ""
p = 0
for i in range(n+1):
    k = 1
    for j in range(len(T)):
        if T[j] == S1[i+j] or S1[i+j] == "?":
            pass
        else:
            k = k - 1
    if k == 1:
        S = S1[0:i] + T + S1[i+len(T):len(S1)]
        p = 1
for i in range(len(S)):
    if S[i] == "?":
        S = S[0:i] + "a" + S[i+1:len(S)]
if p == 0:
    print("UNRESTORABLE")
else:
    print(S)

Submission status: Accepted

Solution code:
s,t=list(input()),list(input())
length_s,length_t=len(s),len(t)

flag=0
for i in reversed(range(length_s-length_t+1)):
    word=s[i:i+length_t]
    cnt=0
    for j in range(length_t):
        if word[j]==t[j] or word[j]=="?":
            cnt +=1
        if cnt==length_t:
            flag=1
            s[i:i+length_t]=t
    if flag==1:break
    
s="".join(s).replace("?","a")
print(s if flag else "UNRESTORABLE")

Submission status: Accepted

Solution code:
s,t=list(input()),list(input())
length_s,length_t=len(s),len(t)
flag=0
for i in reversed(range(length_s-length_t+1)):
    if flag==1:break
    word=s[i:i+length_t]
    cnt=0
    for j in range(length_t):
        if word[j]==t[j] or word[j]=="?":
            cnt +=1
        if cnt==length_t:
            flag=1
            s[i:i+length_t]=t
s="".join(s).replace("?","a")
print(s if flag else "UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys


def main():
    S = list(input())[::-1]
    T = list(input())[::-1]
    d = len(S) - len(T) + 1

    for i in range(d):
        cnt = 0
        for j in range(len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                cnt += 1

        if cnt == len(T):
            for k in range(len(T)):
                S[i+k] = T[k]
            break

    else:
        print('UNRESTORABLE')
        sys.exit()

    for i in range(len(S)):
        if S[i] == '?':
            S[i] = 'a'

    print(''.join(S)[::-1])


main()

Submission status: Accepted

Solution code:
S = input()
T = input()
len_S = len(S)
len_T = len(T)
qc = 0
ans = ''
for i in range(len_S-1, -1, -1):
    si = S[i]
    if (si == '?' or si == T[0]) and len_S - i >= len_T:
        flag = True
        for ii in range(1, len_T):
            if T[ii] == S[i+ii] or S[i+ii] == '?':
                continue
            else:
                flag = False
                break
        if flag:
            ans = S[:i] + T + S[i+len_T:]
            break

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

Submission status: Accepted

Solution code:
s=input()
t=input()
score = 0
L = []
for i in range(len(s)-len(t)+1):
  k = s
  cut = k[i:i+len(t)]
  for j in range(len(t)):
    if cut[j] == t[j] or cut[j] =="?":
      score += 1
  if score == len(t):
    
    k = k[:i] + t + k[i+len(t):]
    L.append(k.replace("?","a"))
  score = 0
if L == []:
  print("UNRESTORABLE")
else:
  L = sorted(L)
  print(L[0])

Submission status: Accepted

Solution code:
S = input()
T = input()
def checker(s, t):
	for i in range(len(s)):
		if s[i] != "?" and s[i] != t[i]:
			return False
	return True


for i in reversed(range(len(S) - len(T) + 1)):
    if checker(S[i:(i + len(T))], T):
        S = S.replace('?', 'a')
        print(S[:i] + T + S[i + len(T):])
        exit()
print('UNRESTORABLE')
exit()

Submission status: Accepted

Solution code:
def main():
	s = input()
	t = input()
 
	for i in reversed(range(len(s) - len(t) + 1)):
		if checker(s[i:(i + len(t))], t):
			s = s.replace("?", "a")
			print(s[:i] + t + s[i+len(t):])
			return
	print("UNRESTORABLE")
 
def checker(s, t):
	for i in range(len(s)):
		if s[i] != "?" and s[i] != t[i]:
			return False
	return True
 
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
Sz=list(input())
T=list(input())
Flag=0

for i in range(len(Sz)-len(T),-1,-1):
    Flag=1
    for s in range(len(T)):

        if not (Sz[i+s]=="?" or Sz[i+s]==T[s]):
            Flag=0
            break
    if Flag:
        for s in range(len(T)):
             Sz[i+s]=T[s]
        break
for i in range(len(Sz)):
    if Sz[i]=="?":Sz[i]="a"

if Flag:print("".join(Sz))
else:print("UNRESTORABLE ")

Submission status: Accepted

Solution code:
S = list(input())
T = list(input())
lenS = len(S)
lenT = len(T)
change = 0
for i in range(lenS-lenT,-1,-1):
    a = 1
    for j in range(lenT):
        if S[i+j] != T[j] and S[i+j] != '?':
            a = 0
    if a == 1:
        for j in range(lenT):
            S[i+j] = T[j]
        change = 1
        break

for i in range(lenS):
    if S[i] == '?':
        S[i] = 'a'

if change == 1:
    print(''.join(S))
else:
    print('UNRESTORABLE')