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 sys

def is_macth(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_macth(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()
T_high = len(T) - 1

ans = "~~UNRESTORABLE~~"

for i in range(T_high, len(S)):
    flg = False
    for j in range(T_high + 1):
        c = S[i + j - T_high]
        if c != '?' and c != T[j]:
            flg = True
            break
    if not flg:
        buf = S.replace("?", "a")
        ans = min(ans, buf[:(i - T_high)] + T + buf[(i + 1):])

print(ans.replace("~", ""))

Submission status: Accepted

Solution code:
S = input()
T = input()
T_high = len(T) - 1

ans = []

for i in range(T_high, len(S)):
    flg = False
    for j in range(T_high + 1):
        c = S[i + j - T_high]
        if c != '?' and c != T[j]:
            flg = True
            break
    if not flg:
        buf = S.replace("?", "a")
        ans.append(buf[:(i - T_high)] + T + buf[(i + 1):])

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

Submission status: Accepted

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

answer = ''
for i in range(len(S)-len(T)+1):
    if any([S[i+j] != '?' and S[i+j] != T[j] for j in range(len(T))]):
        continue
    tmp = (S[:i] + T + S[i+len(T):]).replace('?', 'a')
    if answer == '' or tmp < answer:
        answer = tmp

print('UNRESTORABLE' if answer == '' else answer)

Submission status: Accepted

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

answer = ''
for i in range(len(S)-len(T)+1):
    if any([S[i+j] != '?' and S[i+j] != T[j] for j in range(len(T))]):
        continue
    tmp = (S[:i] + T + S[i+len(T):]).replace('?', 'a')
    if answer == '' or tmp < answer:
        answer = tmp 

if answer == '':
    print('UNRESTORABLE')
else:
    print(answer)

Submission status: Accepted

Solution code:
from copy import deepcopy

S = input()
T = input()

answer = ''
for i in range(len(S)-len(T)+1):
    found = True
    if any([S[i+j] != '?' and S[i+j] != T[j] for j in range(len(T))]):
        continue
    tmp = deepcopy(S).replace('?', 'a')
    tmp = tmp[:i] + T + tmp[i+len(T):]
    if answer == '' or tmp < answer:
        answer = tmp

if answer == '':
    print('UNRESTORABLE')
else:
    print(answer)

Submission status: Accepted

Solution code:
from copy import deepcopy

S = input()
T = input()

answer = ''
for i in range(len(S)-len(T)+1):
    found = True
    for j in range(len(T)):
        if S[i+j] != '?' and S[i+j] != T[j]:
            found = False
            break
    if found:
        tmp = deepcopy(S).replace('?', 'a')
        tmp = tmp[:i] + T + tmp[i+len(T):]
        if answer == '' or tmp < answer:
            answer = tmp

if answer == '':
    print('UNRESTORABLE')
else:
    print(answer)

Submission status: Accepted

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

len_SS = len(SS)
len_T = len(T)

flag = False
for i in range(len_SS - len_T + 1):
    window = SS[len_SS - len_T - i:len_SS - i]
    matching = True
    for j in range(len_T):
        if not window[j] == "?":
            if window[j] != T[j]:
                matching = False
                break

    if matching:
        flag = True
        break

if flag:
    SS = list(SS)
    SS[len_SS - len_T - i:len_SS - i] = list(T)
    SS = "".join(SS)
    SS = SS.replace("?", "a")
    print(SS)

else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    S = input()
    T = input()

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

    if len(ans) == 0:
        print("UNRESTORABLE")
    else:
        t = min(ans)
        print(t.replace("?", "a"))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S=list(input())
T=list(input())
A=[]
for i in range(len(S)-len(T)+1):
    is_ok=1
    for j in range(i, i+len(T)):
        if S[j]!=T[j-i] and S[j]!='?':
            is_ok=0
    if is_ok:
        tmp=S[:]
        is_con=1
        for j in range(i, i+len(T)):
            tmp[j]=T[j-i]
        A.append(tmp)
if len(A)==0:
    print("UNRESTORABLE")
    exit(0)
B=[]
for a in A:
    for i in range(len(a)):
        if a[i]=='?':
            a[i]='a'
    B.append(''.join(a))
B.sort()
print(B[0])

Submission status: Accepted

Solution code:
Sd=input().strip()
T=input().strip()
S=""
for i in range(len(Sd)-len(T),-1,-1):
    sub=Sd[i:i+len(T)]
    for t,s in zip(T,sub):
        if s!="?" and s!=t:
            break
    else:
        S=Sd[:i]+T+Sd[i+len(T):]
        break
if S != "":
    print(S.replace("?", "a"))
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

STRMAX = "z" * 50
ans = STRMAX
for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == '?':
            if j == len(T)-1:
                work = S[:i] + T + S[i+j+1:]
                work = work.replace('?','a')
                if work < ans:
                    ans = work
        else:
            break

if ans == STRMAX:
    print("UNRESTORABLE")
else:
    print(ans)

Submission status: Accepted

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

ans = 'UNRESTORABLE'
for i in range(len(s) - len(t), -1, -1):
    ds = s[i:i + len(t)]
    flg = True
    for j in range(len(t)):
        if (ds[j] != t[j]) and (ds[j] != '?'):
            flg = False
            break
    if flg:
        ans = s[:i] + t + s[i + len(t):]
        ans = ans.replace('?', 'a')
        break
print(ans)

Submission status: Accepted

Solution code:
res = 'z'*100
s = input()
t = input()
l = len(t)
for i in range(len(s)-l+1):
    string = s[i:i+l]
    flag = False
    for (j,k) in zip(string,t):
        if j!=k and j!='?':
            flag = True
            break
    if flag:
        continue
    tmp = s[:i] + t + s[i+l:]
    res = min(res, tmp.replace('?','a'))
if res=='z'*100:
    print('UNRESTORABLE')
else:
    print(res)

Submission status: Accepted

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

def coincide(s, t):
    res = True
    for i in range(0, len(s)):
        if s[i] != t[i] and s[i] != '?':
            res = False
    return res

ss = []
for i in range(0, len(sd) - len(t) + 1):
    if coincide(sd[i:i+len(t)], t):
        ss.append((sd[0:i]+t+sd[i+len(t):]).replace('?', 'a'))
    else:
        continue

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

Submission status: Accepted

Solution code:
import re

S = input().replace('?', '.')
T = input()

ans = []

for i in range(len(S)-len(T), -1, -1):
    if re.match(S[i:i+len(T)], T):
        ans.append((S[:i] + T + S[i+len(T):]).replace('.', 'a'))

print(min(ans) if ans else 'UNRESTORABLE')

Submission status: Accepted

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

a = [] #kouho
tl = len(t)
sl = len(s)

for i in range(sl-tl+1):
    k = 0
    for j in range(tl):
        if t[j] == s[i+j] or s[i+j] == '?':
            k += 1
        else:
            break
    if k == tl:
        if i == 0 and i == sl-tl:
            w = t
        elif i == 0:
            w =  t + ['a' if hj == '?' else hj for hj in s[i+tl::]]
        elif i == sl-tl:
            w =  ['a' if hj == '?' else hj for hj in s[:i:]] + t
        else:
            w = ['a' if hj == '?' else hj for hj in s[:i:]] + t + ['a' if hj == '?' else hj for hj in s[i+tl::]]
        ass = ''
        for jkl in range(len(w)):
            ass += w[jkl]
        a.append(ass)
        
#print(a)

if len(a)== 0:
    print('UNRESTORABLE')
else:
    print(min(a))

Submission status: Accepted

Solution code:
def judge(Sd:str, T:str) -> str:
    lenS = len(Sd)
    lenT = len(T)
    
    #1. Tに一致する部分列を検索
    start = -1
    for i in range(lenS - lenT + 1):
        isMatch = True
        for j in range(lenT):
            if not (Sd[i+j] == T[j] or Sd[i+j] == "?"):
                isMatch = False
            
        if isMatch:
            start = i
            
    if start == -1:
        return "UNRESTORABLE"
    
    #2. あたらしい文字列を構成する
    # i:i+|T|をTで置き換え、?をaで埋める
    S = Sd[:start] + T + Sd[start+lenT:]
    ans = ""
    for i in range(lenS):
        if S[i] == "?":
            ans = ans + "a"
        else:
            ans = ans + S[i]
            
    return ans
            


Sd = input()
T = input()

S = judge(Sd, T)

print(S)

Submission status: Accepted

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

for i, s_i in tuple(zip(range(len(s_)-len(t)+1), [s_[x:x+len(t)] for x in range(len(s_)-len(t)+1)]))[::-1]:
    if all((s_i_c == "?" or s_i_c == t[j]) for j, s_i_c in enumerate(s_i)):
        print((s_[:i] + t + s_[i+len(t):]).replace("?", "a"))
        exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
# enumerate の 番号が降順 のもの. 最後が0になる
def reverse_enumerate(l):
	for i, l_i in zip(range(len(l)-1, -1, -1), l):
		yield i, l_i
# n-gram(調べて)
def n_gram(s, n):
	return [s[i:i+n] for i in range(len(s)-n+1)]
# 二つの文字列が等しいかを調べる. 全ての文字と等しくなる文字を設定可能(all_match)
def is_equal_match(s1, s2, all_match=[]):
	for i in range(len(s1)):
		if not (s1[i] == s2[i] or s1[i] in all_match):
			return False
	return True
# 指定した index に 文字列を埋め込む
def fill_in_str(text, s, n):
	return text[:n] + s + text[n+len(s):]

s_ = input()
t = input()

for i, s_i in reverse_enumerate(n_gram(s_, len(t))[::-1]):
	if is_equal_match(s_i, t, all_match="?"):
		print(fill_in_str(s_, t, i).replace("?", "a"))
		exit()
print("UNRESTORABLE")

Submission status: Accepted

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

for i, s_i in tuple(zip(range(len(s_)-len(t)+1), [s_[x:x+len(t)] for x in range(len(s_)-len(t)+1)]))[::-1]:
	if all((s_i_c == "?" or s_i_c == t[j]) for j, s_i_c in enumerate(s_i)):
		print(s_[:i].replace("?", "a") + t + s_[i+len(t):].replace("?", "a"))
		exit()
print("UNRESTORABLE")

Submission status: Accepted

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

answer = []
for i in range(len(s)-len(t)+1):
    a = 0
    for j in range(len(t)):
        if s[i+j]=='?' or s[i+j]==t[j]:
            a += 1
    if a == len(t):
        ans = []
        for k in range(len(s)):
            if i <= k < i + len(t):
                ans.append(t[k-i])
            elif s[k]=='?':
                ans.append('a')
            else:
                ans.append(s[k])
        ans = ''.join(ans)
        answer.append(ans)
if len(answer)==0:
    print('UNRESTORABLE')
else:
    print(sorted(answer)[0])

Submission status: Accepted

Solution code:
s=input()
t=input()
u=len(t)
for i in range(len(s)-u,-1,-1):
  for j in range(u):
    if s[i+j] != '?' and s[i+j] != t[j]:
      break
  else:
    print('{}{}{}'.format(s[:i],t,s[i+u:]).replace('?','a'))
    break
else:
  print('UNRESTORABLE')

Submission status: Accepted

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

def main():
    s = input()
    t = input()
    i = -1

    for j in range(len(s) - len(t) + 1):
        if all(a == '?' or a == b for a, b in zip(s[j:], t)):
            i = j
    if i == -1:
        print('UNRESTORABLE')
    else:
        ans = s[:i] + t + s[i+len(t):]
        ans = ans.replace('?', 'a')
        print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = list(input())
t = input()
T = list(t)
NS = len(S)
NT = len(T)
j = -1
if NS < NT:
    pass
else:
    for i in range(NS - NT,-1,-1):
        j = i
        for k in range(NT):
            if S[i+k] == T[k] or S[i+k] == '?':
                pass
            else:
                j = -1
                break
        if j != -1:
            break
            
if j == -1:
    print('UNRESTORABLE')
else:
    k = 0
    ans = ''
    while k != NS:
        if k == j:
            ans += t
            k += NT
        else:
            if S[k] == '?':
                ans += 'a'
            else:
                ans += S[k]
            k += 1
    print(ans)

Submission status: Accepted

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

s=input()
t=input()
n=len(t)
l=[]
for i in range(len(s)-n+1):
    if chk(s[i:i+n],t):
        l.extend([s[:i]+t+s[i+n:]])
if not l:
    print("UNRESTORABLE")
else:
    l=sorted(l)
    print(l[0].replace("?","a"))

Submission status: Accepted

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

ls = len(s)
lt = len(t)

ans = 'UNRESTORABLE'
for i in sorted(range(ls - lt + 1), reverse=True):
    ds = s[i:i+lt]
    flg = True
    for j in range(lt):
#        if not((ds[j] == t[j]) or (ds[j] == '?')):
        if (ds[j] != t[j]) and (ds[j] != '?'):
            flg = False
            break
    if flg:
        ans = s[:i] + t + s[i+lt:]
        ans = ans.replace('?', 'a')
        break
print(ans)

Submission status: Accepted

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

search_str = ["?" for x in range(len(T))]

last_occ = -1

for i in range(len(S)) :    
    if i < len(S) - len(T) + 1:
        ok_cnt = 0
        for j in range(len(T)) :
            if S[i + j] == T[j] or S[i + j] == "?" :
                ok_cnt += 1

        if ok_cnt == len(T) :
            last_occ = i
            
if last_occ != -1 :
    str_tmp = S[0:last_occ] + T + S[last_occ + len(T):]
    print(str_tmp.replace("?", "a"))
else :
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s2 = list(input())
t = list(input())

s2.reverse() 
t.reverse() 

ans = 'UNRESTORABLE'

cands = []

for i in range(len(s2) - len(t) + 1):
    for j in range(len(t)):
        if t[j] == s2[i + j] or s2[i + j] == '?':
            if j == len(t) - 1:
                s2[i:i+j + 1] = t
                s2.reverse()
                ans = ''.join(s2)
                ans = ans.replace('?', 'a')
                cands.append(ans)
                break
            else:
                continue
        else:
            break

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

Submission status: Accepted

Solution code:
# time complexity: O(|S - T|*|T|)
# space complexity: O(|S - T|)

S_d = input()
T = input()
len_T = len(T)


def replacable(string, T):
    for char_s, char_t in zip(string, T):
        if char_s == char_t or char_s == '?':
            continue
        else:
            return False
    else:
        return True


# see if S_d can contain T
t_start_index_list = []
# O(|S - T|)
for start_idx in range(len(S_d) - len_T + 1):
    string = S_d[start_idx:start_idx + len_T]
    # O(|T|)
    if replacable(string, T):
        t_start_index_list.append(start_idx)

if len(t_start_index_list) == 0:
    print("UNRESTORABLE")
else:
    S_candidate_list = []
    for t_start_index in t_start_index_list:
        S = S_d[:t_start_index] + T + S_d[t_start_index + len_T:]
        S = S.replace('?', 'a')
        S_candidate_list.append(S)
    S_candidate_list.sort()
    print(S_candidate_list[0])

Submission status: Accepted

Solution code:
s = input()
t = input()
ans = []
if len(t)>len(s):
  print("UNRESTORABLE")
else:
  for i in range(len(s)-len(t)+1):
    st = ""
    for j in range(len(t)):
      if s[j+i]=="?" or s[j+i]==t[j]:
        st+=t[j]
      else:
        break
    if len(st)==len(t):
      st = s[:i]+st+s[i+len(t):]
      ans.append(st.replace("?", "a"))
  if len(ans):
    print(sorted(ans)[0])
  else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import re
sd = input()
ts =  input()

def f():
    cand = []
    for i in range(len(sd) - len(ts) + 1):
        p = sd[i:i + len(ts)]
        pat = re.compile(p.replace('?', '.'))
        if re.match(pat, ts):
            cand.append((sd[:i] + ts + sd[i+len(ts):]).replace('?', 'a'))
    return sorted(cand)[0] if cand else 'UNRESTORABLE'
print(f())

Submission status: Accepted

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

N = len(S2)


def check(S, T):
    for i in range(len(S)):
        if S[i] == '?' or S[i] == T[i]:
            continue
        else:
            return False
    return True


S = 'UNRESTORABLE'
if len(T) <= N:
    C = []
    for i in range(N - len(T) + 1):
        j = i + len(T)
        if (S2[i] == T[0] or S2[i] == '?') and check(S2[i:j], T):
            S = list(S2)
            S[i:j] = list(T)
            S = ''.join(S).replace('?', 'a')
            C.append(S)
    if len(C) > 0:
        S = sorted(C)[0]
print(S)

Submission status: Accepted

Solution code:
S_prime = input()
T = input()
C = []
for i in range(len(S_prime) - len(T) + 1):
  match = True
  for j in range(len(T)):
    if S_prime[i + j] == '?':
      continue
    elif S_prime[i + j] != T[j]:
      match = False
      break
  if match:
    C.append(i)
Ss = []
for c in C:
  S = ''
  for i, s in enumerate(S_prime):
    if i < c or i >= c + len(T):
      S += s if s != '?' else 'a'
    elif i == c:
      S += T
  Ss.append(S)
if Ss:
  print(sorted(Ss)[0])
else:
  print('UNRESTORABLE')

Submission status: Accepted

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

def check(S,T):
	for i in range(len(S)):
		if S[i] != T[i] and S[i] != "?":
			return False
	return True

def pad(S,T,idx):
	tmp = ""
	for i in range(len(S)):
		if i < idx:
			tmp += "a" if S[i] == "?" else S[i]
		elif i < idx + len(T):
			tmp += T[i-idx]
		else:
			tmp += "a" if S[i] == "?" else S[i]
	return tmp

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


a = []
for i in range(len(S)-len(T)+1):
	if check(S[i:i+len(T)],T) == True:
		a.append(pad(S,T,i))
if len(a) == 0:
	print('UNRESTORABLE')
	exit()

print(sorted(a)[0])

Submission status: Accepted

Solution code:
import re
sd = input()
ts =  input()

def f():
    for i in range(len(sd) - len(ts) + 1):
        p = sd[len(sd) - i - len(ts):len(sd) - i]
        pat = re.compile(p.replace('?', '.'))
        if re.match(pat, ts):
            lsd = list(sd)
            lsd[len(sd) - i - len(ts):len(sd) - i] = list(ts)
            print(''.join(lsd).replace('?', 'a'))
            return
    print('UNRESTORABLE')
f()

Submission status: Accepted

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

a=len(s)
b=len(t)
f=0
for i in range(-1,-a+b-2,-1):
    f=0
    for j in range(-1,-b-1,-1):
        if (s[i+j+1]!="?"and t[j]!=s[i+j+1]):
            f=1
            break
    if f==1: continue
    for k in range(-1,-b-1,-1):
        s[i+k+1]=t[k]
    for p in range(a):
        if s[p]=="?":
            s[p]="a"
    print("".join(s))
    exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys
s = raw_input()
t = raw_input()

ls = len(s)
lt = len(t)
if ls < lt :
	print 'UNRESTORABLE'
	sys.exit()

for i in range(ls-lt,-1,-1):
	count = 0
	for j in range(lt):
		if s[i+j] == '?' or s[i+j] == t[j]:
			count += 1
		else :
			break
	if count == lt:

		print s[:i].replace('?','a')+t+s[i+lt:].replace('?','a')
		sys.exit()

print 'UNRESTORABLE'

Submission status: Accepted

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

S2 = input()
T = input()

# TをS2に当て込める位置の数ループ
for i in range(len(S2)-len(T)+1):
    # S2の後ろから、Tの文字数分切り出し
    tmpS = S2[len(S2)-len(T)-i:len(S2)-i]
    
    # Tの文字数分ループ
    for j in range(len(T)):
        # 対象の文字か?と一致しなければbreak
        if tmpS[j] != T[j] and tmpS[j] !=  "?":
            break
        # 最後まで条件を満たしたら出力準備
        if j == len(T) - 1:
            # 元のS2の該当箇所にTを当て込む
            S = S2[:len(S2)-len(T)-i] + T + S2[len(S2)-i:]
            # 残った?は全部a(辞書順最小がいいから)
            S = S.replace("?", "a")
            print(S)
            exit()
# 最後まで条件を満たす箇所がなければこっち
print("UNRESTORABLE")

Submission status: Accepted

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

insert_index = -1
while True:

    flag = True

    if left + len(t) - 1 == len(s):
        break

    for a, b in zip(s[left:left+len(t)], t):
        if a != b and a != "?":
            flag = False
            break
    if flag:
        insert_index = max(insert_index, left)
    left += 1


if insert_index == -1:
    print("UNRESTORABLE")
else:
    next_word = s[:insert_index] + t + s[insert_index+len(t):]

    ans = next_word.replace("?", "a")
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S=input()
T=input()
candidates=[]
strings=[]
for i in range(len(S)-len(T)+1):
    flag=1
    for j in range(len(T)):
        if S[i+j]=='?':
            pass
        else:
            if S[i+j]!=T[j]:
                flag=0
                break
            else:
                pass
    if flag:
        replacement=S[:i]+T+S[i+len(T):]
        strings.append(replacement)

for string in strings:
    s=''
    for i in range(len(string)):
        if string[i]=='?':
            s=s+'a'
        else:
            s=s+string[i]
    candidates.append(s)

if len(candidates)==0:
    print("UNRESTORABLE")
    quit()
elif len(candidates)==1:
    print(candidates[0])
    quit()
else:
    candidates.sort()
    print(candidates[0])
    quit()

Submission status: Accepted

Solution code:
S = input()
T = input()
if len(T) > len(S):
    print('UNRESTORABLE')
    exit()

cands = []
for i in range(len(S)-len(T)+1):
    for c1,c2 in zip(T,S[i:]):
        if c1 == c2: continue
        if c2 == '?': continue
        break
    else:
        tmp = S[:i] + T + S[i+len(T):]
        cands.append(tmp.replace('?','a'))
if cands:
    print(sorted(cands)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

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

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

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
s = input()
t = input()
a =[]
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
    if f == True:
        a.append(i)

s = s.replace('?','a')
if len(a) !=0:
    print(s[:a[-1]]+t+s[a[-1]+len(t):])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:

SD = list(input())
T = list(input())

S = []
flag = -1

for i in range(len(SD)-len(T), -1, -1):
    Sub_S = SD[i:i+len(T)]
    for t,s in zip(T, Sub_S):
        if s!="?" and s!=t:
            break
    else:
        S=SD[:i]+T+SD[i+len(T):]
        flag = 1
        break
    
if flag<0:
    print("UNRESTORABLE")
else:
    for i in range(len(SD)):
        if S[i]=="?":
            S[i]="a"

print("".join(S))

Submission status: Accepted

Solution code:
def check(x,y,n):
	for i in range(n):
		if x[i] != '?' and y[i] != x[i]:
			return False
	return True
			
S = list(input())
T = list(input())
S_len = len(S)
T_len = len(T)

if T_len > S_len:
	print('UNRESTORABLE')
	quit()
	
answer = []

for i in range(S_len - T_len + 1):
	S_dush = S[i:i+T_len]
	if check(S_dush,T,T_len):
		ans = S[:]
		ans[i:i+T_len] = T
		for j in range(S_len):
			if ans[j] == '?':
				ans[j] = 'a'
		answer.append(''.join(ans))
if answer:
	answer.sort()
	print (answer[0])
else:
	print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s = list(input())
slen = len(s)
t = list(input())
tlen = len(t)
box = []


def check(wh, phase=0):
    if wh + phase >= slen:
        return False
    if s[wh + phase] == "?" or s[wh + phase] == t[phase]:
        if phase + 1 == tlen:
            return True
        return check(wh, phase + 1)
    return False


def ume(arr):
    tmp = [e for e in arr]
    for i, e in enumerate(arr):
        if e == "?":
            tmp[i] = "a"
    return tmp



for i, e in enumerate(s):
    if check(i):
        box.append(ume(s[:i] + t + s[i + tlen:]))

if not box:
    print("UNRESTORABLE")
else:
    box.sort
    print("".join(box[-1]))

Submission status: Accepted

Solution code:
s = input()
t = input()
ans = ""
po = False

for i in range(len(s)-len(t)+1):
    if s[i] in [t[0],"?"]:
        if all([s[i + j] in [t[j], "?"]for j in range(len(t))]):
            if not po:
                ans = (s[:i] + t + s[i + len(t):]).replace("?","a")
                po = True
            temp = (s[:i] + t + s[i + len(t):]).replace("?","a")
            if temp < ans:
                ans = temp
            


if ans:
    print(ans.replace("?", "a"))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s = []
s += input()
t = []
t += input()
u = -1
for i in range(len(s) - len(t) + 1):
    no = 0
    if s[i] == t[0] or s[i] == "?":
        for k in range(len(t) - 1):
            if s[i + k + 1] != t[k + 1] and s[i + k + 1] != "?":
                no = 1
                break
        if no == 0:
            u = i
if u == -1:
    print("UNRESTORABLE")
else:
    for i in range(len(s)):
        if u <= i <= u + len(t) - 1:
            s[i] = t[i-u]
        elif s[i] == "?":
            s[i] = "a"
    print("".join(s))

Submission status: Accepted

Solution code:
Sd=input().strip()
T=input().strip()
S=None
for i in range(len(Sd)-len(T),-1,-1):
    sub=Sd[i:i+len(T)]
    for t,s in zip(T,sub):
        if s!="?" and s!=t:
            break
    else:
        S=Sd[:i]+T+Sd[i+len(T):]
        break
print(S.replace("?","a") if S else "UNRESTORABLE")

Submission status: Accepted

Solution code:
s = input()
t = input()
ans = ''
if len(s) >= len(t):
    for i in range(len(s)-len(t), -1, -1):
        s_tmp = s[i:i+len(t)]
        flag = 0
        for j in range(len(t)):
            if s_tmp[j] == '?':
                s_tmp = t[:j+1] + s_tmp[j+1:]
            elif s_tmp[j] != t[j]:
                flag = 1
                break
        if flag == 0:
            ans = s[:i] + t + s[i+len(t):]
            break
else:
    flag = 1

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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


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


cands = []
for i in range(len(s) - len(t) + 1):
    if check(s[i:i + m], t):
        cands.append((s[:i] + t + s[i + m:]).replace('?', 'a'))
if cands:
    cands.sort()
    print(cands[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

ans = []
sl = len(S)
tl = len(T)
for i in range(sl-tl+1):
	if S[i] == '?' or S[i] == T[0]:
		flg = True
		for j in range(1,tl):
			if S[i+j] != '?' and S[i+j] != T[j]:
				flg = False
				break

		if flg:
			s = S[:i] + T + S[i+tl:]
			ans.append(s.replace('?','a'))

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

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

Submission status: Accepted

Solution code:
S = input()
T = input()
len_s = len(S)
len_t = len(T)
candidates = []
for i in range(len_s - len_t + 1):
    insertable = True
    for j in range(len_t):
        if S[i+j] != "?" and T[j] != S[i+j]:
            insertable = False
    if insertable:
        u = ""
        for k in range(len_s):
            if k >=i and k <= i + len_t - 1:
                u+=T[k-i]
            else:
                if S[k] == "?":
                    u+="a"
                else:
                    u+=S[k]
        candidates.append(u)   
if not candidates:
    print("UNRESTORABLE")
else:
    candidates.sort()
    print(candidates[0])

Submission status: Accepted

Solution code:
import sys

S_dash = input()
T = input()

for i in range(len(S_dash) - len(T), -1, -1):
    for idx, s in enumerate(T):
        if S_dash[i + idx] != s and S_dash[i + idx] != '?':
            break
    else:
        S = S_dash[:i] + T + S_dash[i + len(T):]
        S = S.replace('?', 'a')
        print(S)
        sys.exit()
print('UNRESTORABLE')

Submission status: Accepted

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

s = len(S)
t = len(T)
ans = 'UNRESTORABLE'

if len(T) <= len(S):
    for i in range(s - t + 1): # iから|T|文字を見る
        for j in range(t):
            if S[i + j] != T[j] and S[i + j] != '?':
                break
            if j == t - 1:
                c = S[:i] + T + S[t + i:]
                ans = c.replace('?', 'a')

print(ans)