Problem:


Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.  

One more thing he found is a sheet of paper with the following facts written on it:  

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.  

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.  

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.



Sample input:
?tc????
coder


Sample output:
atcoder

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
S = input().rstrip()
T = input().rstrip()
S_len = len(S)
T_len = len(T)
index = -1
if S_len < T_len:
  print('UNRESTORABLE')
  sys.exit()
for i in range(S_len-T_len+1):
  for j in range(T_len):
    if not(S[i+j] == T[j] or S[i+j] == '?'):
      break
    if j == T_len-1:
      index = max(index,i+j)
if index == -1:
  print('UNRESTORABLE')
  sys.exit()
else:
  ans = ''
  for i in range(S_len):
    if S[i]=='?':
      if index-T_len+1 <= i <= index:
        ans += T[i-index+T_len-1]
      else:
        ans += 'a'
    else:
      ans += S[i]
print(ans)

Submission status: Accepted

Solution code:
s = list(map(str, input().rstrip()))
t = list(map(str, input().rstrip()))

ans = []
for i in range(len(s) - len(t) + 1):
    tmp = []
    for j in range(len(t)):
        if s[i:][j] == t[j] or s[i + j] == "?":
            tmp.append(True)
        else:
            tmp.append(False)
    if all(tmp):
        tmp_l = [x.replace("?", "a") for x in s[:i]]
        tmp_r = [x.replace("?", "a") for x in s[i + j + 1:]]
        ans.append(tmp_l + t + tmp_r)

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

Submission status: Accepted

Solution code:
S,T=input(),input()
l,m=len(S),len(T)
 
for i in range(l-m+1):
  if all(c in"?"+d for c,d in zip(S[-i-m:],T)):
    S=S.replace("?","a")
    print(S[:-i-m]+T+S[l-i:])
    quit()
 
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
def main():
    UNRESTORABLE = 'UNRESTORABLE'
    INIT = 'z' * 51

    S = input()
    N = len(S)

    T = input()
    M = len(T)

    ans = INIT
    for i in range(N):
        if (N - i) < M: break
        *B, = S
        for j in range(M):
            if (B[i + j] != '?') and (B[i + j] != T[j]): break
            B[i + j] = T[j]
        else:
            ans = min(ans, (''.join(B)).replace('?', 'a'))

    if ans == INIT:
        print(UNRESTORABLE)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
import string

def main():
    S = list(input())
    T = list(input())
    accept = False
    for i in reversed(range(len(S)-len(T)+1)):
        match = True
        for j,s in enumerate(S[i:i+len(T)]):
            if s != T[j] and s != '?':
                match = False
                break
        if match:
            S[i:i+len(T)] = T
            accept = True
            break
    if accept:
        print(''.join(S).replace('?','a'))
    else:
        print('UNRESTORABLE')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

S = input()
st = len(S)
T = input()
nt = len(T)
ans = []
for i in range(st - nt + 1):
    judge = S[i:i + nt]
    flag = True
    for j in range(nt):
        if judge[j] != '?' and judge[j] != T[j]:
            flag = False
    if flag:
        ans.append(i)
alta = list(S)
for i in range(st):
    if alta[i] == "?":
        alta[i] = "a"
strlist = []
if len(ans) > 0:
    for i in ans:
        base = copy.deepcopy(alta)
        for j in range(nt):
            base[i + j] = T[j]
        strlist.append(''.join(base))
    strlist.sort()
    print(strlist[0])
else:
    print('UNRESTORABLE')

    #print(S[i:i + nt])

Submission status: Accepted

Solution code:
def printans(n):
    S = Sd[:len(Sd)-n] + T + Sd[len(Sd)-n+len(T):]
    print(S.replace('?','a'))

Sd = input()
T = input()
searching = 1
wilds_seq = 0
for i in range(1,len(Sd)+1):
    if Sd[-i] == T[-searching] or Sd[-i] == '?':
        if searching == len(T):
            printans(i)
            break
        else:
            searching += 1
            if Sd[-i] == '?':
                wilds_seq += 1
            else:
                wilds_seq = 0
    else:
        while wilds_seq > 0:
            wilds_seq -= 1
            searching -= 1
            if Sd[-i] == T[-searching]:
                searching += 1
                wilds_seq = 0
                break
        else:
            searching = 1
            wilds_seq = 0
            if len(T) >= 1:
                if Sd[-i] == T[-1]:
                    searching += 1
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import copy

sd = str(input())
t = str(input())
s = copy.deepcopy(sd)

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

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

s = S()
t = S()

Ns = len(s)
Nt = len(t)

if Nt>Ns:
  print("UNRESTORABLE")
  exit()
  
bag = set()
for i in range(Ns-Nt+1):
  flag = 1
  cut = s[i:i+Nt]
  now = ""
  for j in range(Nt):
    if cut[j] == "?":
      now += t[j]
    else:
      if cut[j] != t[j]:
        flag = 0
      else:
        now += t[j]
  if flag:
    bag.add((s[:i] + now + s[i+Nt:]).replace("?","a"))

bag = list(bag)
if not bag:
  print("UNRESTORABLE")
else:
  bag.sort()
  print(bag[0])

Submission status: Accepted

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

ans = []

for i in range(N-M+1):
    tmp_S = S[:]
    if S[i] == "?" or S[i] == T[0]:
        tmp_S[i] = T[0]
        tmp = 1
        for j in range(i+1, i+M):
            if S[j] == "?" or S[j] == T[tmp]:
                tmp_S[i+tmp] = T[tmp]
                tmp += 1
            else:
                break
        tmp_S = "".join(tmp_S)
        if T in tmp_S:
            tmp_S = tmp_S.replace("?", "a")
            ans.append(tmp_S)

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

Submission status: Accepted

Solution code:
s=[x for x in input()]
t=[x for x in input()]
ans=[]
for i in range(len(s)):
  cnt=0
  nowi=i
  fi=i
  for j in range(len(t)):
    if s[nowi]==t[j] or s[nowi]=="?":
      cnt+=1
    else:
      break
    if cnt==len(t):
      ans.append((s[:fi]+t+s[nowi+1:]))
    if nowi+1<=len(s)-1:
      nowi+=1
    else:
      break

A=[]
if len(ans)==0:
  print("UNRESTORABLE")
else:   
  for i in ans:
    moji=""
    for m in i:
      if m=="?":
        moji+="a"
      else:
        moji+=m
    A.append(moji)
  A.sort()
  print(A[0])

Submission status: Accepted

Solution code:
import re
S = str(input())
T = str(input())
S = S.replace("?",".")

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

print('UNRESTORABLE')

Submission status: Accepted

Solution code:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

s=SI()
t=SI()
n=len(s)
u=len(t)
mx=-1
for i in range(n-u+1):
    count=0
    for j in range(u):
        #print(s[i+j])
        if s[i+j]==t[j] or s[i+j]=="?":
            count+=1
    if count==u:
        mx=i
#print(mx)
if mx<0:
    print("UNRESTORABLE")
else:
    for i in range(mx):
        if s[i]=="?":
            print("a",end="")
        else:
            print(s[i],end="")
    print(t,end="")
    for i in range(mx+u,n):
        if s[i]=="?":
            print("a",end="")
        else:
            print(s[i],end="")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import  sys

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    s = sys.stdin.readline().strip()
    t = sys.stdin.readline().strip()
    words = list()

    for i in range(len(s)-len(t)+1):
        u = s[i:i+len(t)]
        u = "".join([ p if q is "?" else q for p,q in zip(t,u) ]).strip()

        if t == u:
            v = s[0:i] + t + s[i+len(t):]
            words.append( v.replace("?", "a") )

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
t = input()
ls = len(s)
lt = len(t)
ans = []
for i in range(ls-lt+1):
    for j in range(lt):
        if s[i+j] == "?":
            continue
        if s[i+j] != t[j]:
            break
    else:
        ans.append((s[:i]+t+s[i+lt:]).replace("?","a"))
ans.sort()
if ans:
    print(ans[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys
S = input()
T = input()
s = len(S)
t = len(T)
if s < t:
    print('UNRESTORABLE')
    sys.exit()

for i in range(s - t, -1, -1):
    OK = True
    for j in range(t):
        if S[i + j] != T[j] and S[i + j] != '?':
            OK = False
    if OK:
        for j in range(t):
            SS = S[:i] + T + S[i + j + 1:]
            SS = SS.replace('?', 'a')
        print(SS)
        sys.exit()

print('UNRESTORABLE')

Submission status: Accepted

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

l0 = len(s)
l = len(t)


def chk(n):
    x = s[n:n+l]
    flg = True
    for j in range(l):
        if x[j] != t[j] and x[j] != "?":
            flg = False
    return flg


res = []
for i in range(l0 - l + 1):
    if chk(i):
        res.append(i)

ans = []
if not res:
    print("UNRESTORABLE")
else:
    p = res[-1]
    y = s[:p] + t + s[p+l:]
    z = y.replace("?", "a")
    print(z)

Submission status: Accepted

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

ans = 'UNRESTORABLE'

for i in range(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
            break
    if flag:
        ans = []
        for j in range(len(S)): ans.append(S[j])
        for j in range(len(T)): ans[i+j] = T[j]
        for j in range(len(S)):
            if ans[j] == '?': ans[j] = 'a'
            
for i in range(len(ans)):
    print(ans[i], end='')
print()

Submission status: Accepted

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

ans = 'UNRESTORABLE'

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] and S[i+j] != '?':
            flag = False

    if flag:
        ans = []
        for j in range(len(S)): ans.append(S[j])
        for j in range(len(T)): ans[i+j] = T[j]
        for j in range(len(S)):
            if ans[j] == '?': ans[j] = 'a'
            
for i in range(len(ans)):
    print(ans[i], end='')
print()

Submission status: Accepted

Solution code:
import re
S = str(input())
T = str(input())
S = S.replace("?",".")

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

print('UNRESTORABLE')

Submission status: Accepted

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

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

for i in range(len(s) - len(t) + 1):
    for j, v in enumerate(t):
        if s[j+i] == v or s[j+i] == "?":
            continue
        else:
            break
    else:
        l = len(t)
        s = s[0:i] + t + s[i+l:]
        break
else:
    print("UNRESTORABLE")
    exit()

s = s.replace("?", "a")

print(s[::-1])

Submission status: Accepted

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

ans = 'UNRESTORABLE' 

def match(k):
    for i in range(len(T)):
        if T[i] != S[i+k] and S[i+k] != '?':
            return False
    return True

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

for i in range(len(S)-len(T)+1):
    if match(i):
        ans=construct(i)

for i in range(len(ans)):
    print(ans[i], end='')
print()

Submission status: Accepted

Solution code:
from collections import Counter
from collections import deque
import math
import bisect
import sys
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

s=input()
t=input()
ls=len(s)
lt=len(t)
l=[]
ans=[]
for i in range(ls-lt+1):
    ch=True
    for k in range(lt):
        if s[i+k]!=t[k] and s[i+k]!="?":
            ch=False
    if ch:
        l.append(list(s[:i]+t+s[i+lt:]))
if len(l)==0:
    print("UNRESTORABLE")
    exit()
for i in range(len(l)):
    for k in range(ls):
        if l[i][k]=="?":
            l[i][k]="a"
ans=sorted(l)
print(*ans[0],sep="")

Submission status: Accepted

Solution code:
import re
s=str(input())
s = s.replace('?', '.')
t=str(input())
for i in range(len(s)-len(t),-1,-1):
  if re.match(s[i:i+len(t)],t):
    s=s.replace(".","a")
    print(s[:i]+t+s[i+len(t):])
    exit()
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

s = list(input())[::-1]
t = list(input())[::-1]
n = len(s)
m = len(t)
res = []
for i in range(n-m+1):
    for j in range(m):
        if s[i+j] != '?' and s[i+j] != t[j]:
            break
    else:
        for k in range(n):
            if i <= k < i+m:
                res.append(t[k-i])
            else:
                if s[k] == '?':
                    res.append('a')
                else:
                    res.append(s[k])
        print(''.join(res[::-1]))
        break
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

Submission status: Accepted

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

n = len(sd)
m = len(t)

for i in range(n - m, -1, -1):
    a = sd[i:i + m]
    ok = True
    for j in range(m):
        if a[j] != "?" and a[j] != t[j]:
            ok = False
            break

    if ok:
        print(sd[:i].replace("?", "a") + t + sd[i + m:].replace("?", "a"))
        exit()

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S1 = list(input().strip())
T = list(input().strip())
n = len(S1)
k = len(T)
G = []
for i in range(n-k+1):
    flag = 0
    for j in range(i,i+k):
        if S1[j]!="?" and S1[j]!=T[j-i]:
            flag = 1
            break
    if flag==0:
        A = S1[:]
        A[i:i+k] = T[:]
        for j in range(n):
            if A[j]=="?":
                A[j] = "a"
        G.append(A)
G = sorted(G)
if len(G)>0:
    print("".join(G[0]))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys
S = str(input())
ls = len(S)
T = str(input())
lt = len(T)

if lt > ls:
  print("UNRESTORABLE")
  sys.exit()

ans = ""
for i in range(ls-lt+1):
  Flag = True
  for j in range(lt):
    #print(S[-1-i-j],T[-1-j],i,j)
    if S[-1-i-j] == T[-1-j] or S[-1-i-j] == "?":
      continue
    else:
      Flag = False
  if Flag:
    now = i #後ろから何文字目か
    break
if not Flag:
  print("UNRESTORABLE")
  sys.exit()  
now = ls - now -lt
#print(S[:now],T,S[now+lt:],now)
ans = S[:now]+T+S[now+lt:]
for i in range(len(ans)):
  if ans[i] == "?":
    ans = ans[:i]+"a"+ans[i+1:]
print(ans)

Submission status: Accepted

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

N=len(S)
TN=len(T)
ans="z"*51
if N<TN:
    print("UNRESTORABLE")
    exit()
for i in range(N-TN+1):
    #始点
    ok=True
    for j in range(TN):
        if S[i+j]==T[j] or S[i+j]=="?":
            continue
        else:
            ok=False
            break
    if ok:
        now=S[:i].replace("?", "a")+T+S[i+TN:].replace("?", "a")
        ans=min(now, ans)

if ans=="z"*51:
    ans="UNRESTORABLE"
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc076/tasks/abc076_c
# Tの位置は全探索可能
# その他で?があればすべてaに埋めてしまえばいい

S = input()
T = input()
for i in range(len(S) - len(T), -1, -1):  # iの位置からTの挿入を試みる
    sub = S[i:i + len(T)]
    is_ok = True
    for s, t in zip(sub, T):
        if s == '?':
            continue
        if s == t:
            continue
        else:
            is_ok = False
            break
    if is_ok:
        break
else:
    print('UNRESTORABLE')
    exit()

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

Submission status: Accepted

Solution code:
from copy import deepcopy

S = list(input())
T = list(input())

NS = len(S)
NT = len(T)

lst = []

for i in range(NS - NT + 1): #スタート位置
    # print (i)
    tmp = deepcopy(S)
    flag = True
    for j in range(i):
        if tmp[j] == '?':
            tmp[j] = 'a'
    for j in range(NT):
        if tmp[i + j] == '?':
            tmp[i + j] = T[j]
        elif tmp[i + j] == T[j]:
            continue
        else:
            flag = False
            break
    for j in range(i + NT, NS):
        if tmp[j] == '?':
            tmp[j] = 'a'
    if flag:
        lst.append(''.join(tmp))

lst.sort()

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()
    t = input()
    n = len(s)

    res = []
    for i in range(n):
        ss = list(s)
        if (s[i] == t[0] or s[i] == "?") and i + len(t) <= n:
            pos = 0
            for j in range(i, i + len(t)):
                if s[j] != "?" and s[j] != t[pos]:
                    break
                pos += 1
            else:
                pos = 0
                for j in range(i, i + len(t)):
                    ss[j] = t[pos]
                    pos += 1
                ss = "".join(ss).replace("?", "a")
                res.append(ss)

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


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

for i in range(len(SP)-len(T),-1,-1):
  cnt = 0
  for j in range(len(T)):
    if SP[i+j] == T[j] or SP[i+j] == "?":
      cnt = cnt +1
  if cnt == len(T):
    S = (SP[:i] + T + SP[i+len(T):]).replace("?","a")
    break
###    
if S == "":
  print("UNRESTORABLE")
else:
  print(S)

Submission status: Accepted

Solution code:
def main():
    s = input()
    t = input()
    
    s = s.replace('?','.')
    import re
    for i in range(len(s)-len(t),-1,-1):
        if re.match(s[i:i+len(t)],t):
            s = s.replace('.','a')
            print(s[:i]+t+s[i+len(t):])
            return 0
    print('UNRESTORABLE')
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
n = len(s)
t = input()
nagasa = len(t)
ans = ""
okflag = 0
for i in range(n - nagasa + 1):
    temp = True
    for j in range(nagasa):
        if s[i + j] == "?":
            continue
        else:
            if s[i + j] == t[j]:
                temp = True
            else:
                temp = False
        if temp is False:
            break
    if temp is True:
        okflag = 1
        start = i
if okflag:
    for i in range(n):
        if i >= start and i < start + nagasa:
            ans += t[i - start]
        else:
            if s[i] == "?":
                ans += "a"
            else:
                ans += s[i]
else:
    ans = "UNRESTORABLE"
print(ans)

Submission status: Accepted

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

ans = []
for l in range(len(S) - len(T) + 1):
    U = S[l: l + len(T)]
    for u, t in zip(U, T):
        if u == '?':
            continue
        if u != t:
            break
    else:
        ans.append((S[:l] + T + S[l + len(T):]).replace('?', 'a'))

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

Submission status: Accepted

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

def main():
    Sd = input()
    T = input()
    rp = len(Sd)-len(T)
    ret = []
    for i in range(rp+1):
        Td = ('?'*i) + T + ('?' * (rp-i))
        Tr = ''
        for i, c in enumerate(Sd):
            d = Td[i]
            if c == '?' and d=='?':
                Tr+='a'
            elif c == '?' and d!='?':
                Tr+=d
            elif c != '?' and d=='?':
                Tr+=c
            elif c == d:
                Tr+=d
            else:
                break
        if len(Tr)==len(Sd):
            ret.append(Tr)
    if len(ret)==0:
        print('UNRESTORABLE')
    else:
        ret.sort()
        print(ret[0])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

match = True

for i in range(len(s)):
    match = True
    if len(s) < len(t) + i:
        match = False
        break

    if s[i] == t[0] or s[i] == '?':

        for j in range(len(t)):
            if not (s[i + j] == t[j] or s[i + j] == '?'):
                match = False

        if match:
            for j in range(len(t)):
                s[i + j] = t[j]
            break

if match:
    print(''.join(reversed(s)).replace('?', 'a'))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
# from collections import defaultdict, deque
# from math import sqrt, gcd, factorial, tan, pi, sin, cos
def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline()[:-1]


def solve():
    s = [x for x in input()]
    t = [x for x in input()]
    n = len(s)
    m = len(t)
    l = -1
    for i in range(n - m + 1):
        ok = 1
        for j in range(m):
            if s[i+j] != t[j] and s[i+j] != '?':
                ok = 0
        if ok:
            l = i
    if l == -1:
        print("UNRESTORABLE")
    else:
        for j in range(m):
            s[j+l] = t[j]
        for i in range(n):
            if s[i] == '?':
                s[i] = 'a'
        
        print(''.join(s))



t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()



"""
1 -> (1, 2) -> (2 ** 3, 3) -> (3, 1)

"""

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    S_dash = input()[::-1]
    T = input()[::-1]

    is_flag = False

    res = ''

    for i in range(len(S_dash)):
        res = S_dash[:i]
        # print(res)
        if len(S_dash) - len(res) >= len(T):
            for j in range(len(T)):
                if S_dash[i+j] == T[j] or S_dash[i+j] == '?':
                    res += T[j]
                else:
                    # print('bk')
                    break
            else:
                is_flag = True
                break

        # print(res)

    # print(S_dash, T, res)
    # print(S_dash[len(T):])
    res = res + S_dash[len(T)+i:]
    res = res.replace('?','a')

    print(res[::-1]) if is_flag else print('UNRESTORABLE')     

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    S_dash = input()
    T = input()

    is_flag = False

    res = ''
    res_list = ['zzzzz']
    for i in range(len(S_dash) - len(T) +1):
        res = S_dash[:i]
        # print(i, res)
        if len(S_dash) - len(res) >= len(T):
            for j in range(len(T)):
                if S_dash[i+j] == T[j] or S_dash[i+j] == '?':
                    res += T[j]
                else:
                    # print('bk')
                    break
            else:
                is_flag = True
                res += S_dash[i+len(T):]
                res_list.append(res)


    # print(res_list)

    res2_list = []
    for res in res_list:
        res = res.replace('?','a')
        res2_list.append(res)

    res2_list.sort()
    res = res2_list[0]

    print(res) if is_flag else print('UNRESTORABLE')     

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
from collections import deque
import re
s = input()
t = input()

d = deque()
for i, ch in enumerate(s[::-1]):
    d.appendleft('.' if ch == '?' else ch)
    if len(d) < len(t):
        continue
    if re.match(''.join(list(d)[:len(t)]), t):
        ans = s[:len(s) - i - 1] + t + s[len(s) - i - 1 + len(t):]
        print(ans.replace('?', 'a'))
        exit()
print('UNRESTORABLE')