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:
# 問題 https://abc076.contest.atcoder.jp/tasks/abc076_c
S = input()
T = input()
Tlen = len(T)
Slen = len(S)
# Tの入りうる箇所の探索
# Tは入りうる箇所の一番うしろに入れて
# 前の?をすべてaで埋め尽くせばよい

for i in range(Slen-Tlen, -1, -1):
    # print(i)
    for s, t in zip(S[i:i + Tlen], T):
        flg = True
        if s != t and s != '?':
            flg = False
            break
    if flg:
        insertidx = i
        break
else:
    print('UNRESTORABLE')
    import sys
    sys.exit()

ans = S[:insertidx] + T + S[insertidx + Tlen:]
ans = ans.replace('?', 'a')
print(ans)

Submission status: Accepted

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

sr = s[::-1]
tr = t[::-1]

for i in range(len(sr)-len(tr)+1):
    srp = sr[i:i+len(tr)]
    for j in range(len(srp)):
        if srp[j] != '?' and srp[j] != tr[j]:
            break
    else:
        ans = sr.replace('?', 'a')
        ans = ans[0:i] + tr + ans[i+len(tr):]
        print(ans[::-1])
        exit()
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

sr = s[::-1]
tr = t[::-1]

for i in range(len(sr)-len(tr)+1):
  srp = sr[i:i+len(tr)]
  for j in range(len(srp)):
    if srp[j] != '?' and srp[j] != tr[j]:
      break
  else:
    ans = sr.replace('?', 'a')
    ans = ans[0:i] + tr + ans[i+len(tr):]
    print(ans[::-1])
    exit()
else:
  print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s=input()
t=input()
 
if len(s) < len(t):
  print("UNRESTORABLE")
  exit()
 
c=len(s)-len(t)
 
flg=False
while True:
  if c<0:
    break
  
  flg2=True
  for i in range(len(t)):
    if s[c+i]!=t[i] and s[c+i]!="?":
      flg2=False
  if flg2:
    flg=True
    break
  c-=1
#print(c)
 
 
 
if flg:
  print((s[:c]+t+s[c+len(t):]).replace("?","a"))
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

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

c=len(s)-len(t)

flg=False
while True:
  if c<0:
    break
  
  flg2=True
  for i in range(len(t)):
    if s[c+i]!=t[i] and s[c+i]!="?":
      flg2=False
  if flg2:
    flg=True
    break
  c-=1
#print(c)



if flg:
  print((s[:c]+t+s[c+len(t):]).replace("?","a"))
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

# Tが当てはまる？
s = 0 # Tが当てはまる最初のインデックス
for i in range(len(S)-len(T)+1)[::-1]:
    flag = 1
    for j in range(len(T)):
        if S[i+j] != "?" and S[i+j] != T[j]:
            flag = 0
            break
    if flag:
        s = i
        break

if len(S) < len(T) or not flag:
    print("UNRESTORABLE")
else: # Tが当てはまった場合
    S = S[:s] + T + S[s+len(T):]
    # "?"を"a"で埋める
    S = S.replace("?", "a")
    print(S)

Submission status: Accepted

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

ls = len(S)
lt = len(T)
ret = 'UNRESTORABLE'
for i in range(ls-lt+1):
    judge = True
    for t in range(lt):
        if S[i+t] != T[t] and S[i+t] != "?":
            judge = False
            break
    if judge:
        ret = S[:i] + T + S[i+lt:]
ret = ret.replace("?", "a")
print(ret)

Submission status: Accepted

Solution code:
# https://abc076.contest.atcoder.jp/tasks/abc076_c

S = input()
T = input()

s_rev = "".join(reversed(S))
t_rev = "".join(reversed(T))

found = False
for i in range(len(S) - len(T) + 1):
    yn = all(s_rev[i + k] == "?" or s_rev[i + k] == t_rev[k] for k in range(len(T)))
    if yn:
        s_rev = s_rev[:i] + t_rev + s_rev[i + len(T) :]
        found = True
        break

if found:
    s = "".join("a" if c == "?" else c for c in reversed(s_rev))
    print(s)
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
Sd = list(input())
A = list(input()) #部分文字列


def check(Sd, A):
    ans = -1
    for i in range(len(Sd)-1, -1, -1): #リストSd内の文字を後ろから順に呼び出すため
        count = 0
#        print ('i=', i)
        for j in range(len(A)): #後ろから何文字目か
#            print ('j=', j)
#            print ('Sd[i-j]', Sd[i-j])
            if Sd[i - j] == A[len(A)-1 - j] or Sd[i - j] == '?':
                count += 1
                if count == len(A):
                    ans = i-j #全て変わる時の一番最初の文字
                    return ans
    return ans

a = check(Sd, A)

if a == -1:
    print ('UNRESTORABLE')
else:
    i = a
    while i < a + len(A):
        Sd[i] = A[i-a]
        i += 1
    for j in range(len(Sd)):
        if Sd[j] == '?':
            Sd[j] = 'a'
    print (''.join(Sd))

Submission status: Accepted

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

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

if flag:
    s = ''
    k = 0
    for i in range(len(S)):
        if index <= i <= index + len(T) - 1:
            s += T[k]
            k += 1
            continue
        if S[i] == '?':
            s += 'a'
        else:
            s += S[i]
else:
    s = 'UNRESTORABLE'

print(s)

Submission status: Accepted

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

index = []
ss = []
flag = False

for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if S[i+j] != T[j] and S[i+j] != '?':
            break
        if j == len(T) - 1:
            flag = True
            index.append(i)

if flag:
    for v in index:
        s = ''
        k = 0
        for i in range(len(S)):
            if v <= i <= v + len(T) - 1:
                s += T[k]
                k += 1
                continue
            if S[i] == '?':
                s += 'a'
            else:
                s += S[i]
        ss.append(s)
    ss.sort()
    s = ss[0]
else:
    s = 'UNRESTORABLE'

print(s)

Submission status: Accepted

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

s = [x for x in s]
t = [x for x in t]

# 合致しているか返す関数
def check_match(s, t):
    result = ['?' for _ in range(len(s))]
    for i in range(len(s)):
        s_ = s[i]
        t_ = t[i]
        if s_ == '?':
            result[i] = t_
        elif t_ == '?':
            result[i] = s_
        elif s_ == t_:
            result[i] = t_
        else:
            return None

        output = ''
        for x in result:
            output += x if x != '?' else 'a'
    return output
  
diff = len(s) - len(t)
results = []

for i in range(diff+1):
    t_new = ['?'] * i + t + ['?'] * (diff - i) 
    result = check_match(s, t_new)
    
    if result:
        results.append(result)
        
if len(results):
    print(sorted(results)[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()
T = input()
ls = len(S)
lt = len(T)
for i in range(ls-lt,-1,-1):
    for j in range(lt):
        if S[i+j] != "?" and S[i+j] != T[j] :
            break
    else:
        #置換処理
        S = [c if c != "?" else "a" for c in S]
        S[i:i+lt] = list(T)
        S = "".join(S)
        break
else:
    S = "UNRESTORABLE"
print(S)

Submission status: Accepted

Solution code:
from functools import reduce
import math
import copy
def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    S_dash = list(_ for _ in input())
    T = list(_ for _ in input())
    S_dash = S_dash[::-1]
    T = T[::-1]
    for i in range(len(S_dash)-len(T)+1):
        flag = False
        for j in range(i,i+len(T)):
            if S_dash[j] == T[j-i] or S_dash[j] == '?':
                flag = True
            else:
                flag = False
                break
        if flag:
            for j in range(i,i+len(T)):
                S_dash[j] = T[j-i]
            break

    S = ''.join(S_dash[::-1]).replace('?','a')

    if S.count(''.join(T[::-1])) == 0:
        print('UNRESTORABLE')
        return
    print(S)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

    return s, t


# 長さn、tを含むパターンを形成
def make_pattern(n, t):
    m = len(t)

    patterns = []
    for i in range(n - m + 1):
        patterns.append(['?'] * i + t + ['?'] * (n - i - m))

    return patterns

# aとbの要素ごとの比較
# ?と*はいずれともマッチする
def check_match(a, b):
    def convert(x, y):
        if x == '?' and y == '?':
            return 'a'
        if x == y:
            return x
        if x == '?':
            return y
        if y == '?':
            return x
        return None

    result = []
    for i in range(len(a)):
        result.append(convert(a[i], b[i]))

    return result

def submit():
    s, t = read_input()

    s = [c for c in s]
    t = [c for c in t]

    patterns = make_pattern(len(s), t)

    candidates = [check_match(s, p) for p in patterns]
    candidates = [''.join(c) for c in candidates if None not in c]
    candidates.sort()

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

if __name__ == '__main__':
    submit()

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

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

    sl = len(s)
    tl = len(t)

    if t in s:
        for i in range(sl):
            if s[i]=='?':
                print('a', end='')
            else:
                print(s[i], end='')
        print()
        return

    for i in range(sl-1, -1, -1):
        if i+tl-1>=sl:
            continue
        ok = 1
        for j in range(tl):
            if s[i+j] not in ['?', t[j]]:
                ok = 0
                break
        if ok:
            s = s[:i] + t + s[i+tl:]
            for i in range(sl):
                if s[i]=='?':
                    print('a', end='')
                else:
                    print(s[i], end='')
            
            print()
            return

    print('UNRESTORABLE')
            

solve()

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().strip()
t = input().strip()

print(solve(s, t))

Submission status: Accepted

Solution code:
s=input()
t=input()
ns,nt,nd=len(s),len(t),len(s)-len(t)
ans="z"*ns
flag=0
for i in range(nd+1):
  judge=True
  for j in range(nt):
    ss=s[i+j]
    tt=t[j]
    if ss!="?" and ss!=tt:
      judge=False
  if judge:
    Key=s[:i]+t+s[i+nt:]
    ans=min(ans,Key.replace("?","a"))
    flag=1
print(ans if flag==1 else "UNRESTORABLE")

Submission status: Accepted

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

sl = len(S)
tl = len(T)

for i in range(sl-tl,-1,-1):
    flag = True
    for j in range(tl):
        s = S[i+j]
        t = T[j]
        if (s != '?')and(s != t):
            flag = False
            break
    if flag == True:
        for j in range(tl):
            S[i+j] = T[j]
        for k in range(sl):
            if S[k] == '?':
                S[k] = 'a'
        print("".join(S))
        exit()

print("UNRESTORABLE")

Submission status: Accepted

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

if len(S) < N:
    print("UNRESTORABLE")
    import sys
    sys.exit()

flag = False
pos = 51
ans = ""
for i in range(len(S)-N+1):
    cnt = 0
    for j in range(N):
        if S[i+j] == "?" or S[i+j] == T[j]:
            cnt += 1
        else:
            break
    if cnt == N:
        pos = i
        flag = True
if flag:
    for i in range(len(S)):
        if pos <= i and i < pos + N:
            ans += T[i-pos]
        elif S[i] == "?":
            ans += "a"
        else:
            ans += S[i]
else:
    ans = "UNRESTORABLE"
print(ans)

Submission status: Accepted

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

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

c=len(s)-len(t)
flg=False
while True:
  if c<0:
    break
  
  flg2=True
  for i in range(len(t)):
    if s[c+i]!=t[i] and s[c+i]!="?":
      flg2=False
  if flg2:
    flg=True
    break
  c-=1
if flg:
  print((s[:c]+t+s[c+len(t):]).replace("?","a"))
else:
  print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import copy

S = input()
T = input()
if len(S) < len(T):
  print("UNRESTORABLE")
  exit()
flg = True
Sarray = []
for i in range(len(S)-len(T)+1):
  flg=True
  Stmp = copy.copy(S)
  for idx, c in enumerate(T):
    if Stmp[i+idx]!=c and Stmp[i+idx]!='?':
      flg=False
      break
  if flg:
    Stmp = Stmp[:i] + T + Stmp[len(T)+i:]
    Stmp = Stmp.replace('?', 'a')
    Sarray.append(Stmp)
if len(Sarray)==0:
  print("UNRESTORABLE")
  exit()
else:
  Sarray.sort()
  print(Sarray[0])

Submission status: Accepted

Solution code:
import sys
S=input()
T=input()
if len(S)<len(T):
    print('UNRESTORABLE')
    sys.exit()
L=[]
for i in range(len(S)-len(T)+1):
    K=S[i:i+len(T)]
    flag=True
    #print(K)
    for j in range(len(T)):
        if T[j]!=K[j] and K[j]!='?':
            flag=False
    if flag:
        U=S[:i]+T+S[i+len(T):]
        L.append(U.replace('?','a'))
#print(L)
if len(L)!=0:
    print(min(L))
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

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


for i in range(s-t, -1, -1):
    n = 0
    for j in range(t-1, -1, -1):
        if S[j+i] != '?' and S[j+i] != T[j]:
            break
        n += 1

    if n == t:
        for x in range(s):
            if i <= x < i+t:
                print(T[x-i], end='')
            else:
                if S[x] == '?':
                    print('a', end='')
                else:
                    print(S[x], end='')
        exit()

print('UNRESTORABLE')

Submission status: Accepted

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

temp = ""
is_restorable = False
for i in range(len(S) - len(T) + 1):
    cand = S[i:i + len(T)]
    for ci, ti in zip(cand, T):
        if ci not in ["?", ti]:
            break
    else:
        temp = S[:i] + T + S[i + len(T):]
        is_restorable = True

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

Submission status: Accepted

Solution code:
def is_match(i, Sd, T):
    for j in range(len(T)):
        if Sd[i + j] != T[j] and Sd[i + j] != '?':
            return False
    return True


Sd = input()
T = input()
diff = len(Sd) - len(T)
len_t = len(T)
matches = []
for i in range(diff + 1):
    if Sd[i] == T[0] or Sd[i] == '?':
        # Sd[i+j]とT[j]を比較してokならappend list
        if is_match(i, Sd, T):
            matches.append(Sd[:i] + T + Sd[i + len_t:])

# print(matches)
tmps = []
for m in matches:
    s = []
    for i in range(len(m)):
        if m[i] == '?':
            s.append('a')
        else:
            s.append(m[i])
    tmps.append(''.join(s))
print(min(tmps, default='UNRESTORABLE'))

Submission status: Accepted

Solution code:
source = input()
target = input()
t_len = len(target)
answer = "z" * 51
for i in range(len(source) - t_len + 1):
    for j in range(i, i + t_len):
        # print(i, source[j], target[j - i])
        if source[j] == target[j - i] or source[j] == "?":
            pass
        else:
            break
    else:
        temp_s = source[:i] + target + source[i + t_len:]
        temp_s = temp_s.replace("?", "a")
        answer = min(answer, temp_s)

if answer == "z" * 51:
    print("UNRESTORABLE")
else:
    print(answer)

Submission status: Accepted

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

	T_length = len(T)
	head = -1
	for i in range(len(S)-T_length+1):
		counter = 0
		for j in range(T_length):
			if(S[i+j]==T[j] or S[i+j]=='?'):
				counter += 1
		if(counter == T_length):
			head = i
				
	ans = ''
	i=0
	if(head == -1):
		print("UNRESTORABLE")
	else :
		while i < len(S):
			if(i!=head and S[i]=='?'):
				ans += 'a'
				i += 1
			elif(i==head):
				ans += T
				i += T_length
			else :
				ans += S[i]
				i+=1

		print(ans)

Submission status: Accepted

Solution code:
s = input()[::-1]
t = input()[::-1]
for i,c in enumerate(s):
  if (c == t[0] or c == '?') and (i+len(t) < len(s)+1):
    for j in range(len(t)):
      if t[j] != s[i+j] and s[i+j] != '?':
        break
    else:
      str_list = list(s)
      for j in range(len(t)):
        str_list[i+j] =  t[j]
      s = ''.join(str_list)
      print(s.replace('?', 'a')[::-1])
      break
else:
  print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = [i for i in input()]
T = input()
tmp = S.copy()

key = len(T)

SS = []
for i in range(len(S)-key+1):
    idx, flg = 0, 0
    for j in range(i, i+key):
        if S[j] != T[idx] and S[j] != '?':
            flg = 1
            break
        idx += 1
    if flg == 0:
        idx = 0
        for j in range(i, i+key):
            S[j] = T[idx]
            idx += 1
        SS.append(S)
        S = tmp.copy()

if SS:
    idx = 0
    for i in SS:
        idx_2 = 0
        for j in i:
            if j == '?':
                SS[idx][idx_2] = 'a'
            idx_2 += 1
        idx += 1
    print(''.join(min(SS)))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s = input()
t = input()
ns = len(s)
nt = len(t)

ans = "UNRESTORABLE"
for i in range(ns - nt + 1):
    if all([s[i + j] == "?" or s[i + j] == t[j] for j in range(nt)]):
        ans = s[:i] + t + s[i + nt:]
        ans = ans.replace("?", "a")
print(ans)

Submission status: Accepted

Solution code:
s_dash = input()
t = input()
n = len(s_dash)
m = len(t)

s = "UNRESTORABLE"
cnt = 0
for i in range(n - m + 1):
    cnt = 0
    for j in range(m):
      if s_dash[i+j] == t[j] or s_dash[i+j] == "?":
        cnt += 1
      else:
        break
    if cnt == m:
      s = s_dash[:i] + t + s_dash[i+m:]
      s = s.replace("?", "a")

      
print(s)

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 s, t in zip(S[i:i+len(T)], T):
        if s != "?" and s != t:
            break
    else:
        S = list(S)
        S[i:i+len(T)] = list(T)
        S = "".join(S).replace("?", "a")
        print(S)
        exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:

import sys


def parse(file):
    s = file.readline().strip()
    t = file.readline().strip()
    return s, t


def main(s, t):
    end = len(s) - len(t)
    for start in reversed(range(end + 1)):
        found = True
        for i in range(len(t)):
            if s[start + i] == t[i] or s[start + i] == '?':
                continue
            else:
                found = False
                break
        if not found:
            continue

        if found:
            ans = s[:start] + t + s[start+len(t):]
            return ans.replace('?', 'a')

    return 'UNRESTORABLE'


if __name__ == '__main__':
    print(main(*parse(sys.stdin)))

Submission status: Accepted

Solution code:
S_= list(input())
T = list(input())
n,m = len(S_),len(T)

#S_をｍ文字ずつ切ってTに一致させられるものがあるかを確認
#なるべく後ろの?でTに一致させたい ->　反転させたものを使う,前の?にaを入れる方が辞書的に早いので
#(ex)
#?tc???? -> ????ct?
#coder -> redoc
#にして,前から調べて一致させることができたら,残りの?をaにしてreverse(元に戻す）
S_.reverse(),T.reverse()
key_index = -1 #-1:なし,x:一致させることができた場合の最初のindex
judge = 0 #0:なし,1:あり
for i in range(n):
  if((S_[i] == '?')or(S_[i] == T[0])):
    if(i+m-1 <= n-1): 
     tmp = S_[i:i+m]
     judge = 1
     for j in range(m):
        if((tmp[j] == T[j])or(tmp[j] == '?')):
          continue
        else:
          judge = 0
          break
  if(judge == 1):
    key_index = i
    break
 
if(judge == 0):
  print('UNRESTORABLE')
else:
  j = 0
  for i in range(n):
    if(key_index <= i <= key_index+m-1):
      S_[i] = T[j]
      j += 1
    elif(S_[i] == '?'):
      S_[i] = 'a'
  S_.reverse()
  ans = ''.join(S_)
  print(ans)

Submission status: Accepted

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


ss = []

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:
                sss = s[:i]+t+s[i+j+1:]
                sss = sss.replace('?', 'a')
                ss.append(sss)
        else:
            break

ss.sort()

print('UNRESTORABLE' if ss==[] else ss[0])

Submission status: Accepted

Solution code:
S = raw_input()
T = raw_input()
ans = 'UNRESTORABLE'
n = len(S)-len(T)+1
if n > 0:
	for i in range(0, n):
		flag = False
		for j in range(0, len(T)):
			if S[-1-i-j] != '?' and S[-1-i-j] != T[-1-j]: break
			if j == len(T)-1: flag = True
		if flag:
			ans = ''
			for j in range(0, len(S)-i-len(T)):
				if S[j] == '?': ans = ans + 'a'
				else: ans = ans + S[j]
			ans = ans + T
			for j in range(len(S)-i, len(S)):
				if S[j] == '?': ans = ans + 'a'
				else: ans = ans + S[j]
			break
print ans

Submission status: Accepted

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

print(solve())

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    return

#B
def B():
    return

#C
def C():
    s = S()
    t = S()
    ansa = []
    anslis = []
    lisans = []
    for i in range(len(s)):
        for j in range(len(t)):
            if 0 <= i + j < len(s) and (s[i + j] == t[j] or s[i + j] == "?"):
                continue
            break
        else:
            ansa.append((i, i + j))
    if len(ansa) == 0:
        print("UNRESTORABLE")
        return
    for ans in ansa:
        a = ""
        for num in range(len(s)):
            i = s[num]
            if ans[0] <= num <= ans[1]:
                i = t[num-ans[0]]
            a += i
        anslis.append(a)
    for ans in anslis:
        k = ""
        for i in ans:
            if i == "?":
                k += "a"
            else:
                k += i
        lisans.append(k)
    lisans.sort()
    print(lisans[0])
        
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()


#A
"""
r = I()
g = I()
print(2*g-r)
"""

#B
"""
n = I()
k = I()
ans = 1
for i in range(n):
    ans = min(ans * 2, ans + k)
print(ans)
"""



    


#D
"""
n = I()
t = LI()
v = LI()
"""
if __name__ == "__main__":
    C()

Submission status: Accepted

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

if len(s) < len(t):
  print('UNRESTORABLE')
else:
  cands = []
  for i in range(len(s) - len(t) + 1):
    is_match = True
    for _s, _t in zip(s[i:i+len(t)], t):
      if _s != '?':
        if _s != _t:
          is_match = False
          break
    if is_match:
      cand = s.replace('?', 'a')
      cand = cand[:i] + t + cand[i+len(t):]
      cands.append(cand)
  if cands:
    print(list(sorted(cands))[0])
  else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()
T = input()
t = len(T)
indices = []
for i in range(len(S) - t + 1):
    for j, s in enumerate(S[i:i+t]):
        if s == '?':
            continue
        if s != T[j]:
            break
    else:
        indices.append(i)
if len(indices) == 0:
    print('UNRESTORABLE')
else:
    cand = []
    for ind in indices:
        ans = ''
        for i, s in enumerate(S):
            if i == ind:
                ans += T
            elif ind < i < ind + t:
                continue
            elif s == '?':
                ans += 'a'
            else:
                ans += s
        cand.append(ans)
    cand = sorted(cand)
    print(cand[0])

Submission status: Accepted

Solution code:
input_A = input()
input_T = input()
input_A, input_T = list(input_A), list(input_T)

if len(input_A) >= len(input_T):
    for i in range(len(input_A)-len(input_T), -1, -1):
        clear_flg = True
        for j in range(len(input_T)):
            if not (input_A[i + j] == '?' or input_A[i + j] == input_T[j]):
                clear_flg = False
                break

        if clear_flg is True:
            input_A[i:i + len(input_T)] = input_T
            input_A = ''.join(input_A).replace('?', 'a')
            break
else:
    clear_flg = False

if clear_flg is True:
    print(input_A)
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-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 _pf(s): return print(s, flush=True)

S = input()
T = input()
if len(S) < len(T):
    print('UNRESTORABLE')
    exit()
result = []
for sidx, s in enumerate(S):
    #  print('sidx,s', sidx, s)
    # どこから始めるかで考える
    if sidx + len(T) > len(S):
        continue
    for tidx, t in enumerate(T):
        #  print('tidx, t', tidx, t)
        if S[sidx+tidx] == '?':
            continue
        if t != S[sidx+tidx]:
            # 一致しない場合はこれではない
            break
    else:
        # 鍵発見
        #  print('sidx', sidx)
        ans = S[:sidx] + T
        if sidx + len(T) < len(S):
            ans += S[sidx+len(T):]
        ans = ans.replace('?', 'a')
        result.append(ans)
if result:
    print(min(result))
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

s = list(input())
t = list(input())

slen = len(s)
tlen = len(t)

cand = []
for i in range(slen - tlen + 1):
    match = True
    for j in range(tlen):
        if s[i + j] == "?" or s[i + j] == t[j]:
            pass
        else:
            match = False
    if match:
        cand.append(i)

if len(cand) != 0:
    cand.reverse()
    ans = ""
    for i in range(slen):
        if not (cand[0] <= i < cand[0] + tlen):
            if s[i] == "?":
                ans += "a"
            else:
                ans += s[i]
        else:
            ans += t[i - cand[0]]
    print(ans)
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
for i in reversed(range(len(S) - len(T) + 1)):
    frag = True
    for j in range(len(T)):
        if S[i + j] == T[j] or S[i + j] == "?":
            continue
        else:
            frag = False
            break
    if frag:
        ans = S[:i] + T
        if i + len(T) < len(S):
            ans += S[i + len(T):]
        ans = ans.replace("?", "a")
        print(ans)
        exit()
print("UNRESTORABLE")

Submission status: Accepted

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

Sd = Sd[::-1]
T = T[::-1]
flg = False
for s in range(len(Sd) - len(T)+1):
    # print(Sd[s:s + len(T)])
    cnt = 0
    for t in range(len(T)):
        # print(Sd[s+t:s + t+1], T[t])
        if Sd[s+t:s + t+1] == T[t] or Sd[s+t:s + t+1] == "?":
            cnt += 1
        else:
            continue
    if cnt == len(T):
        S = Sd.replace(Sd[s:s + len(T)], T, 1)
        S = S.replace("?", "a")
        print(S[::-1])
        flg = True
        break

if flg is False:
    print("UNRESTORABLE")

Submission status: Accepted

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

matched_index = -1
for i in range(len(S)-len(T)+1) :
    s = S[i:i+len(T)]
    for j in range(len(T)) :
        if (s[j] != "?" and s[j] != T[j]) :
            break
    else :
        # 複数マッチの場合は後ろのほうがいいのでこうする
        matched_index = i

if (matched_index > -1) :
    out_str = S[0:matched_index] + T + S[matched_index+len(T):]
    out_str = out_str.replace("?", "a")
    print(out_str)
else :
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
sp = input()
t = input()
len_t = len(t)
len_s = len(sp)
cand = []
part_start = -1

for i in range(len_s - len_t + 1):
    num = 0
    for j in range(len_t):
        if (sp[i + j] == '?' or sp[i + j] == t[j]):
            num += 1

    if (num == len_t):
        part_start = i

if part_start != -1:
    # 部分文字列は最後にあったほうがいい?はaで最小化できる可能性があり、文字列の先頭に近い部分にそれらがあったほうがよい
    ans = sp[:part_start] + t + sp[part_start + len_t:]
    ans = ans.replace('?', 'a')
    print(ans)
else:
    print('UNRESTORABLE')