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:
sd=input()
t=input()

startpoint=51

for i in range(len(sd)-len(t)+1):
	proflag=True
	
	for j in range(len(t)):
		if sd[i+j] == "?":
			continue
		elif sd[i+j] == t[j]:
			continue
		else:
			proflag=False
	if proflag:
		startpoint=i
		



if startpoint == 51:
	print("UNRESTORABLE")
else:
	sd=sd.replace('?','a')
	print(sd[:startpoint]+t+sd[startpoint+len(t):])

Submission status: Accepted

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

ans = [None for i in range(len(s))]
for i in range(len(s)):
    ans[i] = s[i]

for i in reversed(range(len(s)-len(t)+1)):
    flag = 0
    for j in range(len(t)):
        if s[i+j]!=t[j] and s[i+j]!="?":
            flag = 1
    if flag == 0:
        for j in range(len(t)):
            ans[i+j]=t[j]
        for j in range(len(s)):
            if ans[j]=="?":
                ans[j]="a"
        print("".join(ans))
        exit()

print("UNRESTORABLE")

Submission status: Accepted

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

NS = len(Sd)
NT = len(T)

if NS < NT:
  print("UNRESTORABLE")
else:
  flag = False
  for i in range(NS-NT+1):
    for j in range(NT):
      if Sd[i+j] != "?" and Sd[i+j] != T[j]:
        break
      if j == NT-1:
        flag = True
        if "change" not in locals():
          change = i #j番目からTに変えれば満たせる。
        else:
          change = max(change,i) #変更は一番右側にして残りはaにする。
  if flag:
    if "change" in locals():
      ans = Sd[:change]+T+Sd[change+NT:]
    else:
      ans = Sd
    print(ans.replace("?","a"))
  else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
def hantei(start,key):
    if( ango[start]=="?" or ango[start]==key) and len(key)==1:
        return True
    if ango[start]=="?" or ango[start]==key[0]:
        key=key[1:]
        return hantei(start+1,key)
    else:
        return False
ango=input()
key=input()
top=key[0]
ans="UNRESTORABLE"
for i in range(len(ango)-len(key)+1):
    if hantei(i,key):
        ans=ango[:(i)]+key+ango[(i+len(key)):]
for j in range(len(ans)):
    if ans[j]=="?":
        ans=ans[:j]+"a"+ans[(j+1):]
print(ans)

Submission status: Accepted

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

DEBUG = False

reversed_S_dash = S_dash[::-1]
reversed_T = T[::-1]

for i in range(len(S_dash)-len(T)+1):
    flag = True
    for j in range(len(T)):
        if DEBUG:
            print("i: {}, j: {}".format(i, j))
            print("S_dash[i+j]: {}, T[j]: {}".format(reversed_S_dash[i+j], reversed_T[j]))
        if reversed_S_dash[i+j] != reversed_T[j] and reversed_S_dash[i+j] != '?':
            if DEBUG:
                print("miss match")
            flag = False
            break
    if flag:
        if DEBUG:
            print("find")
            print("from {} to {}".format(i, i+len(T)-1))
        ans = reversed_S_dash[:i]+reversed_T+reversed_S_dash[i+len(T):]
        ans = ans.replace('?', 'a')
        print(ans[::-1])
        exit(0)

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import re

S_ = input().replace('?', '.')
T = input()
flag = True
for i in range(len(S_)-len(T), -1, -1):
    angou = re.compile(S_[i:i+len(T)])
    mo = angou.search(T)
    if mo != None:
        S_ = S_[:i] + mo.group() + S_[i+len(T):]
        flag = False
        break
S = S_.replace('.', 'a')

if flag:
    print('UNRESTORABLE')
else:
    print(S)

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

S = input()
T = input()
sl = len(S)
tl = len(T)

def f(idx):
    if idx + tl > sl:
        return False
    for i in range(tl):
        if S[idx + i] != '?' and S[idx + i] != T[i]:
            return False

    ret = [''] * sl
    for i in range(sl):
        ret[i] = T[i - idx] if idx <= i < idx + tl else S[i] if S[i] != '?' else 'a'

    return "".join(ret)

def solve():
    for i in range(sl, -1, -1):
        if f(i):
            print(f(i))
            return

    print("UNRESTORABLE")

solve()

Submission status: Accepted

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


def match(k):
    for i in range(len(t)):
        if t[i] != s[i + k] and s[i + k] != "?":
            return False
    return True


def construct(k):
    ans = list(s)
    for i in range(len(t)):
        ans[k + i] = t[i]
    for i in range(len(s)):
        if ans[i] == "?":
            ans[i] = "a"
    return "".join(ans)


K = len(s) - len(t) + 1

pw_candid = []

for i in range(K):
    if match(i):
        pw_candid.append(construct(i))
        # print(construct(i))
        # sys.exit()
if len(pw_candid):
    print(sorted(pw_candid)[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
sp_l = list(input())
t_l = list(input())

candidates = []
for i in range(len(sp_l)): 
    s_l = sp_l[:] # make new but identical list
    if sp_l[i] == t_l[0] or sp_l[i] == '?':
        s_l[i] = t_l[0]
        for j in range(1,len(t_l)):
            if i+j >= len(sp_l):
                break
            if sp_l[i+j] == t_l[j] or sp_l[i+j] == '?':
                s_l[i+j] = t_l[j]
            else:
                break
        else:
            candidates.append(''.join(s_l))

if len(candidates) == 0:
    print('UNRESTORABLE')
else:
    for i in range(len(candidates)):
        candidates[i] = candidates[i].replace('?','a')
    candidates.sort()
    print(candidates[0])

Submission status: Accepted

Solution code:
s_prime=list(input())
t=list(input())
ls=len(s_prime)
lt=len(t)
s_prime.reverse()
t.reverse()


if lt>ls:
    print('UNRESTORABLE')
else:
    def check(t,s):
        l=len(t)
        c=0
        for i in range(l):
            if s[i]!='?' and s[i]!=t[i]:
                c=1
        return True if c==0 else False
    c=0            
    for i in range(ls-lt+1):
        print()
        if check(t,s_prime[i:i+lt])==1:
            t_in=s_prime[:i]+t+s_prime[i+lt:] 
            for k in range(ls):
                if t_in[k]=='?':
                    t_in[k]='a'
            t_in.reverse()   
            print(''.join(t_in))
            c=1
            break
    print('UNRESTORABLE') if c==0 else exit()

Submission status: Accepted

Solution code:
import bisect
def resolve():
    S = input()
    T = input()
    lenT = len(T)
    found = False
    for i in range(len(S)-lenT, -1, -1):
        for j in range(0, lenT):
            if not (S[i+j] == "?" or S[i+j] == T[j]):
                break
        else:
            S = S[:i] + T + S[i+lenT:]
            found = True
            break
    if found:
        print(S.replace("?", "a"))
    else:
        print("UNRESTORABLE")

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    s = list(input())
    t = list(input())
    ls = len(s)
    lt = len(t)

    s_true = ""
    from copy import copy

    for i in range(ls - lt + 1):
        s_temp = copy(s)
        for j in range(lt):
            if s[i + j] == t[j]:
                pass
            elif s[i + j] == "?":
                s_temp[i + j] = t[j]
            else:
                break

            if j == lt - 1:
                s_true = copy(s_temp)

    if not s_true:
        print("UNRESTORABLE")
    else:
        for i in range(ls):
            if s_true[i] == "?":
                s_true[i] = "a"

    print("".join(s_true))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin

Sp = stdin.readline().rstrip()
T =  stdin.readline().rstrip()


N = len(Sp)
M = len(T)

def check_feasibility(S, T, i):
    S_temp = S[i:i+M]
    index=0
    for j in range(M):
        if S_temp[j]=="?" or S_temp[j]==T[j]:
            index+=1
            continue
        else:
            break
    if index==len(T):
        return 1
    else:
        return 0

pos=-1
for i in range(N-M+1):
    is_ok = check_feasibility(Sp, T, i)
    if is_ok:
        pos=i
        
if pos==-1:
    print("UNRESTORABLE")
else:
    new_s = Sp[:pos] + T + Sp[pos+M:]
    new_s = new_s.replace("?","a")
    print(new_s)

Submission status: Accepted

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

Submission status: Accepted

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


    anss = []
    for i in range(max(0, len(S)-len(T)+1)):
        ans = ""
        for j in range(i):
            if S[j] == "?":
                ans += "a"
            else:
                ans += S[j]

        for j in range(i, i+len(T)):
            if S[j] == T[j-i]:
                ans += S[j]
            elif S[j] == "?":
                ans += T[j-i]
            else:
                break

        for j in range(i+len(T), len(S)):
            if S[j] == "?":
                ans += "a"
            else:
                ans += S[j]

        if len(ans) == len(S):
            anss.append(ans)

    if len(anss):
        print(min(anss))
    else:     
        print("UNRESTORABLE")

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

for i in reversed(range(len(S) - len(T) + 1)):
  T_flg = S[i: i + len(T)]
  for j in range(len(T)):
    if T_flg[j] != T[j] and T_flg[j] != "?":
      break
  else:
    flag = True
    S[i:i + len(T)] = T
    break

if flag == False:
  print("UNRESTORABLE")
else:
  for i in range(len(S)):
    if S[i] == "?":
      S[i] = "a"
  print("".join(map(str,S)))

Submission status: Accepted

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

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


for i in reversed(range(n - m + 1)):
  t_kamo = S[i: i + m]
  for j in range(m + 1):
    if j == m:
      print((S[:i] + T +  S[i + m:]).replace("?", "a"))
      exit()
    if t_kamo[j] == "?":
      continue
    elif t_kamo[j] != T[j]:
      break

print("UNRESTORABLE")

Submission status: Accepted

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

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

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

for i in range(s-t,-1,-1):
    for j in range(t):
        if S[i+j] != '?' and S[i+j] != T[j]:
            break
        else:
            continue
    else:
        A = []
        for j in range(s):
            if i <= j < i+t:
                A.append(T[j-i])
            elif S[j] == '?':
                A.append('a')
            else:
                A.append(S[j])
        ans = A[0]
        for j in range(1,s):
            ans = ans + A[j]
        print(ans)
        break
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

a = len(s)
b = len(t)
ans = []

if a-b < 0:
    print("UNRESTORABLE")
    exit()

for i in range(a-b+1):
    frag = True
    for j in range(b):
        if s[i+j] != t[j] and s[i+j] != "?":
            frag = False
    if frag == True:
        code = s[:i] + t + s[i+b:]
        ans.append(code.replace("?","a"))

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

Submission status: Accepted

Solution code:
s = input()
t = input()
ans = []
if len(s) < len(t):
    print("UNRESTORABLE")
    exit()

for i in range(len(s) - len(t) + 1):
    prot = list(s)
    flag = True
    for j in range(len(t)):
        if prot[i + j] != t[j] and prot[i + j] != "?":
            flag = False
            break
        prot[i + j] = t[j]


    if flag == True:
        prot = "".join(prot).replace("?", "a")
        ans.append(prot)

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

Submission status: Accepted

Solution code:
import copy

s=str(input())
t=str(input())
ls=len(s)
lt=len(t)
S=list(s)
T=list(t)
#print(S,T)


def fill(List):
    for j in range(len(List)):
        if List[j]=='?':
            List[j]='a'
    return List


def change1(index):
    global lt

    cS=copy.copy(S)
    cT=copy.copy(T)
    for k in range(index+1,index+lt):
        if cS[k]=='?':
            cS[k]=cT[k-index]
        elif cS[k]==cT[k-index]:
            pass
        else:
            return False,''
            break
    else:
        return True,''.join(fill(cS))

def change2(index):
    global lt

    cS=copy.copy(S)
    cT=copy.copy(T)
    cS[index]=cT[0]
    for k in range(index+1,index+lt):
        if cS[k]=='?':
            cS[k]=cT[k-index]
        elif cS[k]==cT[k-index]:
            pass
        else:
            return False,''
            break
    else:
        return True,''.join(fill(cS))



L=[]
for i in range(ls-lt+1):
    #print(ls-lt)
    if S[i]==T[0]:
        #print('Yes')
        if change1(i)[0]:
            L.append(change1(i)[1])
    elif S[i]=='?':
        if change2(i)[0]:
            L.append(change2(i)[1])
    


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

Submission status: Accepted

Solution code:

import sys  
import numpy as np

input = sys.stdin.readline

S_ = input()
T = input()

S_ = list(S_)[:-1]
T = list(T)[:-1]

j = False
for i in range(len(S_)-len(T)+1):
    if i==0:
        part = S_[-len(T):]
    else :
        part = S_[-len(T)-i:-i]
    j = False
    for k in range(len(part)-1,-1,-1):
        if part[k] == "?":
            if k==0:
                j = True
            continue
        elif part[k] == T[k]:
            if k==0:
                j = True
        else:
            break
    if j:
        break


if j:
    for p in range(len(T)):
        S_[len(S_)-len(T)-i+p] = T[p]
    # print("*",S_)
    for k in range(len(S_)):
        if S_[k] == "?":
            S_[k] = "a"

    print("".join(S_))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S_=input()
T=input()
N=len(S_)
M=len(T)
posi=[]

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
  if flag==True:
    posi.append(S_[:i]+T+S_[i+M:])
#print(posi)
    
if posi==[]:
  print("UNRESTORABLE")
else:
  ans_list=[]
  for item in posi:
    ans=""
    for str in item:
      if str=="?":
        ans += "a"
      else:
        ans += str
    ans_list.append(ans)
  ans_list.sort()
  print(ans_list[0])

Submission status: Accepted

Solution code:
S_dash = list(input())
M = len(S_dash)

T = list(input())
N = len(T)

match = [False for _ in range(M)]

last_match = -100
for i in range(M - N + 1):
    for j in range(N):
        if S_dash[i+j] == "?":
            continue
        elif S_dash[i+j] == T[j]:
            continue
        elif S_dash[i+j] != T[j]:
            break
    else:
        match[i] = True
        last_match = i
S = S_dash

if last_match == -100:
    print("UNRESTORABLE")
else:
    for i in range(N):
        S[last_match+i] = T[i]
    for i in range(M):
        if S[i] == "?":
            S[i] = "a"
    print("".join(str(x) for x in (S)))

Submission status: Accepted

Solution code:

S_dash = list(input())
M = len(S_dash)

T = list(input())
N = len(T)

match = [False for _ in range(M)]

last_match = -100
for i in range(M - N + 1):
    for j in range(N):
        if S_dash[i+j] == "?":
            continue
        elif S_dash[i+j] == T[j]:
            continue
        elif S_dash[i+j] != T[j]:
            break
    else:
        match[i] = True
        last_match = i
S = S_dash.copy()

if last_match == -100:
    print("UNRESTORABLE")
else:
    for i in range(N):
        S[last_match+i] = T[i]
    for i in range(M):
        if S[i] == "?":
            S[i] = "a"
    print("".join(str(x) for x in (S)))

Submission status: Accepted

Solution code:
import sys,os
from collections import defaultdict, deque
from math import ceil, floor
if sys.version_info[1] >= 5:
    from math import gcd
else:
    from fractions import gcd
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    MOD = 10**9+7

    S = input()
    T = input()
    p = -1
    for i in range(len(S) - len(T) +1):
        match = True
        for j in range(len(T)):
            if not(S[i+j] == '?' or S[i+j] == T[j]):
                match = False
                break
        if match:
            p = i

    if p == -1:
        print('UNRESTORABLE')
    else:
        print((S[:p] + T + S[p+len(T):]).replace('?','a'))

    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from copy import copy
S = list(input())
T = list(input())
N = len(S)
M = len(T)
l = []
flag = 0
exist = 0
for i in range(N-M+1):
    flag = 0
    for j in range(M):
        if T[j] != S[i+j] and S[i+j] != "?":
            flag += 1

    if flag == 0:
        tmp = copy(S)
        for k in range(M):
            tmp[i+k] = T[k]
        l.append(tmp)
        exist = 1

a = []
if exist == 1: 
    L = len(l)
    for i in range(L):
        tmp = copy(l[i])
        for j in range(N):
            if tmp[j] == "?":
                tmp[j] = "a"
        a.append(tmp)
    a.sort()
    ans = "".join(a[0])
    print(ans)
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s = list(input())
t = list(input())
n = len(s)
m = len(t)
if n < m:
    print('UNRESTORABLE')
else:
    f2 = False
    for i in range(n, m-1 , -1):
        f = True
        num = i
        for j in range(1,m+1):
            if s[i-j] != t[-j] and s[i-j] != '?':
                f = False
                break
        if f:
            f2 = True
            break
    if f2:
        for j in range(1, m+1):
            s[num-j] = t[-j]
        for i in range(n):
            if s[i] =='?':
                s[i]= 'a'
        print(''.join(s))
    else:
        print('UNRESTORABLE')

Submission status: Accepted

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

a = len(s)
b = len(t)
ans = []
if a-b+1 < 0:
    print("UNRESTORABLE")
    exit()

for i in range(a-b+1):
    p = 0
    for j in range(b):
        if s[i+j] != t[j] and s[i+j] != "?":
            p = 1
            break
    if p == 0:
        kar = s[:i] + t + s[i+b:]
        kar = kar.replace("?","a")
        ans.append(kar)
if len(ans) == 0:
    print("UNRESTORABLE")
else:
    ans.sort()
    print(ans[0])

Submission status: Accepted

Solution code:
s = input()
n = len(s)
t = input()
m = len(t)
ans = []
res = []
for start in range(n):
    ok = True
    for i in range(len(t)):
        if i+start > n-1:
            ok = False
            break
        if not(t[i] == s[i+start] or s[i+start] == '?'):
            ok = False
            break
    
    
    if ok:res.append(start)
for st in res:
    string = ""
    for i in range(0,st):
        if s[i] == '?':
            string +='a'
        else:
            string += s[i]
    string += t
    for i in range(st+m,n):
        if s[i] == '?':string += 'a'
        else:string += s[i]
    ans.append(string)
if len(ans) >0:
    ans.sort()
    print(ans[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

if len(S_dush) < len(T):
    print("UNRESTORABLE")
    exit()

S_dush_list = []
for i in range(len(S_dush)):
    S_dush_list.append(S_dush[i])

def judge():
    for i in range(len(S_dush)-len(T),-1,-1):
        T_index = 0
        for j in range(i,i+len(T)):
            if S_dush_list[j] != "?":
                if S_dush_list[j] != T[T_index]:
                    break
            T_index += 1
            if len(T) == T_index:
                return j
    return -1

index = judge()
if index == -1:
    print("UNRESTORABLE")
    exit()

for i in range(index-len(T)+1,index+1):
    S_dush_list[i] = T[i-index+len(T)-1]

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

print(''.join(S_dush_list))

Submission status: Accepted

Solution code:
s=input()
T=input()
ans=[]
for i in range(len(s)-len(T)+1):
  flag=True
  S=s[:i]+T+s[i+len(T):]
  for j in range(len(T)):
    if(s[i+j]!="?" and s[i+j]!=T[j]):
      flag=False
  if(flag==True):
    S.replace("?","a")
    #print(S)
    ans.append(S.replace("?","a"))

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

Submission status: Accepted

Solution code:
import sys
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))
MIIZ = lambda: list(map(lambda x: x-1, MII()))

def main():
    s = IS()
    t = IS()
    ls = len(s)
    lt = len(t)
    for i in reversed(range(ls-lt+1)):
        ss = s[i:i+lt]
        for j in range(lt):
            if ss[j] == '?' or ss[j] == t[j]: continue
            else: break
        else:
            s = s[:i] + t + s[i+lt:]
            ans = s.replace('?', 'a')
            print(ans)
            exit(0)
    print('UNRESTORABLE')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc076/tasks/abc076_c"

def main():
    S = input()
    T = input()
    ans = 'UNRESTORABLE'
    words = []
    if S.count('?') == 0:
        print(S)
        exit()

    for i in range(len(S)):
        if S[i] != T[0] and S[i] != '?':
            continue
        s = S[i:i + len(T)]
        if len(s) < len(T):
            continue

        flg = False
        for j, ss in enumerate(s):
            flg = True if ss == T[j] or ss == '?' else False
            if not flg: break
        if flg:
            words.append(S[:i] + T + S[i + len(T):])
    if not words:
        print(ans)
        exit()
    for i in range(len(words)):
        words[i] = words[i].replace('?', 'a')
    words.sort()
    print(words[0])


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

s = [x for x in s[::-1]]
t = [x for x in t[::-1]]    

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

if not flg:
    print('UNRESTORABLE')
    exit()

ans = ''
s = [x for x in s[::-1]]
for i in range(len(s)):
  if s[i] == '?':
    s[i] = 'a'
  ans += s[i]
print(ans)

Submission status: Accepted

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

ans = "UNRESTORABLE"

for i in range(len(s)-len(t)+1):

    for j in range(len(t)):
        isRestorable = True
        if not s[j+i] == t[j] and not s[j+i] == "?":
            isRestorable = False
            break

    if isRestorable:
        ans = s[0:i] + t + s[i+len(t):len(s)]
        ans = ans.replace("?", "a")
print(ans)

Submission status: Accepted

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

def judge(A,B):
    for i in range(len(A)):
        if A[i] != B[i] and A[i] != "?" and B[i] != "?":
            return False
    return True

def dec(S_prime,T):
    S_cand = []
    for i in range(len(S_prime)-len(T)+1):
        if judge(S_prime[i:i+len(T)],T):
            S_cand.append( S_prime[:i] + T + S_prime[i+len(T):] )
    return S_cand

def S_change(S):
    S = list(S)
    for i in range(len(S)):
        if S[i] == "?":
            S[i] = "a"
    S = "".join(S)
    return S

if len(dec(S_prime,T)) == 0:
    print("UNRESTORABLE")
else:
    Ans = dec(S_prime,T)
    for i in range(len(Ans)):
        Ans[i] = S_change(Ans[i])
    Ans.sort()
    print(Ans[0])

Submission status: Accepted

Solution code:
s=list(input())
t=list(input())
S,T=len(s),len(t)
t=t+['?']
if S<T:
  print('UNRESTORABLE')
  exit()
count=[0]*S
for i in range(T+1):
  if t[i] in s:
    x=s.index(t[i])
    if all(s[x-i+j]=='?' or s[x-i+j]==t[j] for j in range(T)):
      count[x-i]=1   
if max(count)==0:
  print('UNRESTORABLE')
  exit()
count=count[::-1]
x=count.index(1)
x,j=S-x-1,0
for i in range(S):
  if (i<x or i>x+T-1):
    if s[i]=='?':
      s[i]='a'
  else:
    s[i]=t[j]
    j+=1
print(''.join(s))

Submission status: Accepted

Solution code:
s=list(input())
t=list(input())
a=-1

for i in range(len(s)-len(t)+1):
    for j in range(len(t)):
        if s[j+i]!=t[j] and s[j+i]!='?':
            break
        if (s[j+i]==t[j] or s[j+i]=='?') and j==len(t)-1:
            a=i

if a<0:
    print('UNRESTORABLE')
else:
    for i in range(len(s)):
        if i>=a and i-a<len(t):
            s[i]=t[i-a]
        if s[i]=='?':
            s[i]='a'
    print(''.join(s))

Submission status: Accepted

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

ans = "UNRESTORABLE"

for i in range(len(s)-len(t)+1):

    for j in range(len(t)):
        isRestorable = True
        if not s[j+i] == t[j] and not s[j+i] == "?":
            isRestorable = False
            break
            
    if isRestorable:
        ans = s[0:i] + t + s[i+len(t):len(s)]
        ans = ans.replace("?", "a")
        
print(ans)

Submission status: Accepted

Solution code:
import sys

s, t = sys.stdin.read().split()
n = len(s); m = len(t)
s = list(s)

def main():
    for i in range(n - m, -1, -1):
        for j in range(m):
            if t[j] != s[i+j] != '?':
                break 
        else:
            for j in range(m):
                s[i+j] = t[j]
            for i in range(n):
                if s[i] == '?': s[i] = 'a'
            print(''.join(s))
            return
    print('UNRESTORABLE')
    
if __name__ ==  '__main__':
    main()

Submission status: Accepted

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

ans = ''
dec = ''
cando = False

def match(enc, key):
  result = True
  for e, k in zip(enc, key):
    if e == '?':
      continue
    else:
      if e != k:
        result = False
        break
  return result

if len(s) < len(t):
  cando = False
else:
  for i in range(len(s)-len(t), -1, -1):
    if match(s[i:i+len(t)], t):
      dec = t
      if i > 0: dec = s[:i] + dec
      if i + len(t) < len(s): dec = dec + s[i+len(t):]
      cando = True
      break
  if cando:
    ans = dec.replace('?', 'a')

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

Submission status: Accepted

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

def solve(S,T):
    ls = len(S)
    lt = len(T)
    S = S[::-1]
    T = T[::-1]
    ans = [S[i] for i in range(ls)]
    # print(ans)
    # 既に含んでいる場合は全てaに変えていい
    if T not in S:    
        for i in range(ls-lt+1):
            for j in range(lt):
                if S[i+j]!=T[j] and S[i+j]!='?':
                    break
            else:
                ans[i:i+lt] = [T[i] for i in range(lt)]
                # print(ans)
                break
        else:
            return 'UNRESTORABLE'
    # print(ans)
    for i in range(ls):
        if ans[i]=='?':
            ans[i] = 'a'
    ans = ''.join(ans)
    return ans[::-1]
print(solve(S,T))

Submission status: Accepted

Solution code:
s = list(input())
t = list(input())
l1 = len(s)
l2 = len(t)
ans = []
for i in range(l1-l2+1):
    s1 = s[:]
    for j in range(l2):
        if s1[i+j] == "?":
            s1[i+j] = t[j]
        elif s1[i+j] != t[j]:
            break
    else:
        s1 = "".join(s1)
        s1 = s1.replace("?","a")
        ans.append(s1)
if ans:
    ans.sort()
    print(ans[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

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


def is_valid(start):
    for i in range(len(t)):
        c = sp[start+i]
        if c != '?' and c != t[i]:
            return False
    return True


def minstr(start):
    end = start + len(t)
    sa = []
    for i, c in enumerate(sp):
        if i >= start and i < end:
            sa.append(t[i - start])
            continue
        if c == '?':
            sa.append('a')
        else:
            sa.append(c)
    return ''.join(sa)


def solve():
    if len(t) > len(sp):
        return False
    start = len(sp) - len(t)
    while start >= 0:
        if is_valid(start):
            print(minstr(start))
            return True
        start -= 1
    return False


if not solve():
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
def is_possible(start_i):
    for i in range(lenT):
        if S[start_i + i] != '?' and S[start_i + i] != T[i]:
            return False
    return True


if __name__ == '__main__':
    S = input()
    T = input()
    lenS = len(S)
    lenT = len(T)
    tmp_i = None
    for i in range(lenS):
        if i + lenT > lenS:
            break
        else:
            if is_possible(i):
                tmp_i = i

    if tmp_i is None:
        print('UNRESTORABLE')
    else:
        print(S[:tmp_i].replace('?', 'a') + T + S[tmp_i + lenT:].replace('?', 'a'))