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()
 
check2 = False
ans = []
for i in range(len(s)-len(t)+1):
    check = s[i: i+len(t)]
    flag = True
    for j in range(len(t)):
        if check[j] != '?' and check[j] != t[j]:
            flag = False
    if flag:
        a = s[0:i] + t + s[i+len(t):len(s)]
        a = a.replace('?','a')
        ans.append(a)
 
if ans == []:
    print('UNRESTORABLE')
else:
    print(sorted(ans)[0])

Submission status: Accepted

Solution code:
# 2019/07/02
# かんにんぐあり

s=input()
t=input()

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

ans=[]

for i in range(len_s-len_t+1):
    flag=True
   # if s[i]==t[0]:
    for j in range(len_t):
        if s[i+j]==t[j] or s[i+j]=='?':continue
        else:
            flag=False
            break
    if flag:
        ans.append((s[:i]+t[:]+s[len_t+i:]).replace('?','a'))

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

Submission status: Accepted

Solution code:
s=list(input())
t=list(input())
s.reverse()
t.reverse()
c=0
if len(s)<len(t):
    print("UNRESTORABLE")
    exit()
for i in range(len(s)-len(t)+1):
    if c!=0:
        break
    ss=s[i:i+len(t)]
    f=True
    for j in range(len(t)):
        if ss[j]!=t[j] and ss[j]!="?":
            f=False
    if f:
        s[i:i+len(t)]=t
        c+=1
for i in range(len(s)):
    if s[i]=="?":
        s[i]="a"
if c:
    s.reverse()
    print("".join(s))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s = list(input())
len_s = len(s)
t = list(input())
len_t = len(t)

if len_s<len_t:
    print("UNRESTORABLE")
    exit()

def fix(ls):
    for i in range(len(ls)):
        if ls[i]=="?":
            ls[i]="a"
    return ls

ans = []
for i in range(len_s-len_t+1):
    flg = True
    for j in range(len_t):
        if t[j]==s[i+j] or s[i+j]=="?":
            pass
        else:
            flg=False
            break
    if flg:
        can1 = fix(s[0:i])
        can2 = t
        can3 = fix(s[i+len_t:])
        ans.append("".join(can1+can2+can3))
ans.sort()
if len(ans)!=0:
    print(ans[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

kainashi="UNRESTORABLE"
ans1=[]
for i in range(len(s)-len(t)+1):
    tmp=s[:]
    flag=1
    for j in range(len(t)):
        if s[len(s)-i-len(t)+j]==t[j] or s[len(s)-i-len(t)+j]=="?":
            tmp[len(s)-i-len(t)+j]=t[j]
        else:
            flag=0
        
    if flag==1:
        ans=""
        for k in range(len(s)):
            if tmp[k]=="?":
                tmp[k]="a"
            ans+=tmp[k]
        ans1.append(ans)

if len(ans1)>0:
    ans1.sort()
    print(ans1[0])
else:
    print(kainashi)

Submission status: Accepted

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

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

ans_list = []

for i in range(len(s)-len(t)+1)[::-1]:
    flag=True
    for j in range(len(t)):
        if s[i+j]=="?" or s[i+j]==t[j]:
            continue
        else:
            flag=False
            break
    if flag:
        ans = "".join(s[:i]+t+s[i+len(t):]).replace("?","a")
        ans_list.append(ans)
    else:
        continue
if len(ans_list)==0:
    print('UNRESTORABLE')
else:
    print(min(ans_list))

Submission status: Accepted

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


def match(k):
    s = S[k:k+len(T)]
    
    for j in range(len(s)):
        if s[j] not in (T[j], '?'):
            return False

    return True

def construct(k):
    ans = list(S)

    for i in range(len(T)):
        ans[k+i] = T[i]
    
    for i in range(len(S)):
        if (ans[i] == '?'):
            ans[i] = 'a'

    return ''.join(ans)

res = []
for i in range(len(S) - len(T) + 1):
    if match(i):
        s = construct(i)
        res.append(s)

if res:
    print(min(res))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys

S_ = list(sys.stdin.readline()[:-1])
T = list(sys.stdin.readline()[:-1])

start_right = -1
for i in range(len(S_)):
    if (S_[i] == "?") or (S_[i] == T[0]):
        for j in range(1, len(T)):
            if (i+j >= len(S_)):
                break
            if (S_[i+j] == "?") or (S_[i+j] == T[j]):
                continue
            else:
                break
        else:
            start_right = i
    
for i in range(len(T)):
    S_[start_right + i] = T[i]

if (start_right != -1):
    for s in S_:
        print(s if (s != "?") else "a", end="")
    print()
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
n=input()
m=input()
le=len(m)
check=0
for i in range(len(n)-le,-1,-1):
    count=0
    for j in range(le):
        if m[j]==n[i+j]:
            count+=1
    if le-count==n[i:i+le].count('?'):
        n=n[0:i]+m+n[i+le:len(n)]
        check=1
        n=n.replace('?','a')
if check==1:
    print(n)
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

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

res = []
for i in range(len(S)-len(T)+1):
    if match(S[i:i+len(T)]):
        res.append((S[:i] + T + S[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()

def last_match(s, t):
    l, r = -1, -1
    for i in range(len(s)):
        if s[i] == "?" or s[i] == t[0]:
            match = True
            #print i, s[i]
            for j in range(len(t)):
                if i + j >= len(s) or (s[i+j] != '?' and s[i+j] != t[j]):
                    match = False
                    break
                #print j, s[i+j], t[j]
            if match:
                l, r = i, i + j
    return l, r
l, r = last_match(s, t)
if l == -1 and r == -1:
    print "UNRESTORABLE"
    exit()
res = ""
for i in range(len(s)):
    if i == l:
        match = True
        j = i
        res += t[0]
    elif i >= l and i <= r:
        res += t[i-j]
    elif s[i] == "?":
        res += "a"
    else:
        res += s[i]
print res

Submission status: Accepted

Solution code:
S = list(input())
T = input()
ans = "UNRESTORABLE"
for i in range(len(S)-len(T)+1):
    key = S[:]
    for n,t in enumerate(T):
        if key[i+n] not in ("?",t):break
        else:
            key[i+n] = t
        if n == len(T)-1:
            ans = "".join(key).replace("?","a")
print(ans)

Submission status: Accepted

Solution code:
S = input()
T = input()
L = []
slide_num = len(S) - len(T)
if slide_num < 0:
    print('UNRESTORABLE')
    exit()
for slide in range(slide_num+1):
    for j in range(len(T)):
        if S[j+slide] == T[j] or S[j+slide] == '?':
            None
        else:
            break
    else:
        L.append((S[:slide] + T + S[slide+len(T):]).replace('?', 'a'))

if L:
    print(L.pop())
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappop, heappush, heapify, heappushpop
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S_ = list(input())
T = input()

for i in range(len(S_)-1, -1, -1):
	search = "".join(S_[i:i+len(T)])
	# print(search)
	if len(search) == len(T):
		for j in range(len(search)):
			if not search[j] == "?" and search[j] != T[j]:
				break
		else:  # 条件を満たす.
			S_[i:i+len(T)] = list(T)
			break
else:
	print("UNRESTORABLE")
	sys.exit(0)

for i in range(len(S_)):
	if S_[i] == "?":
		S_[i] = "a"

print("".join(S_))

Submission status: Accepted

Solution code:
# ABC076
def main():
    s, t = [input().rstrip() for _ in range(2)]
    len_s, len_t = len(s), len(t)
    ans = []  # list of candidates of the answer

    for i in range(len_s - len_t + 1):
        p = s[i : i + len_t]  # partial string (substring)

        if all(j in (i, "?") for i, j in zip(t, p)):
            # a candidate of the answer
            cand = (s[:i] + t + s[i + len_t :]).replace("?", "a")
            ans += [cand]

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def output(S,T,i,j):
  op = ""
  for k in S[:j-i]:
    if k == "?":
      op += "a"
    else:
      op += k
  op += T
  if j-i+len(T) < len(S):
    for k in S[j-i+len(T):]:
      if k == "?":
        op += "a"
      else:
        op += k
  return op


S = input()
T = input()
SS = len(S)
TT = len(T)
cand = []
if TT > SS:
  print("UNRESTORABLE")
else:
  for i,t in enumerate(T):
    for j,s in enumerate(S):
      c = 0
      if ((t == s) or (s == "?")) and (i <= j) and (TT - i <= SS - j):
        for ss, tt in zip(S[j-i:], T):
          if ss != tt and ss != "?":
            break
          else:
            c += 1
        if c == TT:
          cand.append(output(S,T,i,j))
  cand.sort()
  cand.append("UNRESTORABLE")
  print(cand[0])

Submission status: Accepted

Solution code:
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

S = s()
T = s()

cands = set()
for i in range(len(S) - len(T) + 1):
    match = True
    for j in range(len(T)):
        if S[i+j] == T[j] or S[i+j] == '?':
            continue
        match = False
        break
    if match:
        cand = S[:i] + T + S[i+len(T):]
        scand = ''
        for c in cand:
            if c == '?':
                scand += 'a'
            else:
                scand += c
        cands.add(scand)

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

Submission status: Accepted

Solution code:
s = str(input())
t = str(input())
ss = []
for i in range(len(s)-len(t)+1):
    for j in range(len(t)):
        if s[i+j] not in ("?",t[j]):
            break
    else:
        u = s[:i]+t+s[i+len(t):]
        ss.append(u.replace("?","a"))
if ss:
    print(min(ss))
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 22 08:35:15 2019

@author: Owner
"""

from collections import deque

Sd = input()
T = input()
S = deque()

Sr = list(reversed(Sd))
Tr = list(reversed(T))

flg_match = True    
for i,s in enumerate(Sr):
    flg_match = True    
    for j,t in enumerate(Tr):
        if i+j >= len(Sr):
            flg_match = False
            break
        if t == Sr[i+j] or Sr[i+j] == "?":
            pass
        else:
            flg_match = False
            break
        #print(t, Sr[i+j], flg_match)
    if flg_match:
        S.appendleft(T)
        #print(Sd)
        break
    else:
        if s == "?":
            S.appendleft("a")
        else:
            S.appendleft(s)
#print(flg_match)
if not flg_match:
    print("UNRESTORABLE")
else:    
    S = "".join(list(S))
    S = Sd[:len(Sd)-len(S)]+S
    S = S.replace("?", "a")
    
    print(S)

Submission status: Accepted

Solution code:
S=input()
T=input()
SS,TT,possible=len(S),len(T),[]

for i in range(SS-TT+1):
    key,memo=S,0
    for j in range(TT):
        if key[i+j]==T[j]:pass
        elif key[i+j]=="?":key=key[:i+j]+T[j]+key[i+j+1:]
        else:
            memo=1
            break
    if memo==0:
        key=key.replace("?","a")
        possible.append(key)

possible.sort()
possible=possible+["UNRESTORABLE"]
print(possible[0])

Submission status: Accepted

Solution code:
s = input()
t = input()
ans = set()
for i in range(len(s)-len(t)+1):
  cur = s[:i].replace("?", "a")
  for j in range(len(t)):
    if s[i+j]=="?" or s[i+j]==t[j]:
      cur += t[j]
    else:
      break
  cur += s[i+j+1:].replace("?", "a")
  if len(cur)==len(s):
    ans.add(cur)

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

Submission status: Accepted

Solution code:
s=input()
t=input()
l=len(t)
a=[]
for i in range(l-1,len(s)):
    x=s[len(s)-1-i:len(s)-1-i+l]
    cnt=0
    for j in range(l):
        if x[j]=="?" or x[j]==t[j]:
            cnt+=1
        else:
            break
    if cnt==l:
        u=s[:len(s)-1-i].replace("?","a")
        v=s[len(s)-1-i+l:].replace("?","a")
        key=u+t+v
        a.append(key)
if len(a)==0:
    print("UNRESTORABLE")
    exit()
a=sorted(a)
print(a[0])

Submission status: Accepted

Solution code:
S = input()
SA = S.replace('?', 'a')
T = input()

def test(i):
  # i文字目から初めてハマるかどうか確認
  S_sub = S[i:i+len(T)]
  for s,t in zip(S_sub,T):
    if s == '?':
      continue
    if s != t:
      return False
  return True

pw = []
for i in range(len(S)-len(T)+1):
  if test(i):
    x = SA[:i] + T + SA[i+len(T):]
    pw.append(x)

if pw:
  answer = sorted(pw)[0]
else:
  answer = 'UNRESTORABLE'

print(answer)

Submission status: Accepted

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

def checkstr(ss):
  for (s, t) in zip(ss, T):
    check = True
    if s is '?':
      continue
    if s is not t:
      check = False
      break
  return check

tmp=''
ans='z'*50

for i in range(len(S)-len(T)+1):
  str = S[i:len(T)+i] 
  if checkstr(str):
    tmp = S[:i].replace("?","a")+T+S[i+len(T):].replace("?","a")
    ans = min(tmp, ans)
if tmp is '':
  ans = "UNRESTORABLE"
print(ans)

Submission status: Accepted

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

s_n = len(s)
t_n = len(t)

# 辞書順最小にするために末尾の部分文字列からチェック
for i in range(s_n - t_n, -1,-1):
    # 部分文字列
    match = s[i: i + t_n]
    
    isMatch=True
    for m_in,m_c in enumerate(match):
        if m_c == "?":
            continue
        
        if m_c != t[m_in]:
            isMatch = False
            break
    
    # 一致していなければ次を検索
    if not isMatch:
        continue

    # 一致している場合は、部分文字列を置き換える
    password=""
    if i > 0:
        password = s[:i]
    password += t
    if s_n > i + t_n:
        password+=s[i+t_n:]

    password = password.replace('?', 'a')  # 辞書順最小にするために、残りの?をaに置き換えておく
    print(password)
    exit()
    
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc076/tasks/abc076_c
def solve(s, t):
    sl = len(s)
    tl = len(t)
    for i in range(sl-tl, -1, -1):
        for j, k in zip(range(i, i+tl), range(tl)):
            if s[j] == t[k] or s[j] == '?':
                if k == tl-1:
                    return (s[:i]+t+s[i+tl:]).replace('?', 'a')
            else:
                break

    return 'UNRESTORABLE'


if __name__ == '__main__':
    s = input()
    t = input()
    print(solve(s, t))

Submission status: Accepted

Solution code:
from copy import deepcopy
s = input()
t = input()
l_t = len(t)

def check(x, y):
    for i, j in zip(x, y):
        if i == j or i == '?':
            continue
        else:
            return False
    return True


ans = 'z' * (len(s) + 1)
for k in range(len(s) - l_t + 1):
    x = s[k:k+l_t]
    if check(x, t):
        cand = list(deepcopy(s))
        cand[k:k+l_t] = list(t)
        cand = ''.join(cand)
        cand = cand.replace('?', 'a')
        if cand < ans:
            ans = cand

if not ans == 'z' * (len(s) + 1):
    print(ans)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:

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

  if len(t) > len(s):
    print('UNRESTORABLE')
    return

  for i in range(len(s)-len(t), -1, -1):
    flag = True

    for j in range(len(t)):
      if s[i+j] != '?' and s[i+j] != t[j]:
        flag = False
        break

    if flag == True:
      str = s[0:i].replace('?', 'a') + t + s[i+len(t):].replace('?', 'a')
      print(str)
      return

  print('UNRESTORABLE')

main()

Submission status: Accepted

Solution code:
S = list(input())
T = list(input())
if len(S)<len(T):
    print("UNRESTORABLE")
else:
    S = S[::-1]
    T = T[::-1]
    k = 0
    l = 0
    for i in range(len(S)-len(T)+1):
        if S[i]=="?" or S[i]==T[0]:
            k = 0
            for j in range(len(T)):
                if S[i+j]=="?" or S[i+j]==T[j]:
                    k+=1
        if k==len(T):
            l = i
            break
    if k!=len(T):
        print("UNRESTORABLE")
    else:
        ans = []
        for i in range(len(S)):
            if l <= i and i < l+k:
                ans.append(T[i-l])
            elif S[i]=="?":
                ans.append("a")
            else:
                ans.append(S[i])
        print("".join(ans[::-1]))

Submission status: Accepted

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

Sdr = Sd[::-1].replace('?', '.')
Tr = T[::-1]

for i in range(len(Sdr)-len(Tr)+1):
    pattern = Sdr[i:i+len(Tr)]
    if re.match(pattern, Tr):
        print(Sdr.replace(pattern, Tr, 1).replace('.', 'a')[::-1])
        break
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(len(S_dash)-len(T),-1,-1):
    for j in range(len(T)):
        if S_dash[i+j] != '?' and S_dash[i+j] != T[j]:
            break
    else:
        S = [s for s in S_dash]
        for j in range(len(T)):
            S[i+j] = T[j]
        for i in range(len(S)):
            if S[i] == "?":
                S[i] = "a"
        print("".join(S))
        break
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
#coding utf-8
from collections import deque
S = input()
T = input()

s = len(S)
t = len(T)

word =[]

for i in range(s):
  for j in range(t):
    if S[i+j:i+j+1] =="?" or S[i+j:i+j+1]==T[j:j+1]:
      if j==t-1:
        tmp = S[0:i]+T+S[i+j+1:]
        for k in range(s):
          if tmp[k:k+1] =="?":
            tmp =tmp[0:k]+"a"+tmp[k+1:]
        word.append(tmp)
    else:
      break

word.sort()

if not word:
  print("UNRESTORABLE")
else:
  print(word[0])

Submission status: Accepted

Solution code:
import re

s = input().strip()
t = input().strip()

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

if ls < lt:
    print('UNRESTORABLE')
else:
    s = s.replace('?', '.')
    ans = []
    for i in range(len(s) - len(t) + 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:
        print('UNRESTORABLE')
    else:
        print(min(ans))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# input
s = input()
t = input()
ls = len(s)
lt = len(t)
ans = 0
# calc
for i in range(ls, lt-1, -1):
    temp = s[i-lt:i]
    # print(temp)
    counter = 0
    for j in range(lt-1, -1, -1):
        if t[j] == temp[j] or temp[j] == '?':
            counter += 1
            # print(j)
            # print(temp[j])
    if counter == lt:
        ans = i
        break
# output
if ans == 0:
    print('UNRESTORABLE')
else:
    print((s[:ans-lt]+t+s[ans:]).replace('?', 'a'))

Submission status: Accepted

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

index = len(S)
ans = []
isMatch = True

# 後ろから判定
while index - len(T) >= 0:
  obj = S[index - len(T):index]
  # 切り出した文字列がTと一致し得るか判定
  for i in range(len(obj)):
    if not obj[i] == "?" and not obj[i] == T[i]:
      isMatch = False
  
  # T文字列で埋める
  if isMatch:
    ans.append(S[0:index-len(T)] + T + S[index:])

  isMatch = True
  index = index - 1

if ans == []:
  print("UNRESTORABLE")
else:
  ans = sorted(ans)[0]
  for i in range(len(ans)):
    if ans[i] == "?":
      ans[i] = "a"
  print("".join(ans))

Submission status: Accepted

Solution code:
l = input().split()
S = l[0]
l = input().split()
T = l[0]

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

# print(S, T, len(S), len(T))

found = False

def match(pos, max_pos, src, dst):
    if pos == max_pos:
        return True
    if src[pos] == '?' or src[pos] == dst[pos]:
        return match(pos + 1, max_pos, src, dst)
    return False

for i in range(ls - lt + 1):
    index = ls - lt - i
    if match(0, lt, S[index:], T):
        S = S[:index] + T + S[index + lt:]
        found = True
        break

if found:
    S = S.replace('?', 'a')
    print(S)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
def main():
    S = list(input())
    T = list(input())
    S.reverse()
    T.reverse()
    SL = list(S)
    f = False
    for i in range(len(S) - len(T) + 1):
        SL = list(S)
        for j in range(len(T)):
            if S[i + j] == '?' or S[i + j] == T[j]:
                SL[i + j] = T[j]
            else:
                break
        else:
            f = True
            break
    if f is False:
        print('UNRESTORABLE')
    else:
        SL.reverse()
        print(''.join(SL).replace('?', 'a'))


main()

Submission status: Accepted

Solution code:
# けつから順に当てはまるものを探していく
s = list(input())
h = list(input())

index = len(s)
omega = []
notFound = False
while index - len(h) >= 0 and not notFound:
    obj = s[index - len(h):index]
    #この文字列がhと生合成を持っているかを確認する
    for i in range(len(obj)):
        if not obj[i] == "?":
            if not obj[i] == h[i]:
                notFound = True

    if not notFound:
        omega.append(s[0:index-len(h)] + h + s[index:])

    notFound = False
    index -= 1

    

if omega == []:
    print('UNRESTORABLE')
else:
    ans = sorted(omega)[0]
    for i in range(len(ans)):
        if ans[i] == "?":
            ans[i] = "a"
    print("".join(ans))

Submission status: Accepted

Solution code:
S = input()
T = input()
def checkstr(ss):
    for (s,t) in zip(ss,T):
        check = True
        if s=="?":
            continue
        elif s!=t:
            check = False
            break
    return check
ans = ""
for i in range(len(S)-len(T)+1):
    strs = S[i:i+len(T)]
    if checkstr(strs):
        ans = S[:i].replace("?","a")+T+S[i+len(T):].replace("?","a")
if ans=="":
    print("UNRESTORABLE")
else:
    print(ans)

Submission status: Accepted

Solution code:
S = input()
T = input()
def checkstr(ss):
    for (s,t) in zip(ss,T):
        check = True
        if s=="?":
            continue
        elif s!=t:
            check = False
            break
    return check
ans = ""
for i in range(len(S)-len(T)+1):
    strs = S[i:i+len(T)]
    if checkstr(strs):
        ans = S[:i].replace("?","a")+T+S[i+len(T):].replace("?","a")
if ans=="":
    print("UNRESTORABLE")
else:
    print(ans)

Submission status: Accepted

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

SS.reverse()
T.reverse()

flag=0
for i in range(len(SS)-len(T)+1):
    if flag==0:
        for j in range(len(T)):
            if SS[i+j]==T[j] or SS[i+j]=='?':
                if j==len(T)-1:
                    flag=1
                    for k in range(len(T)):
                        SS[i+k]=T[k]
                    break
            else:
                break
            #if flag==1:
            #    break

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

Submission status: Accepted

Solution code:
flag = False

s = input()
t = input()
A = []
finish = False
for i in reversed(range(0,len(s)-len(t)+1)):
    flag = True
    for j in range(len(t)):
        if s[i+j] != t[j] and s[i+j] != '?':
            flag = False
    if flag:
        finish = True
        ans = []
        for k in range(len(s)):
            if i <= k <=i + len(t) -1:
                ans.append(t[k-i])
            else:
                if s[k] == '?':
                    ans.append('a')
                else:
                    ans.append(s[k])
        A.append(ans)
if not finish:
    print('UNRESTORABLE')
else:
    A.sort()
    print(''.join(A[0]))

Submission status: Accepted

Solution code:
from re import*
s,t=open(0)
t=t[:-1]
u=''.join('(%s|\?)'%t for t in t)
a=[]
j=len(t)
for i in range(len(s)-j):
  if match(u,s[i:i+j]):
    a.append(sub('\?','a',s[:i])+t+sub('\?','a',s[i+j:]))
print(min(a or['UNRESTORABLE']))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:


def main():
    moji = list(input())
    hint = list(input())


    for i in range(len(moji) - len(hint) + 1)[::-1]:
        flg = 1
        for j in range(len(hint)):
            if moji[i + j] == '?':
                continue
            if hint[j] != moji[i + j]:
                flg = 0
                break
        if flg:
            for j in range(len(hint)):
                moji[i + j] = hint[j]
            for j in range(len(moji)):
                if moji[j] == '?':
                    moji[j] = 'a'
            print(''.join(moji))
            break
    else:
        print('UNRESTORABLE')



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque
import pdb

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    s = S()
    t = S()
    cands = []
    ans = 'z' * 50
    for st_ix in range(len(s)):
        cur_j = 0
        copy_s_list = list(s)
        is_cand = False 
        while cur_j < len(t) and st_ix + len(t) - 1< len(s):
            if s[st_ix + cur_j] == t[cur_j] or s[st_ix + cur_j] == '?':
                copy_s_list[st_ix + cur_j] = t[cur_j]
                cur_j += 1
                if cur_j == len(t):
                    is_cand = True
            else:
                break

        if is_cand: 
            for ix in range(len(copy_s_list)):
                if copy_s_list[ix] == '?':
                    copy_s_list[ix] = 'a'
            cands.append(''.join(copy_s_list))
    if cands == []:
        print("UNRESTORABLE")
    else:
        print(min(cands))

            
main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque
import pdb

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    s = S()
    t = S()
    cands = []
    ans = 'z' * 50
    for st_ix in range(len(s)):
        i = st_ix 
        cur_j = 0
        copy_s_list = list(s)
        is_cand = False 
        while cur_j < len(t) and st_ix + len(t) - 1< len(s):
            if s[i] == t[cur_j] or s[i] == '?':
                copy_s_list[i] = t[cur_j]
                cur_j += 1
                i += 1
                if cur_j == len(t):
                    is_cand = True
            else:
                break

        if is_cand: 
            for ix in range(len(copy_s_list)):
                if copy_s_list[ix] == '?':
                    copy_s_list[ix] = 'a'
            cands.append(''.join(copy_s_list))
    if cands == []:
        print("UNRESTORABLE")
    else:
        print(min(cands))

            
main()

Submission status: Accepted

Solution code:
S=input()
T=input()
flg=False
ans='UNRESTORABLE'
for i in reversed(range(len(S)-len(T)+1)):
    if flg:
        break

    for j in range(len(T)):
        if S[i+j]==T[j] or S[i+j]=='?':
            pass
        else:
            break
        
        if j==len(T)-1:
            S=S.replace('?','a')
            ans=S[:i]+T+S[i+len(T):]
            flg=True
            break
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
T = list(input())
ls = len(S)
lt = len(T)
count = 0
judge = 0

for i in range(ls - lt + 1):
    for j in range(lt):
        if S[i + j] == '?' or S[i + j] == T[j]:
            count += 1

    if count == lt:
        rep = i
        judge = 1
    
    count = 0

if judge == 0:
    print('UNRESTORABLE')
else:
    for i in range(lt):
        S[rep + i] = T[i]
    S = ''.join(S)
    print(S.replace('?','a'))

Submission status: Accepted

Solution code:

Sd = input()
T  = input()
cand = []
lent = len(T)

for idx in range(len(Sd)-lent+1):
    p = 0
    while p < lent:
        if Sd[idx+p] != T[p] and Sd[idx+p] != "?":
            break

        if p == lent - 1:
            can = ""
            if idx > 0: can = Sd[:idx]
            can += T
            if idx + lent < len(Sd):
                can += Sd[(idx+lent):]
            cand.append(can.replace("?","a"))
        p += 1
if len(cand) == 0:
    print("UNRESTORABLE")
else: 
    cand.sort()
    print(cand[0])