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: Wrong Answer

Solution code:
s=input()
t=input()
ans="zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
n=len(s)-len(t)+1
ok=0
#print(len(s))
#print(len(t))

i_set=[]

for i in range(n):
    count=0

    for j in range(len(t)):
        if t[j]==s[n-i-1+j] or s[n-i-1+j]=="?":
            count=count+1
#            print(i,t[j], s[n-i-1+j], count)
    if count==len(t):
#        print(i)
        ok=1
#        break
        i_set.append(i)
#if t in s:
#    print(s.replace("?","a"))

if ok==1:
    for i in i_set:
        ans0=s[0:n-i-1]+t
        ans1=ans0.replace("?","a")
        for i in range(len(s)-len(ans1)):
            ans1=ans1+"a"
        if ans>ans1:
            ans=ans1
#        print(ans)
    print(ans)
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

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

n=len(s)-len(t)+1
ok=0
#print(len(s))
#print(len(t))

for i in range(n):
    count=0

    for j in range(len(t)):
        if t[j]==s[n-i-1+j] or s[n-i-1+j]=="?":
            count=count+1
#            print(i,t[j], s[n-i-1+j], count)
    if count==len(t):
#        print(i)
        ok=1
        break

if t in s:
    print(s.replace("?","a"))


elif ok==1:
    ans0=s[0:n-i-1]+t
    ans1=ans0.replace("?","a")

    for i in range(len(s)-len(ans1)):
        ans1=ans1+"a"
    print(ans1)
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

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

n=len(s)-len(t)+1
ok=0
#print(len(s))
#print(len(t))

for i in range(n):
    count=0

    for j in range(len(t)):
        if t[j]==s[n-i-1+j] or s[n-i-1+j]=="?":
            count=count+1
#            print(i,t[j], s[n-i-1+j], count)
    if count==len(t):
#        print(i)
        ok=1
        break

if ok==1:
    ans0=s[0:n-i-1]+t
    ans1=ans0.replace("?","a")

    for i in range(len(s)-len(ans1)):
        ans1=ans1+"a"
    print(ans1)
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

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

n=len(s)-len(t)+1
ok=0
#print(len(s))
#print(len(t))

for i in range(n):
    count=0

    for j in range(len(t)):
        if t[j]==s[n-i-1+j] or s[n-i-1+j]=="?":
            count=count+1
#            print(i,t[j], s[n-i-1+j], count)
    if count==len(t):
#        print(i)
        ok=1
        break

if ok==1:
    ans0=s[0:n-i-1]+t
    ans1=ans0.replace("?","a")
    print(ans1)
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
s = input()
t = input()

#s = "?tc????"
#t = "coder"
#s = "???e???"
#t = "coder"


def coarse_match(s, t):
    assert len(s) >= len(t)
    s = s[:len(t)]
    if s == t:
        return True
    ret = True
    for idx in range(len(t)):
        if s[idx] == '?':
            continue
        if s[idx] != t[idx]:
            ret = False
    return ret


ret = ''
has_answer = False
pos = 0
while pos < len(s):
    c = s[pos]
    if c != "?":
        try:
            idx = t.index(c)
            sub_s = s[pos - idx:]
            if coarse_match(sub_s, t):
                has_answer = True
                pos += len(t) - idx
                ret += t
                continue
        except:
            pass
    ret += c
    pos += 1

if has_answer:
    ret = ret.replace('?', 'a')
    print(ret)
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
s = input()
t = input()

#s = "'?tc????'"
#t = "coder"


def coarse_match(s, t):
    assert len(s) >= len(t)
    s = s[:len(t)]
    if s == t:
        return True
    ret = True
    for idx in range(len(t)):
        if s[idx] == '?':
            continue
        if s[idx] != t[idx]:
            ret = False
    return ret


ret = ''
has_answer = False
pos = 0
while pos < len(s):
    c = s[pos]
    try:
        idx = s.index(c)
        sub_s = s[idx:]
        if coarse_match(sub_s, t):
            has_answer = True
            pos += len(t)
            ret += t
            continue
    except:
        pass
    ret += c
    pos += 1

if has_answer:
    ret = ret.replace('?', 'a')
    print(ret)
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
s = input()
t = input()

#s = "'?tc????'"
#t = "coder"


def coarse_match(s, t):
    assert len(s) >= len(t)
    s = s[:len(t)]
    if s == t:
        return True
    ret = True
    for idx in range(len(t)):
        if s[idx] == '?':
            continue
        if s[idx] != t[idx]:
            ret = False
    return ret


ret = ''
has_answer = False
pos = 0
while pos < len(s):
    c = s[pos]
    try:
        idx = s.index(c)
        sub_s = s[idx:]
        if coarse_match(sub_s, t):
            has_answer = True
            pos += len(t)
            print(sub_s, t)
            ret += t
            continue
    except:
        pass
    ret += c
    pos += 1

if has_answer:
    ret = ret.replace('?', 'a')
    print(ret)
else:
    print("UNRESTORABLE")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
ind = 0
sd = input()
t = input()
len_t = len(t)
len_sd = len(sd)
matched = False

while (len_sd - len_t - ind) >= 0:
    # S'からTと同じ長さの文字列を後方から順番に切り出す
    partOfSd = sd[len_sd - ind - len_t:len_sd - ind]
    print ('ind:' + partOfSd)
    # 切り出した文字列PoSdに対して１文字ずつループしてTと比較する
    matched = False
    if len(partOfSd) < len_t:
        break
    else:
        ind_posd = 0
    
    while ind_posd < len_t:
        if partOfSd[ind_posd] == t[ind_posd] or partOfSd[ind_posd] == '?':
            # match
            matched = True
            ind_posd += 1
        else:
            matched = False
            break
    if matched:
        # 全部一致した場合
        # PoSdをTで置き換えて、残りの？をaに置き換える
        slist = list(sd)
        ind_t = 0
        while ind_t < len_t:
            slist[len_sd - ind - len_t] = t[ind_t]
            ind -= 1
            ind_t += 1
        sd = "".join(slist)
        print (sd.replace('?', 'a'))
        break
    else:
        ind += 1
        
if not matched:
    print ('UNRESTORABLE')

# 一致しなかった場合、Sdを一文字ずらして繰り返す

Submission status: Wrong Answer

Solution code:
import sys
S0=raw_input()
T=list(raw_input())

l=len(T)

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


ans=""
for i in range(len(S0)-l+1):
	S1=list(S0)
	#print S1
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
		S_tmp=(''.join(S1)).replace('?','a')
		#print ans
		#print S_tmp
		if ans=="":
			ans=S_tmp
		if S_tmp < ans:
			ans=S_tmp
			
	
if S0==''.join(S1) and S0.find("?")!=-1:
	print "UNRESTORABLE"
	sys.exit()
	

print ans

Submission status: Wrong Answer

Solution code:
import sys
S0=raw_input()
T=list(raw_input())

l=len(T)

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


ans=""
for i in range(len(S0)-l+1):
	S1=list(S0)
	print S1
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
		S_tmp=(''.join(S1)).replace('?','a')
		#print ans
		#print S_tmp
		if ans=="":
			ans=S_tmp
		if S_tmp < ans:
			ans=S_tmp
			
	
if S0==''.join(S1) and S0.find("?")!=-1:
	print "UNRESTORABLE"
	sys.exit()
	

print ans

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
ind = 0
sd = input()
t = input()
len_t = len(t)
matched = False

while ind < len(sd):
    # S'からTと同じ長さの文字列を切り出す
    partOfSd = sd[ind:ind + len_t]
    #print ('ind:' + partOfSd)
    # 切り出した文字列PoSdに対して１文字ずつループしてTと比較する
    matched = False
    if len(partOfSd) < len_t:
        break
    else:
        ind_posd = 0
    
    while ind_posd < len_t:
        if partOfSd[ind_posd] == t[ind_posd] or partOfSd[ind_posd] == '?':
            # match
            matched = True
            ind_posd += 1
        else:
            matched = False
            break
    if matched:
        # 全部一致した場合
        # PoSdをTで置き換えて、残りの？をaに置き換える
        slist = list(sd)
        ind_t = 0
        while ind_t < len_t:
            slist[ind] = t[ind_t]
            ind += 1
            ind_t += 1
        sd = "".join(slist)
        print (sd.replace('?', 'a'))
        break
    else:
        ind += 1
        
if not matched:
    print ('UNRESTORABLE')

# 一致しなかった場合、Sdを一文字ずらして繰り返す

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
ind = 0
sd = input()
t = input()
len_t = len(t)
matched = False

while ind < len(sd):
    # S'からTと同じ長さの文字列を切り出す
    partOfSd = sd[ind:ind + len_t]
    #print ('ind:' + partOfSd)
    # 切り出した文字列PoSdに対して１文字ずつループしてTと比較する
    if len(partOfSd) < len_t:
        matched = False
        break
    else:
        ind_posd = 0
        matched = True
    
    while ind_posd < len_t:
        if partOfSd[ind_posd] == t[ind_posd] or partOfSd[ind_posd] == '?':
            # match
            ind_posd += 1
        else:
            matched = False
            break
    if matched:
        # 全部一致した場合
        # PoSdをTで置き換えて、残りの？をaに置き換える
        slist = list(sd)
        ind_t = 0
        while ind_t < len_t:
            slist[ind] = t[ind_t]
            ind += 1
            ind_t += 1
        sd = "".join(slist)
        print (sd.replace('?', 'a'))
        break
    else:
        ind += 1
        
if not matched:
    print ('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
ind = 0
sd = input()
t = input()
len_t = len(t)
matched = False

while ind < len(sd):
    # S'からTと同じ長さの文字列を切り出す
    partOfSd = sd[ind:ind + len_t]
    print ('ind:' + partOfSd)
    # 切り出した文字列PoSdに対して１文字ずつループしてTと比較する
    if len(partOfSd) < len_t:
        matched = False
        break
    else:
        ind_posd = 0
        matched = True
    
    while ind_posd < len_t:
        if partOfSd[ind_posd] == t[ind_posd] or partOfSd[ind_posd] == '?':
            # match
            ind_posd += 1
        else:
            matched = False
            break
    if matched:
        # 全部一致した場合
        # PoSdをTで置き換えて、残りの？をaに置き換える
        slist = list(sd)
        ind_t = 0
        while ind_t < len_t:
            slist[ind] = t[ind_t]
            ind += 1
            ind_t += 1
        sd = "".join(slist)
        print (sd.replace('?', 'a'))
        break
    else:
        ind += 1
        
if not matched:
    print ('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
ind = 0
sd = input()
t = input()
len_t = len(t)

while ind < len(sd):
    # S'からTと同じ長さの文字列を切り出す
    partOfSd = sd[ind:ind + len_t]
    # 切り出した文字列PoSdに対して１文字ずつループしてTと比較する
    if len(partOfSd) < len_t:
        matched = False
        break
    else:
        ind_posd = 0
        matched = True
    
    while ind_posd < len_t:
        if partOfSd[ind_posd] == t[ind_posd] or partOfSd[ind_posd] == '?':
            # match
            ind_posd += 1
        else:
            matched = False
            break
    if matched:
        # 全部一致した場合
        # PoSdをTで置き換えて、残りの？をaに置き換える
        slist = list(sd)
        ind_t = 0
        while ind_t < len_t:
            slist[ind] = t[ind_t]
            ind += 1
            ind_t += 1
        sd = "".join(slist)
        print (sd.replace('?', 'a'))
        break
    else:
        ind += 1
        
if not matched:
    print ('UNRESTORABLE')

# 一致しなかった場合、Sdを一文字ずらして繰り返す

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
ind = 0
sd = input()
t = input()
len_t = len(t)

while ind < len(sd):
    # S'からTと同じ長さの文字列を切り出す
    partOfSd = sd[ind:ind + len_t]
    print ('ind:' + partOfSd)
    # 切り出した文字列PoSdに対して１文字ずつループしてTと比較する
    if len(partOfSd) < len_t:
        matched = False
        break
    else:
        ind_posd = 0
        matched = True
    
    while ind_posd < len_t:
        if partOfSd[ind_posd] == t[ind_posd] or partOfSd[ind_posd] == '?':
            # match
            ind_posd += 1
        else:
            matched = False
            break
    if matched:
        # 全部一致した場合
        # PoSdをTで置き換えて、残りの？をaに置き換える
        slist = list(sd)
        ind_t = 0
        while ind_t < len_t:
            slist[ind] = t[ind_t]
            ind += 1
            ind_t += 1
        sd = "".join(slist)
        print (sd.replace('?', 'a'))
        break
    else:
        ind += 1
        
if not matched:
    print ('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
import sys
S0=raw_input()
S1=list(S0)
T=list(raw_input())
#print S1
#print T

l=len(T)

S=[]

for i in range(len(S1)-l+1):
	for j in range(l):
		#print i+j,j,S1[i+j],T[j]
		if S1[i+j]!=T[j] and S1[i+j]!="?":
			#print "break"
			break
	else:
		#print "else"
		for j in range(l):
			S1[i+j]=T[j]
	
if S0==''.join(S1):
	print "UNRESTORABLE"
	sys.exit()
	

S2=''.join(S1)
print S2.replace('?','a')

Submission status: Wrong Answer

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

sd = len(SD)
t = len(T)
S = 'z' * 100
SI = 'z' * 100

if t > sd:
	print('UNRESTORABLE')
else:
	for i in range(sd-t+1):
		flag = 0
		for j in range(t):
			if SD[j+i] != T[j] and SD[j+i] != '?':
				flag = 1
				break
		if flag == 0:
			SID = SD.replace( SD[i:i+t], T )
			SI = SID.replace('?','a')
		if SI < S:
			S = SI
	if S == 'z' * 100:
		print('UNRESTORABLE')
	else:
		print(S)

Submission status: Wrong Answer

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

sd = len(SD)
t = len(T)
S = 'z' * 100

if t > sd:
	print('UNRESTORABLE')
else:
	for i in range(sd-t+1):
		flag = 0
		for j in range(t):
			if SD[j+i] != T[j] and SD[j+i] != '?':
				flag = 1
				break
		if flag == 0:
			SID = SD.replace( SD[i:i+t], T )
			SI = SID.replace('?','a')
			if SI < S:
				S = SI
	if S == 'z' * 100:
		print('UNRESTORABLE')
	else:
		print(S)

Submission status: Wrong Answer

Solution code:
a = raw_input()
b = raw_input()
a = list(a)
b = list(b)
n = len(a)
m = len(b)
z = ''
for i in range(n - m + 1):
	flag = True
	for j in range(m):
		if a[i + j] != b[j] and a[i + j] != '?':
			flag = False
	if not flag:
		continue
	c = a
	for j in range(m):
		c[i + j] = b[j]
	for i in range(n):
		if c[i] == '?':
			c[i] = 'a'
	if z == '':
		z = c
	else:
		z = min(z, c)
if z == '':
	print 'UNRESTORABLE'
else:
	print ''.join(z)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
ans = "UNRESTORABLE"

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

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
from collections import defaultdict
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readLine_int_list_reverse(): return list(map(int, stdin.readline().split())).reverse()
def readAll_int(): return list(map(int, stdin))
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readAll_str(): return list(map(str, stdin))

import re

def main():
	_s = input()
	t = input()
	pattern=""
	for i in t:
	    pattern += "[?{}]".format(i)
	l=[]
	ans = ""
	f = False
	ff = False
	for i in range(len(_s)):
	    if _s[i] == '?' or _s[i] == t[0]:
	        tmp = _s[i:i+len(t)]
	        match = re.match(pattern , tmp)
	        if match:
	            
	            f = True
	            if _s[i] == t:
	                ans += _s[i]
	                ff = True
	            else:
	                ans += 'X'
	        else:
	            ans += 'a'
	    else:
	        ans += _s[i]
	    
	        
	for i in range(len(ans)):
	    if ans[i] == "X":
	       _ans = [_ for _ in ans]
	       if ff:
	           l.append(''.join(_ans).replace("X","a"))
	       else:
	           _ans[i:i+len(t)] = t
	       for i in range(len(_ans)):
	           if _ans[i] == "X":
	               _ans[i] = 'a'
	       l.append(''.join(_ans))
	       
	if f:
	    ans = ''.join(min(l))
	    print(ans)
	else:
	    print("UNRESTORABLE")
	    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import re

s=input()
#print(s)


s_t=input()
sl=[]
sl.extend(s)
#print(sl)
hl=re.compile(r'\?+').findall(s)
cl=re.compile(r'\w+').findall(s)
#print(hl)
#print(cl)
hf = False
f = True
tl=[]

if sl[0] == '?':
    for i in range(len(hl)-1):
        tl.append(hl[i])
        tl.append(cl[i])
    if len(hl) != len(cl):
        tl.append(hl[len(cl)])
else:
    for i in range(len(cl)-1):
        tl.append(cl[i])
        tl.append(hl[i])
    if len(hl) != len(cl):
        tl.append(cl[len(hl)])
        
        
#print(tl)

#まず上から
for t in tl:
    if t.find(s_t) != -1:
        s = s.replace('?','a')
        print(s)
        exit()

def fin(s,i):
    
    t=0
    for c in s:
        if c == i[t] or c == '?':
            t+=1
            continue
        
        else:
            #print (':'+c+':'+i[t]+':')
            return False
    
    return True
    
for w in range(len(s)-len(s_t)+1):
    #print(s[len(s)-w-len(s_t):len(s)-w]+':',end='')
    #print(s_t+':',end='')
    if re.compile(r'\w').findall(s[len(s)-w-len(s_t):len(s)-w]) != []:
        
        if fin(s[len(s)-w-len(s_t):len(s)-w],s_t):
            s = s[0:len(s)-w-len(s_t)] + s_t +s[len(s)-w:len(s)]
            s = s.replace('?','a')
        
            print(s)
            exit()
    else:
        continue

for t in tl:
    if t[0]!='?':
        continue
    if len(s_t)<= len(t):
        ind=tl.index(t)
        tl[ind]=(len(t)-len(s_t))*'?'+s_t
        ans=''
        for t in tl:
            ans=ans+t
        
        ans = ans.replace('?','a')
        print(ans)
        exit()

print('UNRESTORABLE')

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
from collections import defaultdict
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readLine_int_list_reverse(): return list(map(int, stdin.readline().split())).reverse()
def readAll_int(): return list(map(int, stdin))
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readAll_str(): return list(map(str, stdin))

import re

def main():
	_s = input()
	t = input()
	pattern=""
	for i in t:
	    pattern += "[?{}]".format(i)
	l=[]
	ans = ""
	f = False
	for i in range(len(_s)):
	    if _s[i] == '?' or _s[i] == t[0]:
	        tmp = _s[i:i+len(t)]
	        match = re.match(pattern , tmp)
	        if match:
	            f = True
	            ans += 'X'
	        else:
	            ans += 'a'
	    else:
	        ans += _s[i]
	
	for i in range(len(ans)):
	    if ans[i] == "X":
	       _ans = [_ for _ in ans]
	       _ans[i:i+len(t)] = t
	       for i in range(len(_ans)):
	           if _ans[i] == "X":
	               _ans[i] = 'a'
	       l.append(_ans)
	       
	if f:
	    ans = ''.join(min(l))
	    print(ans)
	else:
	    print("UNRESTORABLE")
	    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

s_d = input()
t = input()

s = ''
for i in range(len(s_d)):
    i = len(s_d) - i - 1
    find_num = t.find(s_d[i])
    if not find_num == -1:
        if len(s_d[:i]) >= len(t[:find_num]) and len(s_d[i:]) >= len(t[find_num:]):
            s_d = s_d[:(i-find_num)] + t + s_d[(i+len(t)-find_num):]
            break
    elif i == 0:
        print('UNRESTORABLE');
        sys.exit()

for i in range(len(s_d)):
    if s_d[i] == '?':
        s = s + 'a'
    else:
        s = s + s_d[i]
          
print(s)

Submission status: Wrong Answer

Solution code:
import sys
 
S = sys.stdin.readline().rstrip()
T = sys.stdin.readline().rstrip()
 
def checkValid(S, i, T):
    try:
        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
    except Exception as e:
        return False
 
def checkValid2(S, i, T):
    try:
        for j in range(0, len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                pass
            else:
                return False
        return True
    except Exception as e:
        return False
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
    else:
        if checkValid2(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]

Submission status: Wrong Answer

Solution code:
import sys

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

def checkValid(S, i, T):
    try:
        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
    except Exception as e:
        return False

def checkValid2(S, i, T):
    try:
        for j in range(0, len(T)):
            if S[i+j] == T[j] or S[i+j] == '?':
                pass
            else:
                return False
        return True
    except Exception as e:
        return False
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
    else:
        if checkValid2(S, i, T) == True:
            res_list.append(getResult(S, i, T))
            res = True

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

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
from collections import defaultdict
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readLine_int_list_reverse(): return list(map(int, stdin.readline().split())).reverse()
def readAll_int(): return list(map(int, stdin))
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readAll_str(): return list(map(str, stdin))

import re

def main():
	_s = input()
	t = input()
	pattern=""
	for i in t:
	    pattern += "[?{}]".format(i)

	ans = ""
	f = False
	for i in range(len(_s)):
	    if _s[i] == '?' or _s[i] == t[0]:
	        tmp = _s[i:i+len(t)]
	        match = re.match(pattern , tmp)
	        if match:
	            f = True
	            ans += 'X'
	        else:
	            ans += 'a'
	    else:
	        ans += _s[i]
	
	
	for i in range(len(ans)):
	    if ans[i] == "X":
	       _ans = [_ for _ in ans]
	       _ans[i:i+len(t)] = t
	       for i in range(len(_ans)):
	           if _ans[i] == "X":
	               _ans[i] = 'a'
	       
	
	if f:
	    ans = ''.join(_ans)
	    print(ans)
	else:
	    print("UNRESTORABLE")
	    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

s_d = input()
t = input()

s = ''
for i in range(len(s_d)):
    find_num = t.find(s_d[i])
    if not find_num == -1:
        if len(s_d[:i]) >= len(t[:find_num]) and len(s_d[i:]) >= len(t[find_num:]):
            s_d = s_d[:(i-find_num)] + t + s_d[(i+len(t)-find_num):]
            break
    elif i == len(s_d)-1:
        print('UNRESTORABLE');
        sys.exit()
        
for i in range(len(s_d)):
    if s_d[i] == '?':
        s = s + 'a'
    else:
        s = s + s_d[i]
          
print(s)

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
from collections import defaultdict
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readLine_int_list_reverse(): return list(map(int, stdin.readline().split())).reverse()
def readAll_int(): return list(map(int, stdin))
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readAll_str(): return list(map(str, stdin))

import re

def main():
	_s = input()
	t = input()
	pattern=""
	for i in t:
	    pattern += "[?{}]".format(i)

	ans = ""
	f = False
	for i in range(len(_s)):
	    if _s[i] == '?' or _s[i] == t[0]:
	        tmp = _s[i:i+len(t)]
	        match = re.match(pattern , tmp)
	        if match:
	            f = True
	            ans += 'X'
	        else:
	            ans += 'a'
	    else:
	        ans += _s[i]
	
	
	for i in range(len(ans)):
	    if ans[i] == "X":
	       _ans = [_ for _ in ans]
	       _ans[i:i+len(t)] = t
	       for i in range(len(_ans)):
	           if _ans[i] == "X":
	               _ans[i] = 'a'
	       print(_ans)
	       
	ans = ''.join(ans)
	if f:
	    print(ans)
	else:
	    print("UNRESTORABLE")
	    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import re

s=input()
s_t=input()
sl=[]
sl.extend(s)
#print(sl)
hl=re.compile(r'\?+').findall(s)
cl=re.compile(r'\w+').findall(s)
#print(hl)
#print(cl)
hf = False
f = True
tl=[]

if sl[0] == '?':
    for i in range(len(hl)-1):
        tl.append(hl[i])
        tl.append(cl[i])
    if len(hl) != len(cl):
        tl.append(hl[len(cl)])
else:
    for i in range(len(cl)-1):
        tl.append(cl[i])
        tl.append(hl[i])
    if len(hl) != len(cl):
        tl.append(cl[len(hl)])
        
        
#print(tl)

#まず上から
for t in tl:
    if t.find(s_t) != -1:
        s = s.replace('?','a')
        print(s)
        exit()

def fin(s,i):
    
    t=0
    for c in s:
        if c == i[t] or c == '?':
            t+=1
            continue
        
        else:
            #print (':'+c+':'+i[t]+':')
            return False
    
    return True
    
for w in range(len(s)-len(s_t)):
    #print(s[len(s)-w-len(s_t):len(s)-w]+':',end='')
    #print(s_t,end='')
    if re.compile(r'\w').findall(s[len(s)-w-len(s_t):len(s)-w]) != []:
        
        if fin(s[len(s)-w-len(s_t):len(s)-w],s_t):
            s = s[0:len(s)-w-len(s_t)] + s_t +s[len(s)-w:len(s)]
            s = s.replace('?','a')
        
            print(s)
            exit()
    else:
        continue

for t in tl:
    if t[0]!='?':
        continue
    if len(s_t)<= len(t):
        ind=tl.index(t)
        tl[ind]=(len(t)-len(s_t))*'?'+s_t
        ans=''
        for t in tl:
            ans=ans+t
        
        ans = ans.replace('?','a')
        print(ans)
        exit()

print('UNRESTORABLE')

Submission status: Wrong Answer

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 +1

		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 k in j_list:
	for i,c in zip(range(k-lt,k),T):
		S[i] = c

	S_tmp = ''

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

	S_list.append(S_tmp)

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

Submission status: Wrong Answer

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

S_p = input()
T = input()

def search_mask_text(S_p,T,T_p):
    for i in range(len(T_p)):
        if T_p[i] == '?':
            T_p_n = T_p[0:i] + T[i] + T_p[i+1:len(T_p)]
            index_S = S_p.find(T_p_n)
            if index_S != -1:
                return S_p[0:index_S] + T + S_p[index_S+len(T):len(S_p)]
            S_n = search_mask_text(S_p,T,T_p_n)
            if S_n != 'UNRESTORABLE':
                return S_n
            #else:
            #    exit
    return 'UNRESTORABLE'


def search_ans_text(S_t,T):
    for i in range(len(S_t)):
        if S_t[i] == '?':
            S_t = S_t[0:i] + 'a' + S_t[i+1:len(S_t)]
    return S_t



T_p = '?' * len(T)
S_r = search_mask_text(S_p,T,T_p)
print(search_ans_text(S_r,T))

Submission status: Wrong Answer

Solution code:
import sys

s_d = input()
t = input()

s = ''
for i in range(len(s_d)):
    find_num = t.find(s_d[i])
    if not find_num == -1:
        if len(s_d[:i]) >= len(t[:find_num]) and len(s_d[i:]) >= len(t[find_num:]):
            s_d = s_d[:i] + t + s_d[i+len(t)-find_num:]
            break
    elif i == len(s_d)-1:
        print('UNRESTORABLE');
        sys.exit()
        
for i in range(len(s_d)):
    if s_d[i] == '?':
        s = s + 'a'
    else:
        s = s + s_d[i]
          
print(s)

Submission status: Wrong Answer

Solution code:
sd = raw_input()
t = raw_input()
lnsd = len(sd)
lnt = len(t)
sa = 0
tru = None
for i in range(lnsd - lnt + 1):
    for i2 in range(lnt):
        a = sd[(0+sa):(lnt+sa)]
        if a[i2] != t[i2] and a[i2] != "?":
            break
        if i2 == lnsd -lnt:
            tru= i
    sa = sa+1
if tru == None:
    print("UNRESTORABLE")
    exit()
sd = list(sd)
for i in range(lnt):
    sd[tru+i]=t[i]
strn ="".join(sd)
print(strn.replace("?", "a"))

Submission status: Wrong Answer

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)
		
		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])