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:
def match(i, j):
    if i == j: return True
    elif i == "?" or j == "?": return True
    else: return False

def solve():
    s = input()
    t = input()
    Ans = []
    for i in range(len(s) - len(t) + 1):
        #check
        l = list(s)
        for j in range(len(t)):
            if match(s[i + j], t[j]): 
                if s[i + j] == "?": l[i + j] = t[j]
            else: break
        else: #一致する
            for k, w in enumerate(l):
                if w == "?": l[k] = "a"
            Ans.append("".join(map(str, l)))
    if len(Ans) == 0: print("UNRESTORABLE")
    else: 
        Ans.sort()
        print(Ans[0])

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

No = 'UNRESTORABLE'

if len(T)>len(Sd):
    print(No)
    exit()

ofs = []
for i in range(len(Sd)-len(T)+1):
    for j in range(len(T)):
        if Sd[i+j] != T[j] and Sd[i+j] != '?':
            break
    else:
        ofs.append(i)

if not ofs:
    print(No)
    exit()

ans = ''
offset = ofs[-1]
for i in range(len(Sd)):
    if offset <= i < offset+len(T):
        ans += T[i-offset]
    else:
        if Sd[i] == '?': ans += 'a'
        else: ans += Sd[i]
print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    T = input()
    N = len(S)
    M = len(T)
    for i in range(N-M+1)[::-1]:
        for s, t in zip(S[i:i+M], T):
            if s != t and s != "?":
                break
        else:
            return print(S[:i].replace("?", "a") + T + S[i+M:].replace("?", "a"))
    print("UNRESTORABLE")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def canMake(left):
    for s, t in zip(S[left:], T):
        if s == '?':
            continue
        if s != t:
            return False
    return True

ans = []
for left in range(len(S) - len(T) + 1):
    if canMake(left):
        Z = S[: left] + T + S[left + len(T):]
        ans.append(Z.replace('?', 'a'))

if len(ans) == 0:
    ans.append('UNRESTORABLE')
ans.sort()
print(ans[0])

Submission status: Accepted

Solution code:
s=input()

t=input()

s=list(s)

t=list(t)

memo = -1

for i in range(len(s)-len(t)+1):
    count = 0
    for k, j in enumerate(range(i, i+len(t))):
        if s[j] == t[k] or '?' == s[j]:
            count += 1
    if count == len(t):
        memo = i

if memo == -1:
    print('UNRESTORABLE')
else:
    for k,l in enumerate(range(memo,memo+len(t))):
        s[l] = t[k]
    for g in range(len(s)):
        if s[g] == '?':
            s[g] = 'a'
    print(''.join(map(str,s)))

Submission status: Accepted

Solution code:
S = list(input())
T = list(input())
lenS = len(S)
lenT = len(T)
if lenS < lenT:
    print("UNRESTORABLE")
else:
    end = False
    for i in range(lenS - lenT, -1, -1):
        cnt = 0
        for j in range(lenT):
            if S[i + j] == T[j] or S[i + j] == "?":
                cnt += 1
            else:
                break
            if cnt == lenT:
                for k in range(lenT):
                    S[i + k] = T[k]
                end = True
        if end:
            ans = ""
            for k in range(lenS):
                if S[k] == "?":
                    S[k] = "a"
                ans = ans + S[k]
            print(ans)
            break
        if i == 0 and not end:
            print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S0 = input()
S0_n = len(S0)
T = input()
T_n = len(T)

T_kouho = []

for i in range(S0_n-T_n, -1, -1):
  for j,k in enumerate(range(i,i+T_n)):
    if T[j] != S0[k] and S0[k] != '?':
      break
  else:
    T_kouho.append(i)
    break

if len(T_kouho) == 0:
  print('UNRESTORABLE')
else:
  i = 0
  for _ in range(S0_n):
    if i >= S0_n:
      break
    if i == T_kouho[0]:
      print(T, end='')
      i += T_n
    elif S0[i] == '?':
      print('a', end='')
      i+=1
    else:
      print(S0[i], end='')
      i+=1

Submission status: Accepted

Solution code:
s=input()
t=input()
def ga(i,t):
    turn=0
    for k in range(len(t)):
        if s[-(i+k)]!=t[-(k+1)] and s[-(i+k)]!='?':
            break
    else:
        turn+=1
    return turn
li=list(s)
li2=list(t)
for i in range(1,len(s)-len(t)+2):
    if ga(i,t)==1:
        for k in range(len(t)):
            li[-(i+k)]=li2[-(k+1)]
        n=''.join(li)
        break
else:
    print('UNRESTORABLE')
    exit()
print(n.replace('?','a'))

Submission status: Accepted

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

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

ans = ""
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:
        ans = s[:i] + t + s[i+j+1:]
        ans = ans[::-1]
        print(ans.replace("?", "a"))
        exit()
        
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
#076_C
s_ = list(input())
t = list(input())
ans = []
for i in range(len(s_) - len(t) + 1):
    s = s_.copy()
    flg = True
    for j in range(len(t)):
        if s_[i+j] == '?':
            s[i+j] = t[j]
        elif s_[i+j] == t[j]:
            continue
        else:
            flg = False
    if flg:
        s = ''.join(s).replace('?', 'a')
        ans.append(s)
        
if len(ans) == 0:
    print('UNRESTORABLE')
else:
    ans.sort()
    print(ans[0])

Submission status: Accepted

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

len_S, len_T = len(S), len(T)



def judge(start):
    # print(S[start: start+len_T])
    for j in range(len_T):
        if S[start+j] == '?':
            continue

        elif S[start+j] == T[j]:
            continue

        else:
            return False

    return True


for i in range(len_S - len_T, -1, -1):
    if judge(i):
        S = S[:i] + T + S[i+len_T:]
        S = S.replace('?', 'a')
        print(S)
        exit()

print('UNRESTORABLE')

Submission status: Accepted

Solution code:
sdash=input()
t=input()
rsdash=sdash[::-1]
rt=t[::-1]
anslist=[]
for i in range(len(sdash)-len(t)+1):
    count=0
    ans=rsdash
    if rsdash[i]==rt[0] or rsdash[i]=="?":
        for j in range(1,len(t)):
            if rsdash[i+j]==rt[j] or rsdash[i+j]=="?":
                count+=1
        if count==len(t)-1:
            ans=rsdash[:i]+rt+rsdash[i+len(t):]
            ans=ans.replace("?","a")
            anslist.append(ans[::-1])
anslist.sort()
if not anslist:
    print("UNRESTORABLE")
else:
    print(anslist[0])

Submission status: Accepted

Solution code:
#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''


import sys
input = sys.stdin.readline
s = input().rstrip("\n")
t = input().rstrip("\n")
flg = False
cnt = 0

for i in range(len(s)-len(t)+1)[::-1]:
    cnt = 0
    for j in range(len(t)):
        #print(s[i+j],t[j])
        if s[i+j] == t[j] or s[i+j] == '?':
            cnt += 1
        #print(cnt,len(t))
    if cnt == len(t):
        cnt = i
        flg = True
        #print(cnt)
        break

if not flg:
    print("UNRESTORABLE")
else:
    li = list(s.replace("?", "a"))
    #print(li)
    for j in range(len(t))[::-1]:
        #print(li[cnt+j], t[j])
        li[cnt+j] = t[j]
    print("".join(li))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
s = input().rstrip("\n")
t = input().rstrip("\n")
flg = False

for i in range(len(s)-len(t)+1):
    cnt = 0
    for j in range(len(t)):
        #print(s[i+j],t[j])
        if s[i+j] == t[j] or s[i+j] == '?':
            cnt+=1
        #print(cnt,len(t))
    if cnt == len(t):
        temp=i
        flg = True
        #print(cnt)
 
if not flg:
    print("UNRESTORABLE")
else:
    li = list(s.replace("?","a"))
    #print(li)
    for j in range(len(t)):
        #print(li[cnt+j], t[j])
        li[temp+j] = t[j]
    print("".join(li))

Submission status: Accepted

Solution code:
sp = list(input())
t = list(input())
flag = False
cnt = -1
l = len(t)
for i in range(len(sp) - l+1):
    for j in range(l):
        if sp[i+j]  == t[j] or sp[i+j]  == "?":
            pass
        else:
            break
    else:
        cnt = i
if cnt == -1:
    print("UNRESTORABLE")
else:
    for i in range(len(sp)):
        if sp[i] == "?" :
            if not cnt<= i <cnt+l:
                sp[i] ="a"
            else :
                sp[i] = t[i-cnt]
    print("".join(sp))

Submission status: Accepted

Solution code:
import sys

def hantei(s,t):
  for i in range(len(s)):
    if s[i] == t[i] or s[i] == '?':
      pass
    else:
      return False
    
  return True

s = list(input())
t = list(input())

for i in reversed(range(len(s)-len(t)+1)):
  if hantei(s[i:i+len(t)], t):
    for j in range(len(t)):
      s[i+j] = t[j]
        
    print(''.join(s).replace('?', 'a'))
    sys.exit(0)
    
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s = input()
t = list(input())
import sys
ans = []
for i in range(len(s)):
  S = list(s)
  if S[len(s)-len(t)-i] == t[0] or '?':
    m = int(len(s)-len(t)-i)
    #print(m)
    if m >= 0:
      for j in range(len(t)):
        if S[m+j] == t[j] or S[m+j] == '?':
          S[m+j] = t[j]
          if j == len(t)-1:
            #print(S)
            for i in range(len(S)):
              if S[i] == '?':
                S[i] = 'a'
            ans.append(''.join(S))
            break
        else:
          S = list(s)
          break
if ans == []:
  print('UNRESTORABLE')
else:
  ans.sort()
  print(ans[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
t = input()
flag = 0
left = 0
if len(s) < len(t):
    print("UNRESTORABLE")
    exit()
for i in range(1+len(s)-len(t)):
    for j in range(len(t)):
        #後ろから一致or s[i]==?かをみる
        if s[-i-j-1] == "?" or s[-i-j-1] == t[-j-1]:
            if j == len(t) -1 :
                flag = 1
                break
        else: break
    if flag == 1: 
        left = i
        break
    if i == len(s)-len(t):
        print("UNRESTORABLE")
        exit()
#print(left)
for i in range(len(s)):
    if i < len(s) - left - len(t) or i >= len(s) - left:
        if s[i] == "?":
            print("a",end = "") 
        else: print(s[i],end="")
    elif i < len(s) - left:
        print(t[i-(len(s) - left-len(t))],end="")

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(len(SP)-len(T),-1,-1):
  cnt = 0
  for j in range(len(T)):
    if SP[i+j] == T[j] or SP[i+j] == "?":
      cnt = cnt +1
  if cnt == len(T):
    S = (SP[:i] + T + SP[i+len(T):]).replace("?","a")
    break
###    
if S == "":
  print("UNRESTORABLE")
else:
  print(S)

Submission status: Accepted

Solution code:
def check(a, b):
    for i in range(len(a)):
        if not(a[i] == b[i] or a[i] == '?'):
            return False
    return True


s_prime = input()
t = input()
ans_list = []
for i in range(len(s_prime) - len(t) + 1):
    if check(s_prime[i:i+len(t)], t):
        s = s_prime[:i] + t + s_prime[i+len(t):]
        ans = s.replace('?', 'a')
        ans_list.append(ans)
ans_list.sort()
if not ans_list:
    print('UNRESTORABLE')
else:
    print(ans_list[0])

Submission status: Accepted

Solution code:
SS=input()
T=input()
listT=[a for a in T]
TN=len(listT)
listSS=[b for b in SS]
SSN=len(listSS)
n=SSN-TN
numbers=[]
suuji=0
if n<0:
    print('UNRESTORABLE')
else:
    for c in range(n + 1):
        number = 0
        e=0
        for d in range(TN):
            if listSS[c + d] == listT[d] or listSS[c + d] == '?':
                number += 1


        if number == TN:
            e += c
            suuji += number
            numbers.append(e)
    if suuji ==0:
        print('UNRESTORABLE')
    else:
        mojiretu = []
        a=numbers[-1]
        A = int(a)
        moji = ''
        for b in range(TN):
            B = int(b)
            listSS[A + B] = listT[B]
        for z in listSS:
            moji += z

        answer = moji.replace('?', 'a')
        print(answer)

Submission status: Accepted

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

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

if sl < tl:
    print('UNRESTORABLE')
else:
    for i in range(sl-tl+1)[::-1]:
        flag = True
        for j in range(tl):
            if s[i+j] != '?' and s[i+j] != t[j]:
                flag = False
                break
        if flag:
            for j in range(sl):
                if i <= j < i + tl:
                    s[j] = t[j-i]
                    continue
                if s[j] == '?':
                    s[j] = 'a'
                    continue
            print(''.join(s))
            break
    if not flag:print('UNRESTORABLE')

Submission status: Accepted

Solution code:
#pypyは内包表記使わない
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    S = input().rstrip()
    T = input().rstrip()
    s = len(S)
    t = len(T)
    if s<t:
        print('UNRESTORABLE')
        exit()
    else:
        for i in range(s, t-1, -1):
            match = S[i-t:i]
            for j in range(t):
                if match[j] != '?' and T[j] != match[j]:
                    break
            else:
                S = S[:i-t] + T + S[i:]
                S = S.replace('?', 'a')
                print(S)
                exit()
    print('UNRESTORABLE')
    exit()

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

if len(S) < len(T):
    print("UNRESTORABLE")
    sys.exit()

ans = []

for i in range(len(S)-len(T)+1):
    for j, k in enumerate(S[i:i+len(T)]):
        if k == T[j] or k == "?":
            continue
        else: break
    else:
        box = S.copy()
        count = 0
        for x in range(len(S)):
            if i <= x < i + len(T):
                box[x] = T[count]
                count += 1
            elif box[x] != "?":
                continue
            else:
                box[x] = "a"
        ans.append(box)
ans = sorted(ans)
if len(ans) == 0:
    print("UNRESTORABLE")
else:
    print("".join(ans[0]))

Submission status: Accepted

Solution code:
s = input()
t = input()
sl = int(len(s))
tl = int(len(t))

for i in range(sl - tl, -1, -1):
    if s[i] == t[0] or s[i] == "?":
        for j in range(tl):
            if s[i + j] != t[j] and s[i+j] != "?":
                break
        else:
            print((s[:i] + t + s[i + len(t):]).replace("?", "a"))
            exit()

print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

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

def match(s, t, lt, offset):
    for i in range(lt):
        if t[i] != s[offset+i] and s[offset+i] != '?':
            return False
    return True

def main():
    S = input()
    T = input()
    len_S = len(S)
    len_T = len(T)
    ans = list(S.replace('?','a'))
    for i in range(len_S-len_T+1):
        offset = len_S-len_T-i
        if match(S, T, len_T, offset):
            for j in range(len_T):
                ans[offset+j] = T[j]
            break
    else:
        print('UNRESTORABLE')
        exit()
    print(''.join(ans))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

res=-1
ind=-1
for i in range(len(S_)-len(T)+1):
    flag=1
    for j in range(len(T)):
        if S_[i+j]=='?' or S_[i+j]==T[j]:
            continue
        else:
            flag=0
            break
    if flag:
        ind=i

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

for t in T:
    S_[ind]=t
    ind+=1

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

print(''.join(S_))

Submission status: Accepted

Solution code:
import copy
s = list(input())
t = list(input())
ans_list = []
n = len(s)
m = len(t)
for i in range(n-m+1):
  s_copy = copy.copy(s)
  judgement = True
  for j in range(m):
    if s_copy[i+j] != "?" and s_copy[i+j] != t[j]:
      judgement = False
      break
    else:
      s_copy[i+j] = t[j]
  if judgement:
    ans_list.append(s_copy)
x = len(ans_list)
if ans_list != []:
  for l in range(x):
    for k in range(n):
      if ans_list[l][k] == "?":
        ans_list[l][k] = "a"
  ans_list.sort()
  print("".join(ans_list[0]))
else:
  print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s_ = input()
t = input()
flag = False
ans = 'z' * 51
for i in range(len(s_) - len(t) + 1):
    for j in range(len(t)):
        if s_[i + j] != '?' and s_[i + j] != t[j]:
            break
    else:
        flag = True
        s = list(s_[:i] + t + s_[i + len(t):])
        for i in range(len(s)):
            if s[i] == '?':
                s[i] = 'a'
        x = ''.join(s)
        ans = min(ans, x)
if flag:
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import copy
s=list(map(str,input()))
t=list(map(str,input()))
def converta(s):
  for i in range(len(s)):
    if s[i]=='?':
      s[i]='a'
  return s
res=[]
for i in range(len(s)-len(t)+1):
    c=copy.copy(s)
    ok=True
    for j in range(len(t)):
        if t[j]!=s[i+j] and s[i+j]!='?':
            ok=False
            break
        else:
            c[i+j]=t[j]
    if ok:
        res.append(''.join(converta(c)))
if res==[]:
  print('UNRESTORABLE')
else:
  print(sorted(res)[0])

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

S = input()
T = input()
 
for i in reversed(range(len(S)-len(T)+1)):
    flag = 1
    for j in range(len(T)) :
        if S[i+j] != T[j] and S[i+j] != "?":
            flag = 0
    if flag:
        ans = S[:i] + T + S[i+len(T):]
        print(ans.replace("?", "a"))
        exit()
 
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=input()
t=input()
if len(s)<len(t):
  print("UNRESTORABLE")
  exit()
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:
def main():
    S = [s for s in input()]
    T = [t for t in input()]
    L = len(S)
    M = len(T)
    flag = 0
    ans = []
    for i in range(L-M+1):
        P = list(S)
        tmp = list(S[i:i+M])
        cnt = 0
        num = tmp.count('?')
        for j in range(M):
            if tmp[j] != '?' and tmp[j] != T[j]:continue
            elif tmp[j] == T[j]:
                cnt += 1
        if cnt == M-num:
            flag = 1
            for k in range(M):
                P[i+k] = T[k]
            ans.append(P)
            
    if flag:
        pnt = []
        while ans:
            X = ans.pop()
            for i in range(L):
                if X[i] == '?':
                    X[i] = 'a'
            pnt.append(X)
        pnt.sort()
        print(''.join(pnt[0]))
    else:
        print('UNRESTORABLE')

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import re
def end():
    print('UNRESTORABLE')
    exit()

s = input()
t = input()

l_s = len(s)
l_t = len(t)

if l_t > l_s:
    end()

for i in range(l_s - l_t +1):
    target = s[l_s-i-l_t:l_s-i]
    count = 0
    for s1, s2 in zip(target, t):
        if s1 == s2 or s1 == '?':
            count += 1
            if count == l_t:
                ans = re.sub('\?','a',s)
                print(ans[:l_s-i-l_t]+t+ans[l_s-i:])
                exit()
        else:
            break
end()

Submission status: Accepted

Solution code:
sd=input()
t=input()
n=len(sd)
m=len(t)
if n<m:
    print("UNRESTORABLE")
    exit(0)
for i in range(n-m,-1,-1):
    t_kana=sd[i:i+m]
    for j in range(m+1):
        if j==m :
            print((sd[:i]+t+sd[i+len(t):]).replace("?","a"))
            exit(0)
        if t_kana[j]=="?":
            continue
        elif t_kana[j]!=t[j]:
            break
print("UNRESTORABLE")

Submission status: Accepted

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

l = len(T)

ind = 51
for i in range(len(S)-l+1):
    flag = 0
    for j in range(l):
        if S[i+j]!='?' and S[i+j]!=T[j]:
            flag = 1
    if flag==0:
        ind = i

if ind == 51:
    print('UNRESTORABLE')
    exit()

print(S[:ind].replace('?','a')+T+S[ind+l:].replace('?','a'))

Submission status: Accepted

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

s_reversed = list(reversed(S))
T_reversed = list(reversed(T))
F = 0

for i in range(len(S)-len(T)+1):
    s = s_reversed[i:i+len(T)]
    flag = 0
    for j in range(len(T)):
        if s[j] == T_reversed[j] or s[j] == '?':
            continue
        else:
            flag += 1
            break
    if flag ==0:
        F += 1
        for j in range(len(T)):
            s_reversed[i+j] = T_reversed[j]
        break

if F ==0:
    print('UNRESTORABLE')
else:
    R = ''.join(list(reversed(s_reversed)))
    print(R.replace('?','a'))

Submission status: Accepted

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


def is_valid(start, end):
    for t_i, s_i in enumerate(range(start, end)):
        if S[s_i] == "?" or S[s_i] == T[t_i]:
            continue
        else:
            return False
    return True


def assign(start, end):
    for t_i, s_i in enumerate(range(start, end)):
        S[s_i] = T[t_i]
    for i in range(len(S)):
        if S[i] == "?":
            S[i] = "a"


ok = False
for i in range(len_t-1, len(S))[::-1]:
    if S[i] == "?" or S[i] == T[-1]:
        start = i - len_t + 1
        end = i + 1
        if is_valid(start, end):
            ok = True
            assign(start, end)
            break

if ok:
    print("".join(S))
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

key = [str(t[i]) for i in range(len(t))]
count = 0
sub = []

for i in range(len(s)-len(t)+1):
  for j in range(len(t)):
    if s[i+j] != "?" and s[i+j] != t[j]:
      break
    if j == len(t)-1:
      sub.append(i)


ans = [0]*len(s)
if sub:
  k = 0
  change = sub[-1]
  for i in range(len(s)):
    if i == change:
      for j in range(len(t)):
        ans[i+j] = t[j]
      k = len(t)-1
    elif k:
      k -= 1
    else:
      if s[i] == "?":
        ans[i] = "a"
      else:
        ans[i] = s[i]
  print("".join(ans))  
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

lens = len(S) - len(T) + 1

for i in reversed(range(lens)) :
    flag = True
    for j in range(len(T)) :
        if S[i+j] != T[j] and S[i+j] != "?":
            flag = False
    if flag == True :
        ans = S[:i] + T + S[i+len(T):]
        print(ans.replace("?", "a"))
        exit()

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=1
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def is_ok(sd, st, t):
	if len(sd) < st+len(t):
		return False
	for i, c in enumerate(sd[st:st+len(t)]):
		if c != '?' and t[i] != c:
			return False
	return True
	
def to_aaa(a_str):
	rr=list(a_str)
	for i, c in enumerate(rr):
		rr[i] = 'a' if c == '?' else c
	ret = ''.join(rr)
	return ret

def mk_s(a_s_dash, i, t):
	front = to_aaa(a_s_dash[:i])
	back = to_aaa(a_s_dash[i+len(t):])
	ret = front+t+back
	#pa((i,t,front,t,back,ret))
	return ret
			
def main():
	s_dash = input()
	t=input()
	dd='UNRESTORABLE'
	qq='?'
	
	aa=[]
	for i, _ in enumerate(s_dash[:len(s_dash)+1]):
		#pa((s_dash,i,t))
		if is_ok(s_dash,i,t):
			aa.append(mk_s(s_dash, i, t))
			#pa(aa)
			
	if len(aa)>0:
		aa.sort()
		ret = aa[0]
		print(ret)
		return
		
	print(dd)
	return
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
import copy
S_original = input()
T = input()

lis_t = list(T)
T_size = len(T)
S_size = len(S_original)
hantei = 0
lis = []

if T_size <= S_size:
    for start_index in range(S_size - T_size + 1):

        S_prime = copy.deepcopy(S_original)
        hantei = 0
        for i in range(T_size):
            if S_prime[start_index+i] == T[i]:
                continue
            elif S_prime[start_index+i] == "?":
                s_lis = list(S_prime)
                s_lis[start_index+i] = lis_t[i]
                S_prime = "".join(s_lis)
            elif S_prime[start_index+i] != T[i]:
                hantei = 1
                break
        if hantei == 0:
            for j in range(S_size):
                if S_prime[j] != "?":
                    continue
                else:
                    s_lis = list(S_prime)
                    s_lis[j] = "a"
                    S_prime = "".join(s_lis)
            lis.append(S_prime)

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

Submission status: Accepted

Solution code:
S1=input()
T=input()
lens1=len(S1)
lent=len(T)
ans="z"*(lens1+1)
def OK(k):
    for i in range(lent):
        if S1[k+i]!=T[i] and S1[k+i]!="?":
            return False
    return True
for i in range(lens1-lent+1):
    if OK(i):
        Stmp=""
        for j in range(lens1):
            if i<=j<i+lent:
                Stmp+=T[j-i]
            elif S1[j]=="?":
                Stmp+="a"
            else:
                Stmp+=S1[j]
        if Stmp<ans:
            ans=Stmp
if ans!="z"*(lens1+1):
    print(ans)
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

S_dash = input()
T = input()

S_dash_list = list(S_dash)
T_list = list(T)

ei = len(S_dash_list)
si = ei - len(T_list)

insert_flag = False
while si >= 0:
    #print('si: {}, ei: {}'.format(si, ei))
    target = S_dash_list[si:ei]
    flag = True
    for i in reversed(range(len(T_list))):
        if S_dash_list[si + i] != T_list[i] and S_dash_list[si + i] != '?':
            flag = False
            # ここでsiとeiを更新
            ei -= 1
            si = ei - len(T_list)
            break
    if flag:
        for i in range(len(T_list)):
            S_dash_list[si + i] = T_list[i]
        insert_flag = True
        break

if not insert_flag:
    print('UNRESTORABLE')
else:
    ans = ''.join(S_dash_list)
    #print(ans)

    # '?'をaで埋める
    print(ans.replace('?', 'a'))

Submission status: Accepted

Solution code:
def search(S,T):
    match = 0
    maxmatch = 0
    maxmatchloc = -1
    for x in range(len(S)-(len(T)-1)):
        match = 0
        if (S[x] == T[0] or S[x] == '?'):
            for i in range(0,len(T)):
                ##print(S[x+i] + '-' + T[i])
                if (S[x+i] == T[i] or S[x+i] == "?"):
                    match += 1
                else:
                    ##print("Entered!")
                    match = 0
                    break
            if (match >= maxmatch and match != 0):
                ##print("ENTERED")
                maxmatchloc = x
                maxmatch = match
        ##print('At x = ' + str(x) + ': maxmatchlocation is ' + str(maxmatchloc) + ' match is ' + str(match) + ' maxmatch is' + str(maxmatch))
    return maxmatchloc

S = input()
T = input()
code = S
if '?' in S:
    if (len(T) <= len(S)):
        startpoint = search(S,T)
        for x in range(len(S)):
            if (S[x] == '?'):
                code = code[:x] + 'a' + code[x+1:] 
        if(startpoint >= 0):
            code = code[:startpoint] + T + code[startpoint + len(T):]
            print(code)
        else:
            print('UNRESTORABLE')
    else:
        print('UNRESTORABLE')
else:
    print(S)

Submission status: Accepted

Solution code:
S=input()
T=input()
li=[]
for i in reversed(range(len(S)-len(T)+1)):
    st=S[i:i+len(T)]
    t=True
    for j in range(len(T)):
        if(st[j]!='?' and st[j]!=T[j]):
            t=False
            break
    if(t):
        tmp=S.replace('?','a')
        li.append(tmp[:i]+T+tmp[i+len(T):])
if(li):
    print(sorted(li)[0])
else:
    print("UNRESTORABLE")