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: Runtime Error

Solution code:
words = input()
key = input()
for i in range(len(words)):
    if words[i:i+len(key)] == key:
         words[i:i+len(key)] = key
         index = i
else:
    print('UNRESTORABLE')

Submission status: Runtime Error

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

for i in range(ls):
    nows = s_[ls-i-lt:ls-i]
    if len(nows) < lt:
        break
    isSame = True
    for j in range(lt):
        if nows[j] == "?" or nows[j] == t[j]:
            continue
        else:
            isSame = False
            break
    if isSame:
        res = s_[:ls-i-lt] + t + s_[ls-i:]
        res = res.replace("?", "a")
        print(res)
        break
if not isSame:
    print("UNRESTORABLE")

Submission status: Runtime Error

Solution code:
from collections import defaultdict

Ss = input()
T =  input()

Tdic = defaultdict(lambda: len(T))

for idx,i in  enumerate(T):
    Tdic[i] = len(T) -(idx+1)
    
Tdic[T[-1]] = len(T)
Tdic["?"] = 1
    
Flag = False
mutchdic = dict()
mutch = 0
head = len(T)-1
          
while(1):
    for i in range(len(T)):

        if T[-(i+1)] == Ss[head-i]:
            mutch += 1
            if i+1 == len(T):
                mutchdic[head] = mutch
                head += len(T)
                mutch = 0
                Flag = True
        elif T[-(i+1)] == "?":
            if i+1 == len(T):
                mutchdic[head] = mutch
                head += 1
                mutch = 0
                Flag = True
        else:
            mutchdic[head] = mutch
            head += Tdic[Ss[head-i]]+1-i
    if head > len(Ss)-1:
        break

if Flag:
    SsL = list(Ss)
    max_head = max(mutchdic.items(), key=lambda x:x[1])[0]
    for i in range(len(T)):
        SsL[max_head-i] = T[-(i+1)]
        SsLstr = "".join(SsL)
        SsLstr=SsLstr.replace("?","a")
        ans = SsLstr
else:
    ans = "UNRESTORABLE"

print(ans)

Submission status: Runtime Error

Solution code:
S_raw = input()
T_raw = input()

S = list(S_raw)
T = list(T_raw)

i = 0
j = 0

j_list = []
S_list = []

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

while True:
	if j == ls or i == lt:
		if not i == lt:
			print("UNRESTORABLE")
			exit(0)
		elif j != ls:
			j_list.append(j)
			i = 0
			j = j - lt + 2
		else:
			j_list.append(j)
			break

	if T[i] == S[j]:
		i += 1
		j += 1
	elif S[j] == '?':
		i += 1
		j += 1
	else:
		i = 0
		j += 1


for j in j_list:
	for i,c in zip(range(j-lt,j),T):
		S[i] = c

	S_tmp = ''

	for i in range(ls):
		if S[i] == '?':
			S_tmp += 'a'
		else:
			S_tmp += str(S[i])

	S_list.append(S_tmp)

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

Submission status: Runtime Error

Solution code:
import sys

S = sys.stdin.readline().rstrip()
T = sys.stdin.readline().rstrip()

def checkValid(S, i, T):
    if S[i] == T[0]:
        for j in range(0, len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                pass
            else:
                return False
    else:
        return False
    return True

def getResult(S, i, T):
    res = ""
    for k in range(0, len(S)):
        if k < i:
            res = res + S[k]
        elif k >= i and k < i+len(T):
            res = res + T[k-i]
        else:
            res = res + S[k]
    S = res.replace('?', 'a')
    return S

res_list = []

res = False
for i in range(0, len(S)):
    if S[i] != '?':
        if checkValid(S, i, T) == True:
            res_list.append(getResult(S, i, T))
            res = True

if res == False:
    print("UNRESTORABLE")
else:
    smallest = res_list[0]
    for i in range(0, len(res_list)):
        if smallest > res_list[i]:
            smallest = res_list[i]
    print(smallest)

Submission status: Runtime Error

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

n=len(s_)
m=len(t)
ind=0
dic={}
for i in range(n-m+1):
	con1=True
	for j in range(m):
		if s_[i+j] !=t[j] and s_[i+j]!='?':
			con1=False
			dic.clear()
			break
		else:
			dic[i+j]=t[j]
if con1:
	lis=list(s_)
	if len(dic)!=0:
		for key in dic:
			lis[key]=dic[key]
		lis=''.join(lis)
	lis=lis.replace('?','a')
	print(lis)
else:
	print('UNRESTORABLE')

Submission status: Runtime Error

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

s = input()
t = input()
ansstr = []

for i in range(len(s)-len(t)+1)[::-1]:
    tmpstr = s[i:i+len(t)]
    flg = True
    for j in range(len(t)):
        if(tmpstr[j] == '?'):
            pass
        elif(tmpstr[j] == t[j]):
            pass
        else:
            flg = False
    if(flg):
        uset = list(range(i,i+len(t)))
        for k in range(len(s)):
            if(k in uset):
                ansstr += t[k-i]
            elif(s[k]=='?'):
                ansstr += 'a'
            else:
                ansstr += s[k]
        break
print(''.join(ansstr) if flg else 'UNRESTORABLE')

Submission status: Runtime Error

Solution code:
import sys

S = sys.stdin.readline()
T = sys.stdin.readline()


def checkValid(S, i, T):
    if S[i] == T[0]:
        for j in range(0, len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                pass
            else:
                return False
    else:
        return False
    return True

def getResult(S, i, T):
    res = ""
    for k in range(0, len(S)):
        if k < i:
            res = res + S[k]
        elif k >= i and k < i+len(T):
            res = res + T[k-i]
        else:
            res = res + S[k]
    S = res.replace('?', 'a')
    return S

res_list = []

res = False
for i in range(0, len(S)):
    if S[i] != '?':
        if checkValid(S, i, T) == True:
            res_list.append(getResult(S, i, T))
            res = True

if res == False:
    print("UNRESTORABLE")
else:
    smallest = res_list[0]
    for i in len(res_list):
        if smallest > res_list[i]:
            smallest = res_list[i]
    print(smallest)

Submission status: Runtime Error

Solution code:
s = input()
t = input()
s = s[::-1]
t = t[::-1]
import re
s2 = s.replace("?","9")
temp = ""
for i in t:
    temp += "("+i+"|9)"
M = re.search(temp,s2)
temp = []
if M:
    c = 0
    while(c<len(s)):
        print(temp,c)
        if M.start()<=c and c < M.start() + len(t):
            temp.append(t[c])
            c+=1
            continue
        temp.append(s[c])
        c += 1
    print(M.start())
    print("".join(temp).replace("?","a")[::-1])
else:
    print("UNRESTORABLE")

Submission status: Runtime Error

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

n=len(s_)
m=len(t)
ind=0
dic={}
for i in range(n-m+1):
	con1=True
	for j in range(m):
		if s_[i+j] !=t[j] and s_[i+j]!='?':
			con1=False
			dic.clear()
			break
		else:
			dic[i+j]=t[j]
if con1:
	lis=list(s_)
	for key in dic:
		lis[key]=dic[key]
	lis=''.join(lis)
	lis=lis.replace('?','a')
	print(lis)
else:
	print('UNRESTORABLE')

Submission status: Runtime Error

Solution code:
S = input()
T = input()
rec = []
for i in range(len(S)):
    flag = True
    Z = S
    for j in range(len(T)):
        if Z[i] == T[j]:
            for e in range(i - j):
                if Z[e] == "?":
                    Z = Z.replace("?", "a", 1)
            for k in range(len(T)):
                if Z[i + k - j] == T[k]:
                    continue
                elif Z[i + k - j] == "?":
                    Z = Z.replace("?", T[k], 1)
                else:
                    flag = False
            if flag:
                Z = Z.replace("?", "a")
                rec.append(Z)
        elif Z[i] == "?":
            if len(S)-i < len(T):
                flag = False
                break
            for e in range(i):
                if Z[e] == "?":
                    Z = Z.replace("?", "a", 1)
            for k in range(len(T)):
                if Z[i + k] == T[k]:
                    continue
                elif Z[i + k] == "?":
                    Z = Z.replace("?", T[k], 1)
                else:
                    flag = False
            if flag:
                Z = Z.replace("?", "a")
                rec.append(Z)

if rec == []:
    print("UNRESTORABLE")
else:
    print(rec[-1])

Submission status: Runtime Error

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

s = input()
t = input()
ansstr = []

for i in range(len(s)-len(t)+1):
    p = len(s)-len(t)-i
    tmpstr = s[p:p+len(t)]
    flg = True
    for j in range(len(t)):
        if(tmpstr[j] == '?'):
            pass
        elif(tmpstr[j] == t[j]):
            pass
        else:
            flg = False
    if(flg):
        for k in range(len(s)):
            if(p<=k and k<p+len(t)):
                ansstr += t[k-p]
            elif(s[k]=='?'):
                ansstr += 'a'
            else:
                ansstr += s[k]
        break
print(''.join(ansstr) if flg else 'UNRESTORABLE')

Submission status: Runtime Error

Solution code:
S = input().strip()
T = input().strip()
rS = []
for i in range(len(S)):
    rS.append(S[i])
for i in range(len(rS) - len(T)):
    eq = True
    for j in range(len(T)):
        if not(rS[- i - j - 1] == T[- j - 1] or rS[- i - j - 1] == "?"):
            eq = not(eq)
            break
    if eq:
        for j in range(len(T)):
            rS[- i - j - 1] = T[- j - 1]
        break
if eq:
    for i in range(len(rS)):
        if rS[i] == "?":
            rS[i] = "a"
    for i in range(len(rS)):
        print(rS[i], end='')
    print("")
else:
    print("UNRESTORABLE")

Submission status: Runtime Error

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

p=re.compile(s_.replace("?","."))
t_s=len(s_)-len(t)
t_e=t_s+len(t)
for t_s in range(t_s,-1,-1):
    s=s_[:t_s]+t+s_[t_e:]
    if(p.match(s)):
        s=s.replace("?","a")
        print(s)
        break
else:
    print("UNRESTORABLE")

Submission status: Runtime Error

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


t_s=len(s_)-len(t)
t_e=t_s+len(t)
for t_s in range(t_s,-1,-1):
    s=s_[:t_s]+t+s_[t_e:]
    if(re.match(s_.replace("?","."),s)):
        s=s.replace("?","a")
        print(s)
        break
else:
    print("UNRESTORABLE")

Submission status: Runtime Error

Solution code:
S = input()
T = input()
rec = []
for i in range(len(S)):
    flag = True
    Z = S
    for j in range(len(T)):
        if Z[i] == T[j]:
            for e in range(i - j):
                if Z[e] == "?":
                    Z = Z.replace("?", "a", 1)
            for k in range(len(T)):
                if Z[i + k - j] == T[k]:
                    continue
                elif Z[i + k - j] == "?":
                    Z = Z.replace("?", T[k], 1)
                else:
                    flag = False
            if flag:
                Z = Z.replace("?", "a")
                rec.append(Z)
if rec == []:
    print("UNRESTORABLE")
else:
    print(rec[-1])

Submission status: Runtime Error

Solution code:
S = input()
T = input()
rec = []
for i in range(len(S)):
    flag = True
    Z = S
    for j in range(len(T)):
        if Z[i] == T[j]:
            for e in range(i - j):
                if Z[e] == "?":
                    Z = Z.replace("?", "a", 1)
            for k in range(len(T)):
                if Z[i + k - j] == T[k]:
                    continue
                elif Z[i + k - j] == "?":
                    Z = Z.replace("?", T[k], 1)
                    print(Z)
                else:
                    flag = False
            if flag:
                Z = Z.replace("?", "a")
                rec.append(Z)
if rec == []:
    print("UNRESTORABLE")
else:
    print(rec[-1])

Submission status: Runtime Error

Solution code:
S = input().strip()
T = input().strip()
rS = []
for i in range(len(S)):
    rS.append(S[i])
for i in range(len(rS) - len(T), -1, -1):
    eq = True
    for j in range(len(T)):
        if not(rS[i + j] == T[j] or rS[i + j] == "?"):
            eq = not(eq)
            break
    if eq:
        for j in range(len(T)):
            rS[i + j] = T[j]
        break
if eq:
    for i in range(len(rS)):
        if rS[i] == "?":
            rS[i] = "a"
    for i in range(len(rS)):
        print(rS[i], end='')
    print("")
else:
    print("UNRESTORABLE")

Submission status: Runtime Error

Solution code:
S = input()
T = input()
rec = []
for i in range(len(S)):
    flag = True
    Z = S
    for j in range(len(T)):
        if Z[i] == T[j]:
            for e in range(i - j):
                if Z[e] == "?":
                    Z = Z.replace("?", "a", 1)
            print(Z[i])
            print(T[j])
            for k in range(len(T)):
                if Z[i + k - j] == T[k]:
                    continue
                elif Z[i + k - j] == "?":

                    Z = Z.replace("?", T[k], 1)
                    print(Z)
                else:
                    flag = False
            if flag:
                Z = Z.replace("?", "a")
                rec.append(Z)
if rec == []:
    print("UNRESTORABLE")
else:
    print(rec[-1])

Submission status: Runtime Error

Solution code:
S = input()
T = input()
rec = []
for i in range(len(S)):
    flag = True
    Z = S
    for j in range(len(T)):
        if Z[i] == T[j]:
            for e in range(i-j):
                if Z[e] == "?":
                    Z = Z.replace("?","a",1)
            print(Z[i])
            print(T[j])
            for k in range(len(T)-j+1):
                if Z[i+k-j] == T[k]:
                    continue
                elif Z[i+k-j] == "?":

                    Z = Z.replace("?",T[k],1)
                    print(Z)
                else:
                    flag = False
            if flag:
                Z = Z.replace("?", "a")
                rec.append(Z)
if rec == []:
    print("UNRESTORABLE")
else:
    print(rec[-1])

Submission status: Runtime Error

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

filled_idx=[i for i in range(len(s_)) if s_[i]!="?"]

t_s=len(s_)-len(t)
t_e=t_s+len(t)
for t_s in range(t_s,-1,-1):
    s=s_[:t_s]+t+s_[t_e:]
    if(all(s[i]==s_[i] for i in filled_idx)):
        s=s.replace("?","a")
        print(s)
        break
else:
    print("UNRESTORABLE")

Submission status: Runtime Error

Solution code:
import sys
s = input()
t = input()
l = len(t)
if t in s:
    print(s.replace('?', 'a'))
    sys.exit()
for i in range(len(s) - l + 1):
    m = -1
    f = True
    for j in range(l):
        if s[i:i+l][j] == '?' or s[i:i+l][j] == t[j]:
            pass
        else:
            f = False
    if f:
        m = i
if m < 0:
    print('UNRESTORABLE')
else:
    print((s[0:m] + t + s[m+l:]).replace('?', 'a'))

Submission status: Runtime Error

Solution code:
from IPython import embed

S_prime = input()
T = input()
replaced = 'UNRESTORABLE'
matched = True

for i in range(len(S_prime) - len(T) + 1):
    for k in range(len(T)):
        if S_prime[i + k] != T[k] and S_prime[i + k] != '?':
            matched = False
            break
        matched = True
    if matched:
        print(S_prime[0:i])
        replaced = S_prime[0:i] + T + S_prime[i + len(T):]
        break

print(replaced.replace('?', 'a'))

Submission status: Runtime Error

Solution code:
Sd_ = input()
T_ = input()
Sd = []
T = []
S = []
check = False
for i in range(len(Sd_)):
    Sd.append(Sd_[i])
for i in range(len(T_)):
    T.append(T_[i])
num = 0
for a in range(len(Sd)-1,0,-1):
    if Sd[a] is '?':
        num += 1
    else:
        num = 0
    if num >= len(T):
        for m in range(len(T)):
            Sd[a+m] = T[m]
        for s in range(len(Sd)):
            if Sd[s] is '?':
                S.append('a')
            else:
                S.append(Sd[s])
        print(''.join(S))
        exit()


for i in range(len(Sd)-1,0,-1):
    if Sd[i] is not '?':
        if Sd[i] in T_:
            check = True
            num = T_.find(Sd[i])
            for n in range(len(T_)):
                if n < num:
                    if Sd[i-num+n] is not '?':
                        check = False
                        break
                else:
                    if Sd[i-num+n] is not T_[n] and Sd[i-num+n] is not '?':
                        check = False
                        break
            if check:
                for m in range(len(T)):
                    Sd[i-num+m] = T[m]
                break

if check:
    for s in range(len(Sd)):
        if Sd[s] is '?':
            S.append('a')
        else:
            S.append(Sd[s])
    print(''.join(S))
else:
    print('UNRESTORABLE')

Submission status: Runtime Error

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

if len(S1) < T:
    print("UNRESTORABLE")
    quit()

for i in range(len(S1)-len(T)+1):
    flag1 = True
    flag2 = True
    for s,t in zip(S1[i:i+len(T)],T):
        if s == "?" or s == t:
            pass
        else:
            flag1 = False 
            break
    if flag1:
        flag2 = False
        S1 = S1.replace(S1[i:i+len(T)],T)
        S = ["a" if s1 == "?" else s1 for s1 in S1]
        print("".join(S))
        break
if flag2:
    print("UNRESTORABLE")

Submission status: Runtime Error

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

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

not_find = False
start_i = 0
for i in range(s_l - t_l + 1):
    start_i = s_l - i - t_l
    here = True
    for j in range(t_l):
        if S[start_i+j] == T[j] or S[start_i+j] == '?':
            here = True
        else:
            here = False
            break
    if here:
        break
    if start_i == 0:
        not_find = True

if not_find:
    print("UNRESTORABLE")
else:
    for s in range(len(S[:start_i])):
        if S[s] == '?':
            S[s] = 'a'

    S[start_i:start_i+t_l] = T

    for s in range(len(S[start_i+t_l:])):
        if S[s] == '?':
            S[s] = 'a'
    print(S)

Submission status: Runtime Error

Solution code:
import sys

def match(a, b):
    for i in range(len(a)):
        if a[i] != '?' and b[i] != '?' and a[i] != b[i]:
            return False
    return True


Y=str(input())
T=str(input())
S = ''
if match(T,Y[-len(T):]):
    S = Y[:-len(T)] + T
    A = S.replace('?','a')
    print(A)
    sys.exit()
else:
    for i in range(len(Y) - len(T)):
        i += 1
        k = -(len(T)+i)
        if match(T, Y[k:-i]):
            S = Y[:k] + T + Y[-i:]
            A = S.replace('?','a')
            print(A)
            sys.exit()

print('UNRESTORABLE')

Submission status: Runtime Error

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


for i in range(len(S1)-len(T)+1):
    flag1 = True
    flag2 = True
    for s,t in zip(S1[i:i+len(T)],T):
        if s == "?" or s == t:
            pass
        else:
            flag1 = False 
            break
    if flag1:
        flag2 = False
        S1 = S1.replace(S1[i:i+len(T)],T)
        S = ["a" if s1 == "?" else s1 for s1 in S1]
        print("".join(S))
        break
if flag2:
    print("UNRESTORABLE")

Submission status: Runtime Error

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

flag = 0
count = 0
for i in range(len(s)):
    if s[i] == '?' or s[i] in t:
        if count == 0:
            start = i
        count += 1
    else:
        if count >= len(t):
            for j in range(count - len(t) + 1):
                check = 0
                for k in range(len(t)):
                    if s[start + j + k] != t[j + k] and s[start + j + k] != '?':
                        check = 1
                        break
                if check == 0:
                    s = s[:start] + t + s[start + len(t) + 1:]
                    flag = 1
                break
        count = 0

if count >= len(t):
    for j in range(count-len(t)+1):
        check = 0
        for k in range(len(t)):
            if s[start+j+k] != t[j+k] and s[start+j+k] != '?':
                check = 1
                break
        if check == 0:
            s = s[:start] + t + s[start+len(t)+1:]
            flag = 1
        break

if flag == 0:
    print('UNRESTORABLE')
else:
    for i in range(len(s)):
        if s[i] == '?':
            s = s[:i] + 'a' + s[i+1:]
    print(s)

Submission status: Runtime Error

Solution code:
s = input()
t = input()
l = len(t)
for i in range(len(s) - l + 1):
    m = -1
    f = True
    for j in range(l):
        if s[i:i+l][j] == '?' or s[i:i+l][j] == t[j]:
            pass
        else:
            f = False
    if f:
        m = i
if m < 0:
    print('UNRESTORABLE')
else:
    print((s[0:m] + t + s[m+l:]).replace('?', 'a'))

Submission status: Runtime Error

Solution code:
S_ = input()
T = input()
loop = len(S_) - len(T) + 1
stringlist = []

# check S_ in T
for n in range(loop):
    chance = True
    for i in range(len(T)):
        if S_[n+i] == T[i] or S_[n+i] == "?":
            pass
        else:
            chance = False
            break

    if chance:
        tmp = S_[:n] + T + S_[n+len(T):]
        stringlist.append(tmp)

if len(stringlist) == 0:
    print("UNRESTORABLE")
else:
    stringlist.sort()
    String = stringlist[0]
    S = ""
    for i in range(len(S_)):
        if String[i] == "?":
            S += "a"
        else:
            S += String[i]

print(S)

Submission status: Runtime Error

Solution code:
S_ = input()
T = input()
loop = len(S_) - len(T) + 1
stringlist = []

# check S_ in T
for n in range(loop):
    chance = True
    for i in range(len(T)):
        if S_[n+i] == T[i] or S_[n+i] == "?":
            pass
        else:
            chance = False
            break

    if chance:
        tmp = S_[:n] + T + S_[n+len(T):]
        stringlist.append(tmp)

if len(stringlist) == 0:
    print(UNRESTORABLE)
else:
    stringlist.sort()
    String = stringlist[0]
    S = ""
    for i in range(len(S_)):
        if String[i] == "?":
            S += "a"
        else:
            S += String[i]

print(S)

Submission status: Runtime Error

Solution code:
Sd=raw_input()
T=raw_input()
for index in range(len(Sd)-len(T),0,-1):
 flag=True
 for ind2 in range(len(T)):
  if not Sd[index+ind2] in ('?',T[ind2]):
   flag=False
 if flag:
  newS=Sd[:index]+T
  break
if not flag:
 print "UNRESTORABLE"
else: 
 newS=newS.replace('?','a')
 print newS

Submission status: Runtime Error

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

start = len(S) - len(T)
end = len(S)

ans = ''
while start >= 0:
	test = S[start:end]
	solve = True
	for i in range(len(T)):
		c1,c2 = test[i],T[i]
		if c1 != c2 and c1 != '?':
			solve = False
			break
	if solve:
		ans = S[:start] + T + S[end:]
		break
	start -= 1
	end -= 1
if len(ans > 0):
	print(ans.replace('?', 'a'))
else:
	print('UNRESTORABLE')