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:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    r,g = IR(2)
    print(2*g-r)

#B
def B():
    n = I()
    k = I()
    a = 1
    for i in range(n):
        if a > k:
            a += k
        else:
            a *= 2
    print(a)
#C
def C():
    def check(a,b):
        for i in range(len(a)):
            if a[i] != b[i] and a[i] != "?":
                return False
        return True
    def f(c):
        if c == "?":return "a"
        return c
    s = S()
    t = S()
    n = len(s)
    l = len(t)
    ans = []
    for i in range(n-l+1)[::-1]:
        if check(s[i:i+l],t):
            c = [f(s[j]) for j in range(i)]
            c += t
            for j in range(i+l,n):
                c += f(s[j])
            ans.append(c)
    if not len(ans):
        print("UNRESTORABLE")
    else:
        ans.sort()
        for i in ans[0]:
            print(i,end = "")
        print()
#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

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


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

    replaced = False
    for l in range(len(s)-len(t), -1, -1):
        r = l + len(t)
        if match(s[l:r], t):
            replaced = True
            s = s[:l] + t + s[r:]
            break
    if not replaced:
        print("UNRESTORABLE")
    else:
        print(s.replace("?", "a"))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
SS = list(input())
T = list(input())
nt = len(T)
un = 'UNRESTORABLE'
flag = False
for i in reversed(range(len(SS))):
    if i < nt-1:
        break
    flag2 = True
    for j in range(nt):
        if (SS[i-j] == T[nt-1-j] or SS[i-j] == '?') and flag2:
            continue
        else:
            flag2 = False
            break
    if flag2:
        for k in range(nt):
            SS[i+1-nt+k] = T[k]
        flag = True
        break
if flag:
    for s in range(len(SS)):
        if SS[s] == '?':
            SS[s] = 'a'
    print(''.join(SS))
else:
    print(un)

Submission status: Accepted

Solution code:
import sys
write = sys.stdout.write

#BM法をやってみーましょ
Sdash = input()
Slen = len(Sdash)
T = input()

#ord('?') = 63
#ord('a') = 97

#ずらし表の生成
Tlen = len(T)
Pat = [Tlen for i in range(26)]
for i in range(Tlen):
    Pat[ord(T[i]) - 97] = Tlen - (i+1)

#末尾から照合
flg = 0 #探索結果
index = Tlen - 1 #照合開始位置
pos = Tlen - 1 #探索の現在地
kouho = []

while index <= Slen-1:
    if pos == index - Tlen:
        flg = 1
        kouho.append(pos+1) #Tを書き始める位置
        index += 1 #照合開始位置を一つずらして再開
        pos = index
        continue
    if ord(Sdash[pos]) == 63 or Sdash[pos] == T[Tlen - (index - pos) - 1]:
        pos -= 1
        continue
    else:
        move = Pat[ord(Sdash[pos]) - 97]
        if pos + move <= index:
            index += 1
            pos = index
        else:
            index = pos + move
            pos = index
if flg == 1:
    #Sdashで'?'の部分にa, '?'以外の部分にSdashの文字を入れる
    for i in range(kouho[-1]):
        if Sdash[i] == '?':
            write("a")
        else:
            write(Sdash[i])
    write(T)
    for i in range(kouho[-1]+Tlen, Slen):
        if Sdash[i] == '?':
            write("a")
        else:
            write(Sdash[i])
    write("\n")
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

    print(solve(s, t))

def solve(s, t):
    xs = []
    n = len(t)
    for i in range(len(s) - n + 1):
        if match(s[i:i+n], t):
            xs.append(s[:i].replace('?', 'a') + t + s[i+n:].replace('?', 'a'))
    
    if xs:
        return min(xs)

    return 'UNRESTORABLE'

def match(x, y):
    for i, j in zip(x, y):
        if i != '?' and i != j:
            return False
    return True

main()

Submission status: Accepted

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

l = []

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

Submission status: Accepted

Solution code:
def main():
    S = input()
    T = input()
    for i in range(len(S) - len(T), -1, -1):
        ok = True
        for j in range(len(T)):
            if S[i+j] != "?" and S[i+j] != T[j]:
                ok = False
                break
        if ok:
            ans = (S[:i] + T + S[i+len(T):]).replace("?", "a")
            print(ans)
            return
    print("UNRESTORABLE")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = input()
T = input()
X = "UNRESTORABLE"
res = []
for inds, s in enumerate(S):
    if (inds + len(T) > len(S)):
        break
    for indt, t in enumerate(T):
        if S[inds + indt] == t or S[inds+indt] == "?":
            pass
        else:
            break
    else:
        res.append(S[:inds]+T + S[inds + len(T):len(S)])

if (len(res) > 0):
    ans = res[0].replace("?", "a")
    for i in res:
        ans = min(i.replace("?", "a"), ans)
    print(ans)
else:
    print(X)

Submission status: Accepted

Solution code:
S = input()
T = input()
X = "UNRESTORABLE"
ans = ""
x = 0
res = []
for inds, s in enumerate(S):
    if (inds + len(T) > len(S)):
        break
    for indt, t in enumerate(T):
        if S[inds + indt] == t or S[inds+indt] == "?":
            pass
        else:
            break
    else:
        res.append(ans+T + S[inds + len(T):len(S)])
    ans += s

if (len(res) > 0):
    ans = res[0].replace("?", "a")
    for i in res:
        ans = min(i.replace("?", "a"), ans)
    print(ans)
else:
    print(X)

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)):
    if(T[j] == S[i+j] or S[i+j] == "?"):
      continue
    else:
      break
  else:
    ans = S[:i]+T+S[i+len(T):]
    ans = ans.replace("?", "a")
print(ans)

Submission status: Accepted

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

y=0
for i in list(reversed(range(ls-lt+1))):
    L=[]
    for j in range(lt):
        L.append((s[i+j]==t[j] or s[i+j]=='?'))
    if all(L):
        s=list(s)
        s[i:i+lt]=list(t)
        s="".join(s)
        s=s.replace('?','a')
        print(s)
        y=1
        break
if y==0:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()
T = input()
X = "UNRESTORABLE"
ans = ""
x = 0
res = []
for inds, s in enumerate(S):
    if (inds + len(T) > len(S)):
        break
    flag = 0
    for indt, t in enumerate(T):
        if S[inds + indt] == t or S[inds+indt] == "?":
            flag += 1
    if flag == len(T):
        res.append(ans+T + S[inds + len(T):len(S)])
    ans += s
if (len(res) > 0):
    ans = res[0].replace("?", "a")
    for i in res:
        ans = min(i.replace("?", "a"), ans)
    print(ans)
else:
    print(X)

Submission status: Accepted

Solution code:
S = input()
T = input()
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:
        tmp = S[:i] + T + S[i+len(T):]
        ans.append(tmp.replace('?', 'a'))
if ans:
    print(ans[-1])
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

ls = len(s)
lt = len(t)
ANS = []

for i in range(ls) :
    if s[i] == t[0] or s[i] == "?" :
        co = 0
        for k in range(lt) :
            if 0 <= i+k < ls and (s[i+k] == t[k] or s[i+k] == "?") :
                co += 1
        if co == lt :
            ANS.append((s[:i]+t+s[i+lt:]).replace("?","a"))
            
#print(ANS)
ANS.sort()
if len(ANS) > 0 :
    print(ANS[0])
else :
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
SS=input()
T=input()
can=False
for i in reversed(range(len(SS)-len(T)+1)):
    can1=True
    for j in range(len(T)):
        if SS[i+j]==T[j] or SS[i+j]=="?":
            continue
        can1=False
        break
    if can1:
        can=True
        break
if can:
    S=""
    for j in range(len(SS)):
        if i<=j<i+len(T):
            S+=T[j-i]
        elif SS[j]=="?":
            S+="a"
        else:
            S+=SS[j]
    print(S)
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=input()
t=input()
n=len(s)
m=len(t)
ans=-1
for i in range(n-m+1):
    sum=0
    for j in range(m):
        if s[i+j]==t[j] or s[i+j]=='?':
            sum+=1
    if sum==m:
        ans=i
if ans==-1:
    print('UNRESTORABLE')
else:
    s=s[:ans]+t+s[ans+m:]
    b=s.replace('?','a')
    print(b)

Submission status: Accepted

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


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

    for i in reversed(range(len(s) - len(t) + 1)):
        if match(s, t, i):
            s = s[:i] + t + s[i + len(t):]
            print(s.replace("?", "a"))
            return
    print("UNRESTORABLE")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#ABC076C
S=input()
T=input()
for s in range(len(S)-len(T)+1)[::-1]:
	for t in range(len(T)):
		if S[s+t]!='?':
			if S[s+t]!=T[t]:
				break
	else:
		ans=''
		for ss in S[:s]:
			if ss=='?':
				ans+='a'
			else:
				ans+=ss
		ans+=T
		for ss in S[s+len(T):]:
			if ss=='?':
				ans+='a'
			else:
				ans+=ss
		print(ans)
		exit()
print('UNRESTORABLE')

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 + len(t):]).replace("?", "a"))
            exit()
        if t_kamo[j] == "?":
            continue
        elif t_kamo[j] != t[j]:
            break
print("UNRESTORABLE")

Submission status: Accepted

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

ls = len(s)
lt = len(t)
Ans = []
for i in range(ls - lt + 1):
    ss = s[i:i+lt]
    lss = len(ss)
    cnt = 0
    for j in range(lss):
        if s[i + j] == '?' or t[j] == ss[j]:
            cnt += 1
        else:
            break
    else:
        if cnt == lss:
            ans = ''
            for j in range(i):
                if s[j] == '?':
                    ans += 'a'
                else:
                    ans += s[j]
            ans += t
            for j in range(i + lt, ls):
                if s[j] == '?':
                   ans += 'a'
                else:
                    ans += s[j]
            else:
                Ans.append(ans)
if len(Ans) == 0:
    print('UNRESTORABLE')
else:
    print(sorted(Ans)[0])

Submission status: Accepted

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

flag = 0
ans = 'UNRESTORABLE'
for i in range(n - m + 1):
    for j in range(m):
        if S[i + j] != '?' and S[i + j] != T[j]:
            break
    else:
        S2 = S[: i] + T + S[i + m :]
        ans = S2.replace('?', 'a')
        
print(ans)

Submission status: Accepted

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

flag = 0
ans = 'UNRESTORABLE'
for i in range(n - m + 1):
    for j in range(m):
        if S[i + j] != '?' and S[i + j] != T[j]:
            break
    else:
        S2 = S[: i] + T + S[i + m :]
        flag = 1
if flag:
    ans = S2.replace('?', 'a')
        
print(ans)

Submission status: Accepted

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


tmp = -1
for i in range(len(S) - len(T) + 1):
    start = -1
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == '?':
            start = i
        else:
            start = -1
            break
                       
    if start != -1:
                tmp = i
if tmp == -1:
    print('UNRESTORABLE')
    
else:
    res = ''
    count = 0
    for i in range(len(S)):
        if tmp <= i < tmp + len(T):
            res += T[count]
            count += 1
            
        elif S[i] == '?':
            res += 'a'
            
        else:
            res += S[i]

    print(res)

Submission status: Accepted

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


    
if True:
    tmp = -1
    for i in range(len(S) - len(T) + 1):
        start = -1
        for j in range(len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                start = i
            else:
                start = -1
                break
                
        
        if start != -1:
                tmp = i
    if tmp == -1:
        print('UNRESTORABLE')
    else:
        res = ''
        count = 0
        for i in range(len(S)):
            if tmp <= i < tmp + len(T):
                res += T[count]
                count += 1
            elif S[i] == '?':
                res += 'a'
            else:
                res += S[i]

        print(res)

Submission status: Accepted

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

flag = False
for i in range(len(S)-len(T),-1,-1):
    count = 0
    for j in range(len(T)):
        #print(T[j],S[i:i+len(T)][j])
        if T[j] == S[i+j] or S[i+j] == '?':
            count += 1
        else:
            break
    
    if count == len(T):
        flag = True
        for j in range(len(T)):
            S[i+j] = T[j]
        break

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

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

print("".join(S))

Submission status: Accepted

Solution code:
#076-C
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
s=S()
t=S()
rest=False
ii=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:
        rest=True
        ii=i
if rest:
    for j in range(len(t)):
        s[ii+j]=t[j]
    for i in range(len(s)):
        if s[i]=="?":
            s[i]="a"
    for i in range(len(s)):
        print(s[i],end="")
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

ind = 0
def search(ind, target, source, target_pos):
    if target_pos + ind >= len(target) and ind < len(source):
        return ""
    if ind == len(source):
        if target_pos + len(source) <= len(target):
            return target[:target_pos] + source + target[target_pos+len(source):].replace("?","a")
        else:
            return target[:target_pos] + source
    if target[target_pos + ind] == source[ind] or target[target_pos + ind] == "?":
        result = search(ind+1, target, source, target_pos)
    else:
        target = target[:target_pos].replace("?","a")+target[target_pos:]
        result = search(0, target, source, target_pos+1)
    return result

best = ""
for i in range(len(T)):
    result = search(0, S[::-1], T[::-1], i)[::-1].replace("?","a")
    if result != "" and (best == "" or best > result):
        best = result
if best == "":
    print("UNRESTORABLE")
else:
    print(best)

Submission status: Accepted

Solution code:
S=input()
T=input()
s=0
flag1=False
for i in range(len(S)-len(T)+1):
    flag2=True
    for j in range(len(T)):
        if S[i+j]!=T[j] and S[i+j]!='?':
            flag2=False
    if flag2==True:
        flag1=True
        s=i
if flag1==False:
    print('UNRESTORABLE')
else:
    for i in range(len(S)):
        if i>=s and i-s<len(T):
            print(T[i-s],end='')
        elif S[i]=='?':
            print('a',end='')
        else:
            print(S[i],end='')

Submission status: Accepted

Solution code:
# N,M = map(int, input().split())
S = input()
T = input()
N = len(S)

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


cand = []
for i in range(N):

    j = i
    ti = 0

    # print(T,S, "j: ", j, "ti: ", ti)
    while ti < len(T) and j < len(S) and (S[j] == "?" or S[j] == T[ti]):
        ti += 1; j += 1
        # print("\t[while] ti: ", ti, "j: ", j)

    if ti == len(T):
        cand.append( S[0:i] + T + S[i +  len(T):]  )
        
    else:
        continue

if len(cand) == 0:
    print("UNRESTORABLE")
else:
    ans = restore(cand[-1])
    print(ans)

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]


from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

s = input()
t = input()

if len(s) >= len(t):
    ans_l = []
    for i in range(0, len(s)-len(t)+1):
        s_s = s[i:len(s)-1+i]
        flag = True
        for ssi, ti in zip(s_s, t):
            if ssi == "?":
                continue
            if ssi != ti:
                flag = False
                break
        if flag:
            ans_l.append(i)

    d = []
    for ans_i in ans_l:
        tmp_s = (s[:ans_i]+t+s[len(t)+ans_i:]).replace("?", "a")
        d.append(tmp_s)
    d = sorted(d)
    if len(d) > 0:
        print(d[0])
    else:
        print("UNRESTORABLE")
else:
    print("UNRESTORABLE")

Submission status: Accepted

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


def main():
    s_dash = input()
    t = input()
    s_dash_count = len(s_dash)
    t_count = len(t)
    ans = list()

    if s_dash_count >= t_count:
        for k in range(s_dash_count - t_count + 1):
            mod_s_dash = s_dash[k:k + t_count]
            count = 0

            for i in range(t_count):
                if mod_s_dash[i] == t[i]:
                    count += 1
                elif mod_s_dash[i] == '?':
                    count += 1

            if count == t_count:
                s = s_dash[:k] + t + s_dash[k + t_count:]
                s = s.replace('?', 'a')
                ans.append(s)

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

    else:
        print('UNRESTORABLE')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
import numpy as np
import sys

def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
t = S()

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

Submission status: Accepted

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

for s_index in range(len(s)-len(t)+1):
    if s[s_index] in ['?', t[0]]:
        s_p = s[s_index:s_index+len(t)]
        if all(s_p[x] in ['?', t[x]] for x in range(len(s_p))):
            res.append(s[:s_index].replace('?', 'a') + t + s[s_index+len(t):].replace('?', 'a'))
if res:
    print(sorted(res)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

res = []

for s_index in range(len(s)-len(t)+1):
    if s[s_index] in ['?', t[0]]:
        s_p = s[s_index:s_index+len(t)]
        if len([True for x in range(len(s_p)) if s_p[x] in ['?', t[x]]]) == len(t):
            res.append(s[:s_index].replace('?', 'a') + t + s[s_index+len(t):].replace('?', 'a'))

if res:
    print(sorted(res)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
#ABC076C
s=input()
t=input()
can=[]
for i in range(1+len(s)-len(t)):
    check=0
    for j in range(len(t)):
        if s[i+j]!=t[j] and s[i+j]!='?':
            check=1
            break
    if check==0:can.append(i)
if len(can)==0:print('UNRESTORABLE')
else:
    ans=can[-1]
    res=s[:ans]+t+s[ans+len(t):]
    print(res.replace('?','a'))

Submission status: Accepted

Solution code:
sd = input()
t = input()
sdl = len(sd)
tl = len(t)
for i in range(tl, sdl+1)[::-1]:
    f = False
    for j in range(tl):
        if sd[i-tl+j] != t[j] and sd[i-tl+j] != "?":
            f = True
    if not f:
        res = sd[:i-tl] + t + sd[i-tl+j+1:]
        res = res.replace("?", "a")
        print(res)
        exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
def match(a, b):
    for i, j in zip(a, b):
        if i != '?' and i != j:
            return False
    return True

def solve(r, t):
    l = len(t)
    m = len(r)
    for i in range(m - l, -1, -1):
        if match(r[i:i+l], t):
            ans = ''
            for j in range(i):
                if r[j] == '?':
                    ans = ans + 'a'
                else:
                    ans = ans + r[j]
            for j in range(l):
                ans = ans + t[j]
            for j in range(i + l, m):
                if r[j] == '?':
                    ans = ans + 'a'
                else:
                    ans = ans + r[j]
            return ans
    return 'UNRESTORABLE'

R = input()
T = input()
print(solve(R, T))

Submission status: Accepted

Solution code:
ss = input()
t = input()
e = len(ss)
f = len(t)
d = []
try:
    for i in range(e-f+1):
        b = 1
        for j in range(f):
            if ss[i+j] != t[j] and ss[i+j] != '?':
                b = 0
        if b:
            s = ss[:i]+t+ss[i+f:]
            d.append(s.replace('?', 'a'))
    d.sort()
    print(d[0])

except:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
n = 0
ans = ''
flag = False
cnt = 0
for i in range(len(S)-len(T)+1):
  for j in range(len(T)):
    if S[len(S)-i-j-1] == T[len(T)-j-1] or S[len(S)-i-j-1] == '?':
      cnt += 1
  if cnt == len(T):
    n = i
    flag = True
    break
  else:
    cnt = 0
if not flag:
  print('UNRESTORABLE')
else:
  S = S.replace('?','a')
  ans = S[0:len(S)-len(T)-i]+T+S[len(S)-i:len(S)]
print(ans)

Submission status: Accepted

Solution code:
S = input()
T = input()
ls = len(S)
lt = len(T)
cand = []
for i in range(ls-lt+1):
    frac =  S[i:i+lt]
    if all(map(lambda x:x[0]==x[1] or x[0]=='?',zip(frac, T))):
        cand.append(S[0:i]+T+S[i+lt:ls])
if cand == []:
    print("UNRESTORABLE")
else:
    cand = list(map(lambda x:x.replace('?','a'), cand))
    print(sorted(cand)[0])

Submission status: Accepted

Solution code:
S = input()
T = input()
ls = len(S)
lt = len(T)
cand = []
for i in range(ls-lt+1):
    frac =  S[i:i+lt]
    for j in range(lt):
        if frac[j] != T[j] and frac[j] != '?': break
    else:
        cand.append(S[0:i] + T + S[i+lt:ls])
if cand == []:
    print("UNRESTORABLE")
else:
    cand = list(map(lambda x:x.replace('?','a'), cand))
    cand.sort()
    print(cand[0])

Submission status: Accepted

Solution code:
try:
    S = list(input())
    T = list(input())
    t = len(T)
    candidate = []
    for i in range(len(S)):
        tmp = S[i:i+t]
        for j in range(len(tmp)):
            if tmp[j] == T[j] or '?' == tmp[j]:
                pass
            else:
                break
        else:
            candidate.append(S[0:i]+T+S[i+t:len(S)])
    for i in range(len(candidate)):
        l = list(candidate[i])
        for j in range(len(l)):
            if l[j] == '?':
                l[j] = 'a'
        candidate[i]=l
    candidate = list(filter(lambda x:len(x)==len(S),candidate))
    candidate.sort()
    for x in candidate[0]:
        print(x,end='')
    print()
except:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
s = len(S)
t = len(T)
ans = "UNRESTORABLE"
for i in range(s-t+1):
  for j in range(t):
    T[j],S[i+j]
    if(T[j] == S[i+j] or S[i+j] == '?'):
      continue
    else:
      break
  else:
    ans = S[:i] + T + S[i+t:]
    ans = ans.replace('?', 'a')
print(ans)

Submission status: Accepted

Solution code:
def same(s,t):
    for a,b in zip(s,t):
        if not (a==b or a=='?'):
            return False
    return True

s=input()
t=input()
if len(s)<len(t):
    print('UNRESTORABLE')
    exit()
elif len(s)==len(t):
    if same(s,t):
        print(t)
        exit()
for i in range(len(t),len(s)):
    i=len(s)-i
    if same(s[i:i+len(t)],t):
        s=s[:i]+t+s[i+len(t):]
        break
else:
    print('UNRESTORABLE')
    exit()
for c in s:
    print(c if c!='?'else 'a',end='')
print()

Submission status: Accepted

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

ans = 'UNRESTORABLE'
for i in range(n - m + 1):
    for j in range(m):
        if S[i + j] == T[j] or S[i + j] == '?':
            continue
        else:
            break
    else:
        x = S[: i] + T + S[i + m:]
        ans = x.replace('?', 'a')
        
print(ans)

Submission status: Accepted

Solution code:
from copy import deepcopy

s = list(input())
t = list(input())
st=[]

for i in range(len(s)-len(t)+1):
    l=deepcopy(s[len(s)-len(t)-i:len(s)-i])
    for j in range(len(t)):
        if l[j] ==  '?':
            l[j]=t[j]
    if l==t:
        u=deepcopy(s)
        u[len(u)-len(t)-i:len(u)-i]=t
        st.append(''.join(u).replace('?','a'))

if len(st) == 1:
    print(st[0])
elif len(st)>1:
    ans = st[0]
    for i in range(1,len(st)):
        if ans > st[i]:
            ans = st[i]
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

ans = 'UNRESTORABLE'
for i in range(n - m + 1):
    for j in range(m):
        if T[j] == S[i + j] or S[i + j] == '?':
            continue
        else:
            break
    else:
        ans = S[0 : i] + T + S[i + m :]
        ans = ans.replace('?', 'a')

print(ans)

Submission status: Accepted

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

ans = 'UNRESTORABLE'
for i in range(n - m + 1):
    flag = 1
    for j in range(m):
        if T[j] == S[i + j] or S[i + j] == '?':
            continue
        else:
            flag = 0
    if flag:
        ans = S[0 : i] + T + S[i + m :]
        ans = ans.replace('?', 'a')

print(ans)

Submission status: Accepted

Solution code:
s = list(str(input()))
t = list(str(input()))
ans = []
an = 0
if len(s) < len(t):
    print("UNRESTORABLE")
    exit()
for i in range(len(s)-len(t)+1):
    cou = s[i:i+len(t)]
    for k in range(len(t)):
        if s[i+k] != t[k] and s[i+k] != "?" and t[k] != "?":
            break
        if k == len(t)-1:
            ans.append(s[:i]+t+s[i+len(t):])
for h in range(len(ans)):
    for f in range(len(s)):
        if ans[h][f] == "?":ans[h][f] = "a"
    ans[h] = "".join(ans[h])
if ans == []:
    print("UNRESTORABLE")
    exit()
ans.sort()
print(ans[0])

Submission status: Accepted

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

len_S = len(S)
len_T = len(T)
for i in range(len_S-len_T, -1, -1):
  if not all(s in ['?', t] for s, t in zip(S[i:i+len_T], T)):
    continue
  print((S[:i] + T + S[i+len_T:]).replace('?','a'))
  exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
# https://abc076.contest.atcoder.jp/tasks/abc076_c

Sd = input()
T = input()

is_replaced = False
for i in range(len(Sd) - len(T), -1, -1):
    can_replace = True
    k = i
    j = 0
    while can_replace and j < len(T):
        if Sd[k] == T[j] or Sd[k] == "?":
            k += 1
            j += 1
        else:
            can_replace = False
            break

    if can_replace:
        # let's replace
        Sd_l = list(Sd)
        for ti in range(len(T)):
            Sd_l[i + ti] = T[ti]
        Sd = "".join(Sd_l)
        is_replaced = True
        break

if is_replaced:
    print(Sd.replace('?', 'a'))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=input()
p=input()
c=0
if len(s)<len(p):
    print("UNRESTORABLE")
else:
    for i in range(len(s)-len(p),-1,-1):
        if c!=0:
            break
        d=s[i:i+len(p)]
        for j in range(len(p)):
            if d[j]!="?":
                if d[j]!=p[j]:
                    break
        else:
            s=s[:i]+p+s[i+len(p):]
            c=1
    if c!=1:
        print("UNRESTORABLE")
    else:
        for i in range(len(s)):
            if s[i]=="?":
                s=s[:i]+"a"+s[i+1:]
        print(s)

Submission status: Accepted

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

Sd = input()
T = input()
lSd, lT = len(Sd), len(T)

found = []
for i in range(lSd - lT + 1):
    subFound = True
    for j in range(lT):
        if not (Sd[i + j] == T[j] or Sd[i + j] == '?'):
            subFound = False
    if subFound:
        found.append((Sd[0:i] + T + Sd[i + lT:]).replace('?', 'a'))

found.sort()
if len(found) == 0:
    S = "UNRESTORABLE"
else:
    S = found[0]

print(S)

Submission status: Accepted

Solution code:
S = input()
T = input()
lenS = len(S)
lenT = len(T)
res = []
for i in range(lenS - lenT + 1):
    tgt = S[i:i + lenT]
    for a, b in zip(tgt, T):
        if not (a == b or a == '?'):
            break
    else:
        tmp = S[:i] + T + S[i + lenT:]
        res.append(tmp.replace('?', 'a'))
if res:
    print(min(res))
else:
    print('UNRESTORABLE')