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:
import math
import sys
import bisect

S1 = input()
T = input()

candidates = []


def check(sStart):
    for i in range(len(T)):
        sIdx = sStart + i
        if sIdx < len(S1) and (S1[sIdx] == T[i] or S1[sIdx] == "?"):
            pass
        else:
            return False
    return True


for i in range(len(S1)):
    s = ""
    if check(i):
        c = S1[0:i] + T +S1[i+len(T):]
        # print(c)
        candidates.append(c)

clist = list(set(candidates))
clist = sorted(clist)


if len(clist) == 0:
    print("UNRESTORABLE")
else:
    ret = clist[0]
    ret = ret.replace("?","a")
    print(ret)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()
t = input()
n = len(s)

def check(idx):
    for i in range(len(t)):
        ii = idx+i
        if ii>=n:
            return False
        if s[ii]!="?" and s[ii]!=t[i]:
            return False
    return True

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

res = []
for i in range(n):
    if check(i):
        res.append(restore(i))
# print(res)
if len(res)==0:
    print("UNRESTORABLE")
else:
    res.sort()
    print(res[0])

Submission status: Accepted

Solution code:
from re import match

s = input().replace('?','.')
t = input()
s_len = len(s)
t_len = len(t)

for i in range(s_len-t_len,-1,-1):
    if match(s[i:i+t_len],t):
        s = s.replace('.','a')
        print(s[:i]+t+s[i+t_len:])
        exit()
print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys

sd = input()
t = input()
NG = 'UNRESTORABLE'


def valid(os: int):
    if len(sd) <= os or len(sd) - os < len(t):
        return False
    for sc, tc in zip(sd[os:os+len(t)], t):
        if sc != tc and sc != '?':
            return False
    ans = ''
    for i, sc in enumerate(sd):
        if i < os:
            c = sc
        elif i < os + len(t):
            c = t[i - os]
        else:
            c = sc
        if c == '?':
            c = 'a'
        ans += c
    return ans


if len(t) > len(sd):
    print(NG)
    sys.exit()

for i in range(len(sd) - len(t) + 1):
    ans = valid(len(sd) - len(t) - i)
    if ans:
        print(ans)
        sys.exit()

print(NG)

Submission status: Accepted

Solution code:
import re
S = input().replace('?', '.')
T = input()
n = len(T)
for i in range(len(S) - n, -1, -1):
    if re.match(S[i:i+n], T):
        S = S.replace('.', 'a')
        print(S[:i] + T + S[i+n:])
        exit()
print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s, t = [input() for _ in range(2)]

n, m = map(len, [s, t])
for i in range(n-m, -1, -1):
	if not all(s_j in ["?", t_j] for s_j, t_j in zip(s[i:i+m], t)):
		continue
	print((s[:i] + t + s[i+m:]).replace("?", "a"))
	exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
def listToString(_list, split=""):
    maped_list = map(str, _list)  # mapで要素すべてを文字列に
    mojiretu = split.join(maped_list)
    return mojiretu


def stringToList(_string, split=" "):
    str_list = []
    temp = ''
    for x in _string:
        if x == split:  # 区切り文字
            str_list.append(temp)
            temp = ''
        else:
            temp += x
    if temp != '':  # 最後に残った文字列を末尾要素としてリストに追加
        str_list.append(temp)
    return str_list


s = input()
t = input()
firstMatch = False
out = listToString(["z" for i in range(len(s))])
# tがiからスタートするとして全探索O(n^2)<=50*50
for i in range(len(s) - len(t) + 1):
    localStr = []
    match = True
    for j in range(i):  # i番目まではsの?をaで置き換えるだけ
        if s[j] == "?":
            localStr.append("a")
        else:
            localStr.append(s[j])
    for j in range(i, i+len(t)):
        # print("i:{} j:{}".format(i, j))
        # print("s[j]:{} t[j-i]:{}".format(s[j], t[j-i]))
        if s[j] != t[j - i]:
            if s[j] == "?":
                localStr.append(t[j - i])
            else:
                match = False
                break
        else:
            localStr.append(s[j])
    for j in range(i+len(t), len(s)):
        if s[j] == "?":
            localStr.append("a")
        else:
            localStr.append(s[j])
    # print("match:{}".format(match))
    if match:
        firstMatch = True
        if listToString(localStr) < out:
            # print("local:{} out:{}".format(localStr, out))
            out = listToString(localStr)
if firstMatch:
    print(out)
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = list(input())
T = list(input())
ans = []
for i in range(len(S) - len(T) + 1):
    SS = S[i:i+len(T)]
    for j in range(len(T)):
        if SS[j] != T[j] and SS[j] != '?':
            break
    else:
        sfir = ''.join(S[:i])
        saft = ''.join(S[i+len(T):])
        sfir = sfir.replace('?', 'a')
        saft = saft.replace('?', 'a')
        ans.append(sfir+''.join(T)+saft)
    
if ans:
    print(sorted(ans)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
ss,t = list(input()),input()

flag = False
for i in range(len(ss)):
    if ss[i] == '?' or ss[i] == t[0]:
        for j in range(len(t)):
            if i+j >= len(ss):                
                break
            elif ss[i+j] == t[j] or ss[i+j] == '?':
                if j == len(t)-1:
                    flag = True
                    start_t = i
                continue
            else:
                break
        
ans = 'UNRESTORABLE'
if flag:
    ss[start_t:start_t+len(t)] = t
    ans = ''.join(ss).replace('?','a')

print(ans)

Submission status: Accepted

Solution code:
a = list(input())
b = list(input())
count = 0
m = 0
ar = []
c = ""
for i in range(len(a)-len(b)+1):
    for j in range(len(b)):
        if b[j] == a[i+j] or a[i+j] == "?":
            count += 1
    if count == len(b):
        for k in range(len(a)):
            if k >= i and k <= i + len(b) - 1:
                c += b[m]
                m += 1
            else:
                if a[k] == "?":
                    c += "a"
                else:
                    c += a[k]
        ar.append("".join(c))
        c = ""
    count = 0
    m = 0
if len(ar) == 0:
    print("UNRESTORABLE")
else:
    print(sorted(ar)[0])

Submission status: Accepted

Solution code:
import copy as cp
s = input()
t = input()
u = list(s)
v = list(t)
w = cp.deepcopy(u)
p = 0
q = 0
for h in range(len(u) - 1, -1, -1):
    if q == 1:
        break
    if v[-1] == u[h]:
        #w[i] = v[0]
        for j in range(len(v)):
            if (v[-1 - j] == u[h - j]) and h - j >= 0:
                pass
                if j == len(v) - 1:
                    #for k in range(len(v)):
                        #w[h - k] = v[-1 - k]
                    q = 1
                    p = 1
            else:
                break
if q == 0:
    for i in range(len(u) - 1, -1, -1):
        if p == 1:
            break
        if v[-1] == u[i] or "?" == u[i]:
            #w[i] = v[0]
            for j in range(len(v)):
                if (v[-1 - j] == u[i - j] or "?" == u[i - j]) and i - j >= 0:
                    pass
                    if j == len(v) - 1:
                        for k in range(len(v)):
                            w[i - k] = v[-1 - k]
                        p = 1
                else:
                    break

if p == 1:
    for m in range(len(u)):
        if w[m] == "?":
            w[m] = "a"
    print("".join(w))
if p == 0:
    print("UNRESTORABLE")

Submission status: Accepted

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

for i in range(len(Sd) - len(T), -1, -1):
    flag = True
    for j, k in zip(range(i, i+len(T)), T):
        if Sd[j] != k and Sd[j] != '?':
            flag = False
            break

    if flag:
        Sd_List = list(Sd)
        for j, k in zip(range(i, i+len(T)), T):
            Sd_List[j] = k
        Sd = "".join(Sd_List)
        result = Sd.replace('?', 'a')
        break

print(result if result else 'UNRESTORABLE')

Submission status: Accepted

Solution code:
s, t = [input() for _ in range(2)]
t_len = len(t)

def is_fit(a, b):
	for i in range(t_len):
		if a[i] not in [b[i], "?"]:
			return False
	return True

for i in range(len(s)-t_len, -1, -1):
	if not is_fit(s[i:i+t_len], t):
		continue
	s = list(s)
	s[i:i+t_len] = list(t)
	s = "".join(s).replace("?", "a")
	print(s)
	exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S_hint=input()
T=input()
List_S_hint=list(S_hint)
S_candidates=[]
Out=0
for i in range(len(S_hint)-len(T)+1):
  Out=0
  Word=""
  Candidate=list(List_S_hint)
  for j in range(len(T)):
    if Candidate[i+j] is "?":
      Candidate[i+j]=T[j]
    elif Candidate[i+j]==T[j]:
      pass
    else:
      Out=1
      break
  if Out==1:
    pass
  else:
    for n in Candidate:
      if n=="?":
        Word+="a"
      else:
        Word+=n
    S_candidates.append(Word)  
if len(S_candidates)==0:
  print("UNRESTORABLE")
else:  
  print(min(S_candidates))

Submission status: Accepted

Solution code:
# encoding: utf-8
Sd = input()
T = input()

LSd = len(Sd)
LT = len(T)

ans = "UNRESTORABLE"
for i in range(LSd - LT + 1):
    for si, ti in zip(Sd[LSd - LT - i: LSd - i], T):
        if si != ti and si != "?": break
    else:
        ans = Sd[:LSd - LT - i].replace("?", "a") + T + Sd[LSd - i:].replace("?", "a")
        break

print(ans)

Submission status: Accepted

Solution code:
sp = list(input())
t = list(input())
n = len(sp)
for i in range(len(t), n + 1):
    flag = True
    for j in range(len(t)):
        if sp[n - i + j] != t[j] and sp[n - i + j] != '?':
            flag = False
            break
    if flag:
        s = ''
        sp[n - i:n - i + len(t)] = t
        for j in range(n):
            if sp[j] == '?':
                s += 'a'
            else:
                s += sp[j]
        print(s)
        exit()
print('UNRESTORABLE')

Submission status: Accepted

Solution code:
sp = list(input())
t = list(input())
n = len(sp)
s = []
for i in range(len(t), n + 1):
    flag = True
    for j in range(len(t)):
        if sp[n - i + j] != t[j] and sp[n - i + j] != '?':
            flag = False
            break
    if flag:
        s.append('')
        ss = sp
        ss[n - i:n - i + len(t)] = t
        for j in range(n):
            if ss[j] == '?':
                s[-1] += 'a'
            else:
                s[-1] += ss[j]
if s == []:
    print('UNRESTORABLE')
else:
    s.sort()
    print(s[0])

Submission status: Accepted

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

if len(sd)<len(T):
    fl=False
else:
    for i in range(len(sd)-len(T),-1,-1):
    #    print(i)
        st=sd[i:len(T)+i]
    #    print(sd[i:len(T)+i])
        fl=True
        for j in range(len(T)):
            if st[j]!='?':
                if st[j]!=T[j]:
                    fl=False
        if fl:
            s=sd[:i]+T+sd[len(T)+i:]
            for k in range(len(s)):
                if s[k]=='?':
                    s=s[:k]+'a'+s[k+1:]
            break
if fl:
    print(s)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
def solve(S, T):
    for i in reversed(range(0, len(S) - len(T) + 1)):
        if all(S[j] == "?" or S[j] == T[j - i] for j in range(i, i + len(T))):
            return (S[0:i] + T + S[i + len(T):]).replace("?", "a")
    return "UNRESTORABLE"


if __name__ == "__main__":
    S = input()
    T = input()
    print(solve(S, T))

Submission status: Accepted

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

s_len = len(s)
t_len = len(t)
ans = s[:]
question_num = 0
unrestorable = True
for i in range(s_len):
    si = (s_len-1) - i
    restorable = True
    if s_len - si >= t_len:
        for ti in range(t_len):
            w = s[si+ti]
            if w == "?" or w == t[ti]:
                continue
            restorable = False
    else:
        restorable = False
    if restorable:
        unrestorable = False
        for ti in range(t_len):
            ans[si+ti] = t[ti]
        break

for i in range(s_len):
    if ans[i] == "?":
        ans[i] = "a"

if unrestorable:
    print("UNRESTORABLE")
else:
    print(''.join(ans))

Submission status: Accepted

Solution code:
def 解():
    sS = input().rstrip()
    sT = input().rstrip()
    impo = "UNRESTORABLE"
    iS = len(sS)
    iT = len(sT)
    for i in range(iS-iT,-1,-1):
        bF = 1
        for j in range(iT):
            if sS[i+j] != "?" and sS[i+j] != sT[j]:
                bF = 0
                break
        if bF:
            sAns = (sS[0:i]+sT+sS[i+iT:]).replace("?","a")
            print(sAns)
            exit()
    print(impo)
解()

Submission status: Accepted

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

for i in range(len(s) - 1, len(t) - 2, -1):
    j = 0
    tmps = s.copy()
    while s[i - j] == '?' or s[i - j] == t[len(t) - 1 - j]:
        tmps[i - j] = t[len(t) - 1 - j]
        j += 1
        if len(t) - 1 - j < 0:
            for k in range(len(tmps)):
                if tmps[k] == '?':
                    tmps[k] = 'a'
            print(''.join(tmps))
            sys.exit(0)

print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys

def is_match(x, t, n):
    for i in range(n):
        if x[i] != '?' and x[i] != t[i]:
            return False
    return True


s = input()
t = input()
len_s = len(s)
len_t = len(t)

if len_s < len_t:
    print('UNRESTORABLE')
    sys.exit(0)

segs = [s[i:i+len_t] for i in range(len_s - len_t + 1)]
ans = 'z' * 51

for i, seg in enumerate(segs):
    if is_match(seg, t, len_t):
        tmp = s[0:i] + t + s[i+len_t:]
        tmp = tmp.replace('?', 'a')
        ans = min(ans, tmp)

if len(ans) > 50:
    print('UNRESTORABLE')
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if len(T) > len(Sprime):
    print('UNRESTORABLE')
else:
    ans = -1
    for i in range(len(Sprime)-len(T)+1):
        for j in range(len(T)):
            if Sprime[i:i+len(T)][j] != '?' and\
            Sprime[i:i+len(T)][j] != T[j]:
                break
        else:
            ans = max(i,ans)
    if ans == -1:
        print('UNRESTORABLE')
    else:
        Sprime = list(Sprime)
        Sprime[ans:ans+len(T)] = T
        Sprime = ''.join(Sprime)
        print(Sprime.replace('?','a'))

Submission status: Accepted

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

get = []
for i in range(len(S)-len(T)+1):
    for s, t in zip(S[i:], T):
        if s != '?' and s != t:
            break
    else:
        get.append(i)
if not get:
    print('UNRESTORABLE')
    exit()

result = 'z' * 51
for i in get:
    x = S[:i] + T + S[i+len(T):]
    result = min(result, x.replace('?', 'a'))
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Ans = []
for i in range(len(S)-len(T)+1):
    same = True
    for j in range(len(T)):
        if S[i+j] != T[j] and S[i+j]!="?":
            same = False
    if same:
        out = ""
        for k in range(i):
            out += ("a" if S[k] == "?" else S[k])
        out += T
        for k in range(i+len(T), len(S)):
            out += ("a" if S[k] == "?" else S[k])
        Ans.append(out)
        
Ans.sort()
print(Ans[0] if Ans else "UNRESTORABLE")

Submission status: Accepted

Solution code:
a = list(input())
b = list(input())

n = len(a)
m = len(b)
s = n - m
check = 0
while s >= 0:
    count = 0
    for i in range(m):
        if a[s + i] == '?' or a[s + i] == b[i]:
            count += 1
        else:
            s -= 1
            break
    #print(a[s:s + m], count)
    if count == m:
        for i in range(m):
            a[s + i] = b[i]
        print(''.join(a).replace('?', 'a'))
        exit()

print('UNRESTORABLE')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
S = input()
T = input()

match_idx = 999
for i in range(len(S)-len(T)+1):
    unmatch = 0
    for s,t in zip(S[i:i+len(T)],T):
        if s != t and s != '?':
            unmatch = 1
    if unmatch == 0:
        match_idx = i


if match_idx > len(S)-len(T):
    print('UNRESTORABLE')
    exit()

xT = '?'*(match_idx) + T + '?'*(len(S)-len(T)-match_idx)

ans_list = []
for s,t in zip(S,xT):
    if s == '?' and t == '?':
        ans_list.append('a')
    elif s == '?':
        ans_list.append(t)
    else:
    # elif t == '?':
        ans_list.append(s)

print(''.join(ans_list))

Submission status: Accepted

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

if len(t)>len(s):
  print("UNRESTORABLE")
  exit()

ans = []

for i in range(len(s)-len(t)+1):
  for j in range(len(t)):
    if s[i+j] != t[j] and s[i+j]!='?':
      break
  else:
    ans.append((s[:i]+t+s[i+len(t):]).replace('?','a'))

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 14 17:08:52 2018
ABC076C
@author: maezawa
"""

sp = list(input())
t = list(input())

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

lt = len(t)
i = len(sp)-lt
ok = False
while i >=0:
    spc = sp[i:i+lt]
    if can_replace(spc,t):
        sp[i:i+lt] = t
        ok = True
        break
    i -= 1
if ok:
    for i in range(len(sp)):
        if sp[i] == '?':
            sp[i] = 'a'

    s = ''.join(sp)
    print(s)
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

if len(T) > len(S):
    print("UNRESTORABLE")
    exit()

ans = []

for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if S[i+j]!=T[j] and S[i+j]!='?':
            break
    else:
        ans.append((S[:i] + T + S[i+len(T):]).replace('?', 'a'))

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

Submission status: Accepted

Solution code:
a = input()
t = input()
n = len(a)
m = len(t)
if n<m:
  print("UNRESTORABLE")
  exit()
for i in range(n-m+1)[::-1]:
  flag = True
  for j in range(m):
    if a[i+j] != t[j] and a[i+j] != "?":
      flag = False
  if flag:
    b = a.replace("?","a")
    print(b[:i] + t + b[i+m:])
    exit()
        
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
ns = len(S)
T = input()
nt = len(T)
if T in S:
    print(S.replace('?', 'a'))
    exit()

for i in reversed(range(ns - nt + 1)):
    for x, y in zip(S[i:], T):
        if not (x == '?' or x == y):
            break
    else:
        r = S.replace('?', 'a')
        print(r[:i] + T + r[i + nt:])
        break
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

found = []
for i in range(len(S) - len(T) + 1):
    for cs,ct in zip(S[i:],T):
        if cs != '?' and cs != ct:
            break
    else:
        found.append(i)

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

ans = 'z' * 51
for i in found:
    tmp = S[:i] + T + S[i+len(T):]
    ans = min(ans, tmp.replace('?', 'a'))
print(ans)

Submission status: Accepted

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

ans = -1
for i in range(len(s)-len(t)+1):
    u = s[i:i+len(t)]
    flag = 0
    k = i
    for index, iu in enumerate(u):
        if not (iu == "?" or iu == t[index]):
            k = index
            flag = 1
    if flag == 0:
        ans = k

if ans == -1:
    print("UNRESTORABLE")
else:
    i = 0
    word = ""
    while i < len(s):
        if i == ans:
            word += t
            i += len(t)
        elif s[i] == "?":
            word += "a"
            i += 1
        else:
            word += s[i]
            i += 1
    print(word)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = lc()
t = lc()

ans = "z"*50

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

            if sij != '?' and sij != t[j]:
                break

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

Submission status: Accepted

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

found = []
for i in range(len(S) - len(T) + 1):
    for cs,ct in zip(S[i:],T):
        if cs != '?' and cs != ct:
            break
    else:
        found.append(i)

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

ans = 'z' * 51
for i in found:
    tmp = S[:i] + T + S[i+len(T):]
    ans = min(ans, tmp.replace('?', 'a'))
print(ans)

Submission status: Accepted

Solution code:
from itertools import zip_longest
S = input()
T = input()
NT = len(T)

candidate = []
for i in range(len(S)):
    for target, t in zip_longest(S[i:i+NT], T):
        if target != "?" and target != t:
            break
    else:
        candidate.append(S[:i] + T + S[i+NT:])

ans_candidate = []
for c in candidate:
    c = c.replace("?", "a")
    ans_candidate.append(c)

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

Submission status: Accepted

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

ans = -1
ansS = []
if len(S) < len(T):
    ans = -1
else:
    for i in range(len(S) - len(T) + 1):
        ans = 1
        for j in range(len(T)):
            #print(S[i + j], T[j])
            if S[i + j] != T[j] and S[i + j] != "?":
                ans = -1
                break
        if ans == 1:
            thisS = S[:i] + T + S[i + len(T):]
            for j in range(len(S)):
                if thisS[j] == "?":
                    thisS[j] = "a"
            ansS.append(thisS)

if len(ansS) == 0:
    print("UNRESTORABLE")
else:
    ansS.sort()
    for i in ansS[0]:
        print(i, end = "")
    print()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

    def match(s1, t):
        return all(a in (b, '?') for a, b in zip(s1, t))

    substrings = [s[i:i+len(t)] for i in range(len(s)-len(t)+1)]
    res = 'UNRESTORABLE'
    for ss in reversed(substrings):
        if match(ss, t):
            left, _, right = s.rpartition(ss)
            res = left + t + right
            res = res.replace('?', 'a')
            break
    print(res)

def main4():
    s = input() # abcdr?tc????
    t = input() # coder

    def match(sub, t):
        for i in range(len(t)):
            if sub[i] == "?":
                continue
            if sub[i] == t[i]:
                continue
            return False
        return True

    substrings = []
    for i in range(len(s)-len(t)+1):
        substrings.append(s[i:i+len(t)])
    # ['c????', 'tc???', '?tc??', 'r?tc?', 'dr?tc', 'cdr?t', 'bcdr?', 'abcdr']

    for sub in reversed(substrings):
        if match(sub, t):
            left, _, right = s.rpartition(sub)
            result = (left + t + right).replace("?", "a")
            print(result)
            exit()
    print('UNRESTORABLE')


def main3():
    def product_mask_s(s, c):
        import itertools
        s_products = list(itertools.product(s, c))

        s_masks = []
        len_s = len(s)
        for products in itertools.product(range(2), repeat=len_s):
            t = ""
            for i in range(len_s):
                t += s_products[i][products[i]]
            s_masks.append(t)
        return s_masks

def main2():
    """ TLE WA """
    # atcoder
    # ?tc????
    # ?t?o?er
    import itertools
    s_prime = input()
    t_origin = input()
    len_t = len(t_origin)
    t_products = list(itertools.product(t_origin, "?"))

    t_kouho = []
    for products in itertools.product(range(2), repeat=len_t):
        t = ""
        for i in range(len_t):
            t += t_products[i][products[i]]
        t_kouho.append(t)

    for t in t_kouho:
        find_i = s_prime.find(t)
        if find_i == -1:
            continue
        # 見つかったなら、tを代入する
        ans = "{}{}{}".format(s_prime[:find_i], t_origin, s_prime[find_i+len_t:])
        print(ans.replace("?", "a"))
        exit()
    print("UNRESTORABLE")


def main():
    """ WA """
    s_change = lambda s, i, c: "{}{}{}".format(s[:i-1], c, s[i-1+1:])

    s_prime = input() # ?tc????
    t_origin = input()       # coder

    # s_prime の中から tの部分文字列の位置を探す
    len_t = len(t_origin)
    t = t_origin

    t_kouho = [t]
    for t_i in range(1, len_t):
        t_tail = s_change(t, len_t-t_i+1, "?")
        t_head = s_change(t, t_i, "?")
        if t_tail == t_head:
            t_kouho.append(t_tail)
        else:
            t_kouho.append(t_tail)
            t_kouho.append(t_head)
        t = s_change(t_tail, t_i, "?")
        if t == "?"*len_t:
            break
    print(t_kouho)

    for t in t_kouho:
        t_i = s_prime.find(t)
        if t_i == -1:
            continue
        # 見つかったなら、tを代入する
        ans = "{}{}{}".format(s_prime[:t_i], t_origin, s_prime[t_i+len_t:])
        print(ans.replace("?", "a"))
        exit()
    print("UNRESTORABLE")

if __name__ == '__main__':
    main4()

Submission status: Accepted

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

n = len(s)
delta = len(t)
ans_list = []

for i in range(n-delta+1):
	for j in range(delta):
		if s[i+j] == '?' or s[i+j] == t[j]:
			pass
		else:
			break
	else:
		ans_list.append((s[:i]+t+s[i+j+1:]).replace('?', 'a'))

ans_list.sort()

if ans_list:
	print(ans_list[0])
else:
	print('UNRESTORABLE')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = input()
t = input()
start = -1
for i in range(len(s)-len(t)+1):
    flag = 1
    for j in range(len(t)):
        if s[i+j] != t[j] and s[i+j] != "?":
            flag = 0
    if flag:
        start = i

if start == -1:
    print("UNRESTORABLE")
else:
    ans = s[:start] + t + s[start+len(t):]
    for i in ans:
        if i == "?":
            print("a", end="")
        else:
            print(i, end="")
    print()

Submission status: Accepted

Solution code:
#ABC076C
S_dash=str(input())
T=str(input())
location=-1
for i in range(len(S_dash)-len(T)+1):
    j=0
    while j<=len(T)-1:
        if S_dash[i+j]==T[j] or S_dash[i+j]=='?':
            if j==len(T)-1:
                location=i
            j+=1
        else:
            break
if location>=0:
    S_dash_list=list(S_dash)
    for i in range(len(T)):
        S_dash_list[location+i]=T[i]
    S_tmp="".join(S_dash_list)
    S=S_tmp.replace('?','a')
if location>=0:
    print(S)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
def c_dubious_document_2(S, T):
    # S:鍵の文字列のいくつかを'?'で置き換えたもの
    # T:Sの中に含まれている可能性のある文字列
    candidate = []  # 解の候補('?'を含む)
    for i in range(len(S) - len(T) + 1):
        # Sの中にTを置く場所がないままTがSの末尾をはみ出すなら、復元不可能が確定
        for j in range(len(T)):
            if S[i + j] != '?' and S[i + j] != T[j]:
                break  # Sのi文字目以降にTを埋め込める可能性がなくなった
        else:
            # TをSのi文字目以降に埋め込める。解の候補とする
            candidate.append(S[:i] + T + S[i + len(T):])
    if len(candidate) == 0:
        ans = 'UNRESTORABLE'
    else:
        tmp = []
        for s in candidate:
            tmp.append(s.replace('?', 'a'))
        ans = sorted(tmp)[0]
    return ans

S = input()
T = input()
print(c_dubious_document_2(S, T))

Submission status: Accepted

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

answers = []
for i in range(len(s_)-len(t)+1):
    ans = s_
    for j in range(len(t)):
        matched = True
        if s_[i+j] != t[j] and s_[i+j] != '?':
            matched = False
            break
        else:
            ans = ans[:i+j] + t[j] + ans[i+j+1:]
    if matched:
        ans = ans.replace('?', 'a')
        answers.append(ans)
# print(answers)

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

Submission status: Accepted

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

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


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