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:
S = input()
T = input()

flg = 0
ans = ""
temp = ""
for i in range(len(S)):
    flg = 0
    if len(S[i:]) < len(T):
        break
    for j in range(len(T)):
        if S[i:][j] != T[j] and S[i:][j] != "?":
            flg = 1
    if flg == 0:
        temp = (S[0:i]+T+S[i+len(T):]).replace("?", "a")
        if temp < ans or ans == "":
            ans = temp
    
print(ans if ans != "" else "UNRESTORABLE")

Submission status: Accepted

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

arr = []
length = len(b)

for i in range(len(a) - len(b) + 1):
  tmp = copy.deepcopy(a)
  formable = True
  for j, bs in enumerate(b):
    if bs == a[i+j] or a[i+j] == "?":
      continue
    formable = False
  if formable == False:
    continue

  for k, bbs in enumerate(b):
    tmp[i+k] = bbs

  for l, sa in enumerate(tmp):
    if sa == "?":
      tmp[l] = "a"
  arr.append("".join(tmp))
nn = sorted(arr)
ans = "UNRESTORABLE" if len(arr) == 0 else nn[0]

print(ans)

Submission status: Accepted

Solution code:

import re


def match(s, t):
    pat = "".join(map(lambda x: "({0}|[?])".format(x), list(t)))
    r = re.compile(pat)

    for pos in range(len(s) - len(t), -1, -1):
        m = r.match(s[pos:])
        if m:
            start, end = m.span()
            return (pos+start, pos+end)
    return None

def foo(s, t):
    m = match(s, t)
    if m is None:
        return "UNRESTORABLE"
    start, end = m
    r = s[:start] + t + s[end:]
    return r.replace("?", "a")


if __name__ =="__main__":
    s = input()
    t = input()
    print(foo(s, t))

Submission status: Accepted

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

if len(s) < len(t):
    print('UNRESTORABLE')
    exit(0)

ans,tmp = "", ""

for i in range(len(s)):
    if len(s[i:]) < len(t):
        ok = False
        break
    ok = True
    for j in range(len(t)):
        if s[i:][j] != t[j] and s[i:][j] != "?":
            ok = False
    if ok:
        tmp = (s[:i]+ t + s[i+len(t):]).replace("?", "a")
        if tmp < ans or ans == "":
            ans = tmp

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

Submission status: Accepted

Solution code:
import re


s_dash = input()
t = input()
for i in reversed(range(len(s_dash)-len(t)+1)):
    matchobj = re.match(s_dash[i:i+len(t)].replace("?", "[a-z]"), t)
    if matchobj:
        _s = list(s_dash)
        _s[i:i+len(t)] = t
        print("".join(_s).replace("?", "a"))
        exit()
print("UNRESTORABLE")
exit()

Submission status: Accepted

Solution code:


s = input()
t = input()

s1 = len(s)
t1 = len(t)

if t1 > s1:
    print("UNRESTORABLE")
    exit(0)

res = ""
for i in range(s1):
    u = s[i:i + t1]
    if len(u) != t1: continue
    ok = True
    for j in range(t1):
        if u[j] != '?' and u[j] != t[j]:
            ok = False
    if ok:
        tmp = (s[:i] + t + s[i+t1:]).replace("?", "a")
        if res == "" or tmp < res:
            res = tmp

if res: print(res)
else  : print("UNRESTORABLE")

Submission status: Accepted

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

def f(s, t):
  l = "?"*len(t)
  found = False
  # if l in s:
  #   print('done')
  for i in range(len(s)-len(t)+1):
    temp = s[len(s)-len(t)-i:len(s)-i]
    count = 0
    for j in range(len(temp)):
      if t[j] == temp[j] or temp[j] == "?":
        count += 1
    if count == len(t):
      s = list(s)
      s[len(s)-len(t)-i:len(s)-i] = t
      for k in range(len(s)):
        if s[k] == "?":
          s[k] = "a"
      return "".join(s)
  return "UNRESTORABLE"
print(f(s, t))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

Sd = input()
T = input()

def match(L):
    for l, t in zip(L, T):
        if l == "?":
            continue
        else:
            if l != t:
                return False
            else:
                pass
    return True

res = []

for i in range(len(Sd)-len(T)+1)[::-1]:
    if match(Sd[i:i+len(T)]):
        res.append((Sd[:i] + T + Sd[i+len(T):]).replace("?", "a"))

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

Submission status: Accepted

Solution code:
S = raw_input()
T = raw_input()
m = len(S)
n = len(T)
ans = "UNRESTORABLE"
for i in range(m-n, -1, -1):
    ok = True
    for j in range(n):
        if not ((S[i+j] == T[j]) or (S[i+j] == '?')):
            ok = False
            break
    if ok:
        s = ""
        for j in range(m):
            if i <= j < i+n:
                s += T[j-i]
            elif S[j] == '?':
                s += 'a'
            else:
                s += S[j]
        ans = s
        break
print ans

Submission status: Accepted

Solution code:
import re
s=input()
t=input()
regex = '([?'+'][?'.join(t[::-1])+'])'
m=re.split(regex,s[::-1])
if len(m)==1:
    print('UNRESTORABLE')
else:
    m[1] = t[::-1]
    print(''.join(m).replace('?','a')[::-1])

Submission status: Accepted

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

def check(s, t):
    for i in range(len(s)):
        for j in range(min(len(t), len(s[i:]))):
            if s[i:][j] != '?' and s[i:][j] != t[j]:
                break
            if j == len(t)-1:
                return True, i
    return False, 0


s = s[::-1]
t = t[::-1]
c, i = check(s, t)
ans = ''
if c:
    ans = s[:i] + t + s[(i+len(t)):]
    ans = ans.replace('?', 'a')
    print(ans[::-1])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

N = len(S_)
for i in range(N):
    if i < len(T) - 1:
        continue

    check = [S_[-i - 1 + k] == T[k] or S_[-i - 1 + k] == "?" for k in range(len(T))]
    # print(i, check)
    if all([S_[-i - 1 + k] == T[k] or S_[-i - 1 + k] == "?" for k in range(len(T))]):
        S = list(S_)
        for j, t in enumerate(T):
            S[N - i - 1 + j] = t
        for j in range(len(S)):
            if S[j] == "?":
                S[j] = "a"
        print("".join(S))
        exit()

print("UNRESTORABLE")

Submission status: Accepted

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

def check(A, B):
    for a, b in zip(A, B):
        if a != '?' and a != b:
            return False
    return True

for i in range(len(Sd) - len(T) + 1)[::-1]:
    if check(Sd[i:i + len(T)], T):
        print((Sd[:i] + T + Sd[i + len(T):]).replace('?', 'a'))
        break
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import re
 
def solve(s, t):
    s = s.replace('?', '.')
    ls, lt = len(s), len(t)
    if ls < lt:
        return 'UNRESTORABLE'
    ans = []
    
    for i in range(ls - lt + 1):
        m = re.match(s[i:i+lt], t)
        if m is None:
            continue
        ans.append( (s[:i] + t + s[i+lt:]).replace('.', 'a') )
        
    if not ans:
        return 'UNRESTORABLE'
    
    return min(ans)
 
S = input()
T = input()
 
print(solve(S, T))

Submission status: Accepted

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


def f(begin, txt, query):
    for i in range(len(query)):
        if (txt[begin+i] == '?') or (txt[begin+i] == query[i]):
            pass
        else:
            return False
    return True


def g(begin, txt, query):
    tmp = list(txt)
    for i in range(len(tmp)):
        if tmp[i] == '?':
            tmp[i] = 'a'
    for i in range(len(query)):
        tmp[begin+i] = query[i]
    return ''.join(tmp)


flag = False

for i in range(len(S) - len(T) + 1):
    if f(len(S) - i - len(T), S, T):
        S = g(len(S) - i - len(T), S, T)
        flag = True
        break

if flag is False:
    print('UNRESTORABLE')
else:
    print(S)

Submission status: Accepted

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

Slen=len(S)
Tlen=len(T)
flag=0
def check(S,T,i):
	k=0
	Slen=len(S)
	Tlen=len(T)
	for _ in range(Tlen):
		if S[_+i]==T[_] or S[_+i]=="?":
			k+=1
	if k==Tlen:
		return True
	else:
		return False
	
for j in range(Slen-Tlen,-1,-1):
	if check(S,T,j):
		S[j:j+Tlen]=T[::]
		for _ in range(Slen):
			if S[_] == "?":
				S[_]="a"
		ans="".join(S)
		flag=1
		print(ans)
	

if flag !=1:
	print("UNRESTORABLE")

Submission status: Accepted

Solution code:
lisS = list(input())
lisT = list(input())
for i in range(len(lisS)-len(lisT)+1)[::-1]:
  for num,item in list(enumerate(lisT))[::-1]:
    if item != lisS[i+num] and lisS[i+num] != "?":
      break
  else:
    for j in range(len(lisT)):
      lisS[i+j] = lisT[j]
    while "?" in lisS:
      lisS[lisS.index("?")] = "a"
    print ("".join(lisS))
    break
else:
  print ("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys
s = list(raw_input()[::-1])
t = list(raw_input()[::-1])
for i in xrange(0, len(s)-len(t)+1):
    for j in xrange(len(t)):
        if ("?" == s[i+j]) or (t[j] == s[i+j]):
            continue
        break
    else:
        for j in xrange(len(t)):
            s[i+j] = t[j]
        break
else:
    print "UNRESTORABLE"
    sys.exit()
print "".join([(lambda x: "a" if x == "?" else x)(s[i]) for i in xrange(len(s))])[::-1]

Submission status: Accepted

Solution code:
val = list(input())[::-1]
code = list(input())[::-1]

judge = False

for i in range(len(val)-len(code)+1):
    slice_val = val[i:i+len(code)]
    res = val[i+len(code):]
    res2 = val[:i]
    for j in range(0, len(code)):
        if slice_val[j] == code[j] or slice_val[j] == "?":
            slice_val[j] = code[j]
        else:
            break
    if slice_val == code:
        slice_val += res
        res2 += slice_val
        judge = True
        break
print("".join(reversed(res2)).replace("?", "a") if judge else "UNRESTORABLE")

Submission status: Accepted

Solution code:
S_d = input()
T = input()
len_S_d = len(S_d)
len_T = len(T)
ans_list = []
all_a = S_d.replace("?", "a")
for i in range(len_S_d - len_T + 1):
	if all(map(lambda x: x[0] == x[1] or x[0] == "?", zip(S_d[i:i + len_T], T))):
		ans_list.append(all_a[:i] + T + all_a[i + len_T:])
if ans_list:
	print(min(ans_list))
else:
	print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
T = input()
ok = False
ans = []

for i in range(len(S)-len(T),-1,-1):
	tmpS = list(S)
	for j in range(len(T)):
		if tmpS[i+j] == "?":
			tmpS[i+j] = T[j]

	if "".join(tmpS[i:i+len(T)]) == T:
		ans = tmpS
		ok = True
		break

ans = "".join(ans)
if ok == False:
	print("UNRESTORABLE")
else:
	print(ans.replace("?","a"))

Submission status: Accepted

Solution code:
S = input()
T = input()
flg = 0
ans = []

for i in range(len(S)-len(T), -1, -1):
    tmpS = list(S)
    for j in range(len(T)):
        if tmpS[i+j] == "?":
            tmpS[i+j] = T[j]
    
    if "".join(tmpS[i:i+len(T)]) == T:
        ans = tmpS
        flg = 1
        break

ans = "".join(ans)
if flg == 0:
    print("UNRESTORABLE")
else:
    print(ans.replace("?", "a"))

Submission status: Accepted

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

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

for i in reversed(range(len(s) - len(t)+1)):
    if eq(s[i:i+len(t)], t[:]):
        s[i:i + len(t)] = t[:]
        ans = ''.join(s)
        print(ans.replace('?', 'a'))
        quit()
print('UNRESTORABLE')

Submission status: Accepted

Solution code:

def solve():
	S = input()
	T = input()
	ans_l = []

	cnt = 0
	for start in range(len(S) - len(T)+1):
		for i,t in enumerate(T):
			if S[start+i] == t or S[start+i] == '?':
				if i == len(T)-1:
					ans = S[:start].replace('?', 'a') + T + S[start+i+1:].replace('?', 'a')
					ans_l.append(ans)
					break
			else:
				break

	return ans_l
	
ans_l = solve()
if len(ans_l) > 0:
	print(sorted(ans_l)[0])
else:
	print('UNRESTORABLE')

Submission status: Accepted

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


# 後ろから見ていって、はまったらa埋めする
def check_embed(s, t, ind):
    use_s = s[ind:ind+len(t)]
    for i in range(len(t)):
        if use_s[i] != "?" and use_s[i] != t[i]:
            return False
    return True

def check_a(s, t, ind):
    use_s = s[ind:ind+len(t)]
    for i in range(len(t)):
        if not ((use_s[i] == "?" and t[i] == "a") or use_s[i] == t[i]):
            return False
    return True

"""
flag = 0
for i in range(len(s) - len(t) + 1):
    if check_a(s, t, i):
        print(i)
        flag = 1
        s = (s[:i] + t + s[i + len(t):]).replace("?", "a")
        break
if flag:
    print(s)
else:
"""
ans = []
flag = 0
for i in range(len(s) - len(t) + 1):
    if check_embed(s, t, len(s) - len(t) - i):
        flag = 1
        ans.append((s[:len(s) - len(t) - i] + t + s[len(s) - i:]).replace("?", "a"))
if flag:
    ans.sort()
    print(ans[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
def checker(L1, L2):
    N = len(L1)
    for i in range(N):
        if L1[i] == L2[i] or L1[i] == "?":
            pass
        else:
            return False
 
    return True
 
 
def solve():
    while 1:
        try:
            S_prime = list(input())
            T = input()
            if len(T) > len(S_prime):
                print("UNRESTORABLE")
                raise EndLoop
            elif S_prime.count("?") == 0:
                if T in "".join(S_prime):
                    print("".join(S_prime))
                    raise EndLoop
                else:
                    print("UNRESTORABLE")
                    raise EndLoop
            elif S_prime.count("?") == len(S_prime):
                S = "".join(["a" for _ in range(len(S_prime) - len(T))]) + T
                print(S)
                raise EndLoop
 
            possible_ans = []
            for i in range(len(S_prime) - len(T) + 1):
                tmp_str = list(S_prime[i:len(T) + i])
                if checker(tmp_str, T):
                    if i + len(T) == len(S_prime):
                        S = "".join(S_prime[:i]) + "".join(T)
                        possible_ans.append(S.replace("?", "a"))
                    else:
                        S = "".join(S_prime[:i]) + T + "".join(S_prime[i + len(T):])
                        possible_ans.append(S.replace("?", "a"))
                        
            if len(possible_ans) == 0:
                print("UNRESTORABLE")
            else:
                print(min(possible_ans))
            break
        except:
            break
 
 
solve()

Submission status: Accepted

Solution code:
import re

S_IN = input()
T = input()
U = 'UNRESTORABLE'

def get_substring(start_index, len, str):
    """
    get substring.
    """
    return str[start_index:start_index + len]

def validate(str, cmp):
    """
    validate.
    """
    is_valid = True
    for index, c in enumerate(str):
        if c == '?':
            continue
        elif c == cmp[index]:
            continue
        else:
            is_valid = False
            break
    return is_valid

def make_string(start_index, replacement, str):
    """
    make string.
    """
    length = len(replacement)
    before = str[:start_index].replace('?', 'a')
    repl = replacement
    after = str[start_index + length:].replace('?', 'a')
    return before + repl + after

def main():
    """
    main function
    """
    s_length = len(S_IN)
    t_length = len(T)
    limit = s_length - t_length + 1

    if limit < 0:
        print(U)

    list = []
    # print("limit - {0}".format(limit))
    for i in range(limit):
        substr = get_substring(i, t_length, S_IN)
        # print(substr)
        if validate(substr, T):
            list.append(make_string(i, T, S_IN))

    list.sort()
    ans = list[0] if bool(list) else U

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

indices = []
for i in range(len(sPrime) - len(t) + 1):
	if sPrime[i] != t[0] and sPrime[i] != "?":
		continue
	possible = True
	for j in range(i+1, i+len(t)):
		diff = j - i
		tLetter = t[diff]
		if sPrime[j] != "?" and sPrime[j] != tLetter:
			possible = False
			break
	if possible:
		indices.append(i)
possibilities = []
for i in indices:
	new = ""
	for l in range(len(sPrime)):
		letter = sPrime[l]
		if l < i or l >= i + len(t):
			new = new + letter
		else:
			new = new + t[l - i]
	possibilities.append(new)

for i in range(len(possibilities)):
	possibilities[i] = possibilities[i].replace("?", "a")
possibilities.sort()
if len(possibilities) == 0:
	print("UNRESTORABLE")
else:
	print(possibilities[0])

Submission status: Accepted

Solution code:
def matches(pattern, text):
    for i in range(len(text)-len(pattern), -1, -1):
        good = True
        for j in range(len(pattern)):
            if not text[i+j] in ['?', pattern[j]]:
                good = False
        if good:
            return i
    return -1

Sprime = input()
T = input()

match = matches(T, Sprime)
if match == -1:
    print('UNRESTORABLE')
else:
    print((Sprime[:match]+T+Sprime[match+len(T):]).replace('?','a'))

Submission status: Accepted

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

S_r = Sd[::-1]
T_r = T[::-1]

lenS = len(Sd)
lenT = len(T)

Flag2 = True

if lenS < lenT:
    pass
else:
    for i in range(lenS - lenT + 1):
        for j in range(lenT):
            Flag1 = True
            if S_r[i+j] == "?" or S_r[i+j] == T_r[j]:
                pass
            else:
                Flag1 =False
                break
        if Flag1:
            Flag2 = False
            S_r = S_r.replace(S_r[i:i+lenT],T_r,1)
            S_r = S_r.replace("?","a")
            ans = S_r[::-1]
            break

if Flag2:
    ans = "UNRESTORABLE"
print(ans)

Submission status: Accepted

Solution code:
import sys

S = input()
T = input()
len_S = len(S)
len_T = len(T)

offset = len_S - len_T
while offset >= 0:
    found = True
    for i in range(offset, offset + len_T):
        if S[i] != T[i - offset] and S[i] != '?':
            found = False
            break
    if found:
        S = S[:offset] + T + S[offset + len_T:]
        print(S.replace('?', 'a'))
        sys.exit()
    offset -= 1
print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys
s=input()
T=input()
for i in range(len(s)-len(T)+1,0,-1):
  for j in range(len(T)):
    if T[j]!=s[i-1+j] and "?"!= s[i-1+j] :
      break
  else:
    s=s[:i-1]+T+s[i+len(T)-1:]
    s = s.replace('?', 'a')
    print(s)
    sys.exit()
else: print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S=str(input())
T=str(input())
SL=len(S)
TL=len(T)
a=[]
 
for i in range(SL-TL+1):
 
    count=0
 
    for j in range(TL):
 
        if S[i+j]=='?' or T[j]=='?' or S[i+j]==T[j]:
            count += 1
 
    if count==TL:
        ans = S[:i].replace('?','a')+T+S[i+TL:].replace('?','a')
        a.append(ans)
 
if len(a)==0:
    print('UNRESTORABLE')
else:
    a.sort()
    print(a[0])

Submission status: Accepted

Solution code:
a = list(raw_input())
b = list(raw_input())
sign = False
backup = list(a)
ans = '~' * 100
for a_bi in xrange(len(a) - len(b) + 1):
	#TRY TO RESTORE B AT EVERY BYTE OF A
	#a_bi = byte's index in a
	ok = True
	for b_bi in xrange(len(b)):
		if a[a_bi + b_bi] != '?' and a[a_bi + b_bi] != b[b_bi]:
			ok = False
	if ok:
		#MEANING THAT THIS IS RESTORABLE
		a[a_bi:a_bi + len(b)] = b
		a = ''.join(a).replace('?', 'a')
		ans = min(a, ans)
		a = list(backup)
if ans == '~' * 100:
	print 'UNRESTORABLE'
else:
	print ans

Submission status: Accepted

Solution code:
a=raw_input()
b=raw_input()
ans='z'*51
for i in xrange(len(a) - len(b) + 1):
 w = False
 for j in xrange(len(b)):
  if a[j+i] != '?' and a[j+i] != b[j]:
   w = True
   break
 if w:
  continue
 res = ''
 for j in xrange(len(a)):
  if j >= i and j < i + len(b):
   res += b[j - i]
  elif a[j] == '?':
   res += 'a'
  else:
   res += a[j]
 ans = min(ans, res)
if len(ans) > 50:
 ans = 'UNRESTORABLE'
print ans

Submission status: Accepted

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
        ans0=s[0:n-i-1]+t+s[n-i-1+len(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: Accepted

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

def f(S, T):
    lastIndex = None
    pat = None

    for i in range(len(S)-len(T)+1):
        pat2 = g(S, T, i)

        if pat2 != None and (pat == None or pat2 < pat):
            pat = pat2

    if pat == None:
        return 'UNRESTORABLE'

    S2 = list(S)
    for index, letter in pat:
        S2[-index] = letter

    return ''.join(S2).replace('?', 'a')
            


def g(S, T, st):
    pat = []

    for i in range(len(T)):
        s = S[st + i]
        t = T[i]

        if s == '?':
            if t != 'a':
                pat.append((-(st + i), t))
        elif s != t:
            return None

    return pat
        

def test():
    assert f('?tc????', 'coder') == 'atcoder'
    assert f('??p??d??', 'abc') == 'UNRESTORABLE'


def main():
    S = input()
    T = input()
    print(f(S, T))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    S = list(input())
    T = list(input())

    num = -1
    for i in range(len(S) - len(T) + 1):
        flg = True
        for j in range(1, len(T) + 1):
            if S[-(i + j)] == "?":
                continue
            if S[-(i + j)] == T[-j]:
                continue
            flg = False
            break

        if flg:
            num = i
            break

    if num == -1:
        print("UNRESTORABLE")
    else:
        start = len(S) - (len(T) + num)
        end = len(S) - num
        S[start:end] = T
        mojiretu = "".join(S)
        ans = mojiretu.replace("?", "a")
        print(ans)

Submission status: Accepted

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

def is_restorable(a,b):
    ans = True
    for x,y in zip(a,b):
        if ans:
            if x == y or x == '?':
                pass
            else:
                ans = False
    return ans

s = s[::-1]
t = t[::-1]
s_ = list(s)

can_restore = False

for i,x in enumerate(s):
    if i+len(t) <= len(s):
        if not can_restore:
            if is_restorable(s[i:i+len(t)],t):
                can_restore = True
                s_[i:i+len(t)] = t

s_ = s_[::-1]
s_ = ''.join(s_)

ans = s_.replace('?','a')

if can_restore:
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
sd, t = input(), input()
# for i in range(len(t)-len(sd)+1)において,
# :sd[i]==t[i] or sd=='?' ならばそのsdの箇所を実際にtで書き換え、かつ残りの?をaに書き換えたものを結果として残す。結果は昇順なので比較しながら一つだけ残す
# :それ以外ならばとくになにもしない
# 結果が空白ならばUNRESTORABLEとする
# オーダーがよくわからんけど余裕だろう
ans = ''
for i in  range(len(sd) - len(t)+1):
    target = sd[i:i+len(t)]
    count = 0
    for j in range(len(t)):
        if t[j]==target[j] or target[j]=='?':
            count+=1
        else:
            break
    if count==len(t):
        # 候補として追加。残りの?をaに変える
        tmp = sd[:i]+t+sd[len(t)+i:]
        if ans=='':
            ans = ''.join(['a' if i=='?' else i for i in tmp])
        else:
            ans = min(ans, ''.join(['a' if i=='?' else i for i in tmp]))
print('UNRESTORABLE' if ans=='' else ans)

Submission status: Accepted

Solution code:
S2 = list(input())
T = list(input())
S = []
Temp = 0

for _ in range(len(S2)-len(T)+1):
    for j in range(len(T)):
        if T[j] == S2[Temp + j] or S2[Temp + j] == "?":
            if j == len(T) - 1:
                S.append([S2[x] for x in range(Temp)] + [T[x] for x in range(len(T))] + [S2[x] for x in range(Temp + len(T), len(S2))])
                Temp += 1
                break
            else:
                continue
        else:
            Temp += 1
            break

#print(S2)
#print(S)
for i in range(len(S)):
    for j in range(len(S2)):
        if S[i][j] == '?':
            S[i][j] = 'a'
    S[i] = ''.join(S[i])
if S != []:
    print(sorted(S)[0])
else: print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S_prime = input()
T = input()
ans = 'z'
matched = False

if len(S_prime) < len(T):
    print('UNRESTORABLE')
    exit()

for i in range(len(S_prime) - len(T) + 1):
    for k in range(len(T)):
        if S_prime[i + k] != T[k] and S_prime[i + k] != '?':
            break

    if k == len(T) - 1 and (S_prime[i + k] == T[k] or S_prime[i + k] == '?'):
        tmp = (S_prime[0:i] + T + S_prime[i + len(T):]).replace('?', 'a')
        if ans > tmp:
            ans = tmp
        matched = True

if matched:
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import re


def solve(s, t):
    sd = s.replace('?', '.')
    sa = s.replace('?', 'a')
    ls, lt = len(s), len(t)
    if ls < lt:
        return 'UNRESTORABLE'
    ans = []
    for i in range(ls - lt + 1):
        if re.match(sd[i:i + lt], t) is not None:
            ans.append(sa[:i] + t + sa[i + lt:])
    if not ans:
        return 'UNRESTORABLE'
    return min(ans)


print(solve(input().strip(), input().strip()))

Submission status: Accepted

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

def check(s1,s2):
    for a,b in zip(s1,s2):
        if not (a==b or a=='?'):
            return False
    else:
        return True
find=[]
for i in range(len(s)-len(part)+1):
    if check(s[i:i+len(part)], part):
        find.append(i)
if len(find)!=0:
    s[find[-1]:find[-1]+len(part)]=part
    print(''.join([c if c!='?' else 'a'for c in s]))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

S = input()
T = input()

pos = -1
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:
        pos = max(pos, i)

if pos == -1:
    print('UNRESTORABLE')
else:
    ans = list(S)
    ans[pos:pos + len(T)] = list(T)
    for i in range(len(ans)):
        if ans[i] == '?':
            ans[i] = 'a'
    print(''.join(ans))

Submission status: Accepted

Solution code:
S=str(input())
T=str(input())
SL=len(S)
TL=len(T)
a=[]

for i in range(SL-TL+1):

    count=0

    for j in range(TL):

        if S[i+j]=='?' or T[j]=='?' or S[i+j]==T[j]:
            count += 1

    if count==TL:
        ans = S[:i].replace('?','a')+T+S[i+TL:].replace('?','a')
        a.append(ans)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""076-c"""


import sys

def kmp(T, P):
    n, m = len(T), len(P)
    j = 0
    k = 0
    fail = kmp_fail(P)
    while j < n:
        if T[j] == P[k] or T[j] == '?':
            if k == m - 1:
                return j - m + 1
            j += 1
            k += 1
        elif k > 0:
            k = fail[k-1]
        else:
            j += 1
    return -1

def kmp_fail(P):
    n = len(P)
    fail = [0] * n
    j = 1
    k = 0
    while j < n:
        if P[j] == P[k]:
            fail[j] = k + 1
            j += 1
            k += 1
        elif k > 0:
            k = fail[k - 1]
        else:
            j += 1
    return fail


def main():
    """Main function."""
    S_p = input()
    T = input()

    pos = kmp(S_p, T)

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

    S_cand = S_p[:pos] + T + S_p[pos+len(T):]
    S_cand = S_cand.replace('?', 'a')

    pos_ = 0
    while pos_ >= 0:
        pos_ = kmp(S_p[pos+1:], T)
        if pos_ >= 0:
            pos = pos + pos_ + 1
            S_cand2 = S_p[:pos] + T + S_p[pos+len(T):]
            S_cand2 = S_cand2.replace('?', 'a')
            S_cand = S_cand if S_cand < S_cand2 else S_cand2

    print(S_cand)

if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

Solution code:
s = input()
t = input()
ret = "z"*51
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:
        ls = list(s)
        ls[i:i+len(t)] = list(t)
        for i in range(len(s)):
            if ls[i] == "?" : ls[i] = "a"
        ret = min(ret,"".join(ls))

print(ret if ret != "z"*51 else "UNRESTORABLE")

Submission status: Accepted

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

pos = -1
for i in range(len(S)-len(T)+1):
	#check it out
	contain = True
	for j in range(len(T)):
		if(S[i+j] != T[j] and S[i+j] != '?'):
			contain = False
			break
	if contain :
		pos = i
		
if pos == -1: print("UNRESTORABLE")
else:	
	ans = S[0:pos] + T + S[pos+len(T):]
	print(ans.replace('?','a'))

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
INF = float("inf")


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

    ans_list = []

    for i in range(len(S) - len(T) + 1):
        ok = True
        s = list(S[:])
        for j in range(len(T)):
            if S[i + j] == "?" or S[i + j] == T[j]:
                s[i + j] = T[j]
            else:
                ok = False
        if ok:
            ans_list.append("".join(s).replace("?", "a"))

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


if __name__ == '__main__':
    main()