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:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    S_prime=readline().strip()
    T=readline().strip()

    U=list(S_prime)
    for i in range(len(U)):
        if U[i]=="?":
            U[i]="a"

    words=[]
    for i in range(len(S_prime)-len(T)+1):
        V=U[:]
        for j in range(i,i+len(T)):
            if S_prime[j]!=T[j-i]:
                if S_prime[j]!="?":
                    break
            V[j]=T[j-i]
        else:
            words.append("".join(V))

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

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import re

s2 = input().replace('?','.')
t = input()
a, b = len(s2), len(t)
ans = None

for i in range(a-b+1):
    s2_l = list(s2)
    t_l = list(t)
    if re.match(s2[i:i+b], t) is not None:
        for k in range(b):
            s2_l[i+k] = t_l[k]
        #ans = s2_l.replace(s2_l[i:i+b], t_l[0:b]).replace('.','a')
        ans = "".join(s2_l).replace('.','a')
if ans is None:
    print('UNRESTORABLE')
else:
    print(ans)

Submission status: Accepted

Solution code:
import sys

S = list(sys.stdin.readline().rstrip())
T = list(sys.stdin.readline().rstrip())

len_s = len(S)
len_t = len(T)

for i in range(len_s - len_t, -1, -1):
    for j in range(len_t):
        if S[i+j] == T[j] or S[i+j] == "?":
            continue
        else:
            break
    else:
        for k in range(len_t):
            S[i+k] = T[k]

        print(("".join(S)).replace("?","a"))
        exit()

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
chiper = input()
key = input()
save = []
cl = len(chiper)
kl = len(key)

def writeWord(ci):
    word=''
    for i in range(ci):
        if(chiper[i]=='?'):
            word += 'a'
        else:
            word += chiper[i]

    word += key
    for i in range(ci+kl,cl):
        if(chiper[i]=='?'):
            word += 'a'
        else:
            word += chiper[i]

    return word

def search():
    if(cl<kl):
        return
    last= cl-kl
    for i in range(last+1):
        for j in range(kl):
            if(chiper[i+j]=='?' or chiper[i+j]==key[j]):
                if(j==kl-1):
                    word = writeWord(i)
                    save.append(word)
            else:
                break
        
search()
if(len(save)==0):
    print('UNRESTORABLE')
else:
    save.sort()
    print(save[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:
s_dash = input()
t = input()

s = ''

slen = len(s_dash)
tlen = len(t)

k = None

for i in range(slen-tlen+1):
    for j in range(tlen):
        if s_dash[i+j] != t[j] and s_dash[i+j] != '?':
            break
    else:
        k = i

if k == None:
    print('UNRESTORABLE')
    exit()

for i in range(k):
    if s_dash[i] == '?':
        s = s + 'a'
    else:
        s = s + s_dash[i]
for i in range(tlen):
    s = s + t[i]
for i in range(k+tlen,slen):
    if s_dash[i] == '?':
        s = s + 'a'
    else:
        s = s + s_dash[i]
print(s)

Submission status: Accepted

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


input = sys.stdin.readline
def IS(cb): return cb(input().strip())
def IL(cb): return [cb(s) for s in input().strip().split()]
def IR(cb, rows): return [IS(cb) for _ in range(rows)]
def ILL(cb, rows): return [IL(cb) for _ in range(rows)]


def solve():
    Sd = IS(str)
    T = IS(str)
    T_len = len(T)

    for i in range(len(Sd) - T_len, -1, -1):
        S_i = Sd[i: i + T_len]
        matched = all([s == '?' or s == t for s, t in zip(S_i, T)])
        if matched:
            left = ''.join(['a' if s == '?' else s for s in Sd[: i]])
            right = ''.join(['a' if s == '?' else s for s in Sd[i + T_len:]])
            print(left + T + right)
            exit()
    print('UNRESTORABLE')

solve()

Submission status: Accepted

Solution code:
s = str(input())
t = str(input())
lst = []
i= 0
for ss in range(len(s)-len(t)+1):
  flg = 1
  i= 0
  for tt in range(len(t)):
    now = ss + tt
    if s[now] == t[tt] or s[now] == '?':
      pass
    else:
      flg = 0
  if flg:
    ns = list(s)
    ns[ss:now+1] = list(t)
    lst.append(("").join(ns))
if lst:
  lst = [_.replace('?','a') for _ in lst]
  lst.sort()
  print(lst[0])

else:
  print('UNRESTORABLE')

Submission status: Accepted

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

if len(s_) < len(t):
    print("UNRESTORABLE")
    exit(0)

flag = True
for i in range(len(s_) - len(t), -1, -1):
    flag = True
    for j in range(len(t)):
        if s_[i + j] != t[j] and s_[i + j] != "?":
            flag = False
    if flag is True:
        s_ = s_[0:i] + t + s_[i + len(t):]
        break

if flag is False:
    print("UNRESTORABLE")
else:
    print(s_.replace("?", "a"))

Submission status: Accepted

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


def can_contains(S, T):
  for s, t in zip(S, T):
    if s != '?' and s != t:
      return False

  return True


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

  len_T = len(T)

  if len(Sd) < len_T:
    print('UNRESTORABLE')
    return 

  for idx in range(len(Sd) - len_T, -1, -1):
    if can_contains(Sd[idx:idx + len_T], T):
      result = Sd.replace('?', 'a')
      result = result[:idx] + T + result[idx + len_T:] 
      
      print(result)
      return

  print('UNRESTORABLE')


if __name__ == '__main__':
  main()

Submission status: Accepted

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


i=len(Sd)-len(T)
j=0
ans=[]
flag=False
while(1):
    if i<0:
        #print('not find')
        break
    #print('searching',i,j,Sd[i],T[j])
    if Sd[i]==T[j] or Sd[i]=='?':
        #print('find',i,j,Sd[i],T[j])
        if j==len(T)-1:
            ans.append((Sd[:i-j]+T+Sd[i-j+len(T):]).replace('?','a'))
            i-=j+1
            j=0
            flag=True
            continue
        j+=1
        i+=1
    else:
        i-=j+1
        j=0

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

Submission status: Accepted

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

b = []

for i in range(len(s)-len(t)+1):
    flag = True
    for j, st in enumerate(s[i:i+len(t)]):
        if not st == t[j] and not st == '?':
            flag = False
            break
    if flag:
        heapq.heappush(b,(s[:i]+t+s[i+len(t):]).replace('?','a'))

if not b:
    print('UNRESTORABLE')
else:
    print(heapq.heappop(b))

Submission status: Accepted

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

matches = []
for i in range(len(S) - len(T) + 1):
    index = i
    for j in range(len(T)):
        if S[index] == '?' or S[index] == T[j]:
            if j == len(T) -1:
                matches.append(i)
            index += 1
        else:
            break

if len(matches) == 0:
    print('UNRESTORABLE')
else:
    strings = []
    for i in matches:
        strings.append(''.join(S[:i] + T[:] + S[i+len(T):]).replace('?', 'a'))
    print(sorted(strings)[0])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def read():
    S = input().strip()
    T = input().strip()
    return S, T


def solve(S, T, INF = 100000):
    S = '.' + S
    T = '.' + T
    dp = [[INF for j in range(len(T))] for i in range(len(S))]
    
    offset = -1
    for i in range(len(S)):
        dp[i][0] = 0
    for j in range(len(T)):
        dp[0][j] = 0
    for i in range(1, len(S)):
        for j in range(1, len(T)):
            if S[i] == '?' or T[j] == '?' or S[i] == T[j]:
                dp[i][j] = dp[i-1][j-1] + 1
                if dp[i][j] == len(T)-1:
                    offset = i - (len(T)-1)
            else:
                dp[i][j] = 0
    
    if offset == -1:
        return 'UNRESTORABLE'
    U = [s if s != '?' else 'a' for s in S[1:]]
    for i in range(len(T)-1):
        U[offset + i] = T[i+1]
    return ''.join(U)
            

if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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:
s=list(input())
t=list(input())
s_len=(len(s))
t_len=(len(t))

for i in range(s_len-t_len,-1,-1):
    for j in range(t_len):
        if s[i+j]==t[j] or s[i+j]=="?":
            continue
        else:
            break
    else:
        for k in range(t_len):
            s[i+k]=t[k]
        
        print(("".join(s)).replace("?","a"))
        exit()

print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=input()
t=input()
s_len=(len(s))
t_len=(len(t))

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:
        print((s[:i]+t+s[i+t_len:]).replace("?","a"))
        exit()

print("UNRESTORABLE")

Submission status: Accepted

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

cand = []

for i in range(len(s)-len(t)+1):
    
    # i番目からtと同じ長さの文字列を取りだす
    td = s[i: i+len(t)]
    
    for j in range(len(t)):
        if td[j] == '?':
            td = td[0:j] + t[j] +td[j+1:]
        elif td[j] != t[j]:
            break;
    
    if td == t:
        cand.append(s[0:i] + td + s[i+len(t):])


if len(cand) == 0:
    print('UNRESTORABLE')
else:
    cand = list(map(lambda x: x.replace('?', 'a'), cand))
    print(min(cand))

Submission status: Accepted

Solution code:
S,T=input(),input()
s,t=len(S),len(T)

for i in range(s-t+1):
  if all(c in "?"+d for c,d in zip(S[-i-t:],T)):
    S=S.replace("?","a")
    print(S[:-i-t]+T+S[s-i:])
    quit()

print("UNRESTORABLE")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
Sd = input()
T = input()
 
def check(A, B):
    for a, b in zip(A, B):
        if a != '?' and a != b:
            return False
 
    return True
 
for i in range(len(Sd) - len(T) + 1)[::-1]:
    if check(Sd[i:i + len(T)], T):
        print((Sd[:i] + T + Sd[i + len(T):]).replace('?', 'a'))
        break
else:
    print('UNRESTORABLE')

Submission status: Accepted

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


def isEqual(s1, s2):
    for i in range(len(s1)):
        if s1[i] != '?' and s1[i] != s2[i]:
            return False

    return True


kouhos = []
for i in range(len(S_hint)-len(T)+1):
    if isEqual(S_hint[i:i+len(T)], T):
        kouho = S_hint[0:i] + T + S_hint[i+len(T):]
        kouho = kouho.replace('?', 'a')
        kouhos.append(kouho)

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

Submission status: Accepted

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


def found(i):
    for j in range(len(T)):
        if S[i] == '?' or S[i] == T[j]:
            i += 1
        else:
            return False
    return True

ret = 'UNRESTORABLE'
for i in range(len(S) - len(T), -1, -1):
    if found(i):
        ret = S[:i] + T + S[i + len(T):]
        break
    else:
        i += 1

print(''.join('a' if x == '?' else x for x in ret))

Submission status: Accepted

Solution code:
s=input()
t=input()
ans=[]
for i in range(len(s)-len(t)+1):
  cnt=0
  for j in range(len(t)):
    if (s[i+j]==t[j]) or (s[i+j]=='?'):
      if j==len(t)-1:
        ss=s[:i]+t+s[i+j+1:]
        if ss.count('?')>0:
          ss=ss.replace('?','a')
        ans.append(ss)
      else:
        pass
    else:
      break
if ans!=[]:
  print(min(ans))
else:
  print('UNRESTORABLE')

Submission status: Accepted

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

S2 = S2.replace('?', '.')
S2 = S2[::-1]
T = T[::-1]

ans = ""
i = 0
find = False
while i < len(S2):
    if find == False and len(T) > len(S2) - i:
        break
    p = S2[i:i+len(T)]
    m = re.match(p, T)
    if m and find == False:
        find = True
        ans = ans + T
        i += len(T)
    else:
        if S2[i] == '.':
            ans = ans + 'a'
        else:
            ans = ans + S2[i]
        i += 1

if len(ans) == len(S2):
    print(ans[::-1])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
s2 = input()
t = input()
c1=1
c2=0
c3=0

for i in range(len(s2)-len(t)+1):
    if s2[i]==t[0] or s2[i]=='?':
        for j in range(1,len(t)):
            if s2[i+j]==t[j] or s2[i+j]=='?':
                c1+=1
        if c1 == len(t):
            c2=1
            c3=i
        c1=1
if c2==1:
    s=list(s2)
    for i in range(len(t)):
        s[c3+i]=t[i]
    for i in range(len(s2)):
        if s[i]=='?':
            s[i]='a'
    print("".join(s))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import copy

s = input().rstrip()
t = input().rstrip()
ns, nt = len(s), len(t)

start = -1
for i in range(ns - nt, -1, -1):
    for j in range(nt):
        if s[i+j] != t[j] and s[i+j] != '?':
            break
    else:
        start = i
        break

if start >= 0:
    s_ans = s[:start] + t + s[start+len(t):]
    print(s_ans.replace('?', 'a'))
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

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

# 一致する位置を探して返す
def search(s, t):
    head = len(s)-len(t)
    tail = len(s)
    for i in range(head+1):
        s_ = s[head:tail]
        # 一致するか確認
        for j in range(len(t)):
            if t[j] != s_[j]:
                if s_[j] != '?':
                    break
        # 正常終了した
        else:
            return head, tail
        head -= 1
        tail -= 1
        continue
    return None, None

# tの文字列分後ろからみて一致したら入れる
if len_s < len_t:
    print('UNRESTORABLE')
else:
    head, tail = search(s, t)
    if head is None:
        print('UNRESTORABLE')
    else:
        s = list(s)
        s[head:tail] = t
        s = "".join(s)
        s = s.replace('?', 'a')
        print(s)

Submission status: Accepted

Solution code:
import collections
def main():

    S = list(input())[::-1]
    T = list(input())[::-1]
    ary = []


    c = collections.Counter(S)
    if len(S) < len(T):
        print('UNRESTORABLE')
        return

    # if c['?'] == len(S):
    #     if len(S) == len(T):
    #
    #         print(''.join(T))
    #         return
    #     else:
    #         print('UNRESTORABLE')
    #         return
    else:
        for s_left in range(len(S)):
            s_l = s_left
            flg = True
            tmp = S[::1]
            for t_left in range(len(T)) :
                if s_l < len(S) and (S[s_l] == T[t_left] or S[s_l] == '?'):
                    tmp[s_l] = T[t_left]
                    s_l += 1
                    continue
                else:
                    flg = False
                    break
            if flg:
                ans_t = tmp[::-1]
                ans = ''
                for i in range(len(S)):
                    if ans_t[i] == '?':
                        ans += 'a'
                    else:
                        ans += ans_t[i]
                # print(ans)
                # return
                ary.append(ans)

        if len(ary) == 0:

            print('UNRESTORABLE')
            return
        ary.sort()
        print(ary[0])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

def main():
    S = readline().rstrip()
    T = readline().rstrip()
    N = len(S)
    M = len(T)
    for i in range(N-M, -1, -1):
        s = S[i:i+M]
        flg = True
        for e1, e2 in zip(s, T):
            if e1 != '?' and e1 != e2:
                flg = False
                break
        if flg:
            ans = list(S)
            for j in range(i, i+M):
                ans[j] = T[j-i]
            ans = ''.join(ans)
            print(ans.replace('?', 'a'))
            return

    print('UNRESTORABLE')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s2 = input()
t = input()
c1=1
c2=0
c3=0

for i in range(len(s2)-len(t)+1):
    if s2[i]==t[0] or s2[i]=='?':
        for j in range(1,len(t)):
            if s2[i+j]==t[j] or s2[i+j]=='?':
                c1+=1
        if c1 == len(t):
            c2=1
            c3=i
        c1=1
if c2==1:
    s=list(s2)
    for i in range(len(t)):
        s[c3+i]=t[i]
    for i in range(len(s2)):
        if s[i]=='?':
            s[i]='a'
    print("".join(s))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()
T = input()
same = lambda x, y: all(True if i == j or i == '?' else False for i, j in zip(x, y))
n = len(T)
for i in range(len(S)-n, -1, -1):
    if same(S[i:i+n], T):
        S = S[:i] + T + S[i+n:]
        break
else:
    print('UNRESTORABLE')
    exit()
print(S.replace('?', 'a'))

Submission status: Accepted

Solution code:
s=input()
t=input()
ans=[]
for i in range(len(s)-len(t)+1):
  start=i
  end=i+len(t)
  ok=True
  for j in range(len(t)):
    if s[start+j]!='?' and s[start+j]!=t[j]:
      ok=False
      break
  if ok:
    a=s[:start]+t+s[end:]
    ans.append(a.replace('?','a'))
if ans:
  ans=sorted(ans)
  print(ans[0])
else:
  print('UNRESTORABLE')

Submission status: Accepted

Solution code:
inf = 10**15
mod = 10**9+7
s = input()
t = input()
lenn = len(s)
lent = len(t)
ans = []
for i in range(lenn-lent+1):
    flag = True
    tmp = ['' for i in range(lenn)]
    for j,k in enumerate(range(i,i+lent)):
        if (s[k] != t[j]) and (s[k] != '?'):
            flag = False
            break
        else:
            tmp[k]=t[j]
    if flag:
        for l in range(lenn):
            if tmp[l] == '':
                if s[l] == '?':
                    tmp[l] = 'a'
                else:
                    tmp[l] = s[l]
        ans.append(''.join(tmp))
ans.sort()
if ans:
    print(ans[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys

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

length = len(T)
position = len(S) - length

while position >= 0:
    text = S[position:position+length]
    for i in range(length):
        if text[i] == "?":
            text[i] = T[i]
    if text != T:
        position -= 1
    else:
        S[position:position+length] = T
        position = -10

if position != -10:
    print("UNRESTORABLE")
    sys.exit()

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

print("".join(S))

Submission status: Accepted

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

for i in reversed(range(len(SD)-len(T)+1)):
  #print(SD[i:i+len(T)])
  for j in range(len(T)):    
    if SD[i+j]!="?" and SD[i+j]!=T[j]:
      break
  else:
    for j in range(len(T)):
      if SD[i+j]=="?":
        SD[i+j]=T[j]
    for j in range(len(SD)):
      if SD[j]=="?":
        SD[j]="a"
    print("".join(SD))
    break
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

found_s = False
for i in range(len(s) - len(t), -1, -1):
    for c1, c2 in zip(s[i:i+len(t)], t):
        if c1 == '?' or c1 == c2:
            continue
        else:
            break
    else:
        found_s = True
        s = s[:i] + t + s[i+len(t):]
        break

if found_s:
    print(s.replace('?', 'a'))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=input()
t=input()
lt=len(t)
ls=len(s)

#例外処理
if lt>ls:
    print("UNRESTORABLE")
else:
    # 後ろから見ていってｔを入れれそうならflagを立てる
    # t入れれたら残りの?を全部aに変えてしまえばいい
    ans=[]
    for i in range(ls-lt+1):
        flag=True
        for j in range(lt):
            if s[i+j]!="?" and s[i+j]!=t[j]:
                flag=False
                break                
        if flag: # countがtの長さになってたらtぶち込めるてこと
            tmp=s[:i]+t+s[i+lt:]
            tmp=tmp.replace("?", "a")
            ans.append(tmp)
            
      
    if ans:
        an=sorted(ans)
        print(an[0])
       
    else:
        print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s_dash_rev = input()[::-1]
t_rev = input()[::-1]
s_length = len(s_dash_rev)
t_length = len(t_rev)
matched = False
for begin_index in range(s_length - t_length + 1):
    end_index = begin_index + t_length
    part_s_dash_rev = s_dash_rev[begin_index:end_index]
    part_matched = True
    for i in range(t_length):
        if part_s_dash_rev[i] != '?' and part_s_dash_rev[i] != t_rev[i]:
            part_matched = False
            break
    if part_matched:
        # replace by `T`
        matched = True
        s_rev = s_dash_rev[:begin_index] + t_rev + s_dash_rev[end_index:]
        break
if not matched:
    print('UNRESTORABLE')
else:
    s = s_rev[::-1]
    print(s.replace('?', 'a'))

Submission status: Accepted

Solution code:
A = list(input())
B = list(input())
for i in reversed(range(len(A)-len(B)+1)):
    for j in reversed(range(len(B))):
        if A[j+i] == B[j] or A[j+i] == "?":
            pass
        else:
            j +=1
            break
    if j == 0:
        A[i:i+len(B)] = B
        A = "".join(A)
        A = A.replace("?","a")
        print(A)
        exit()
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

def main():
    s = list(input())
    t = list(input())
    n = len(s)
    m = len(t)
    ans = ''
    for i in range(n - m,-1,-1):
        flag = True
        for j in range(m):
            if s[i + j] != t[j] and s[i + j] != '?':
                flag = False
                break
        if flag:
            ans = s[:i] + t + s[i + m:]
            break
    if ans:
        for i in range(n):
            if ans[i] == '?':
                ans[i] = 'a'
        print(''.join(ans))
    else:
        print('UNRESTORABLE')
if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans_ls = []
for start in range(len_s - len_t + 1):
    Pass = True
    for i in range(len_t):
        if s[start+i] != '?':
            if s[start+i] != t[i]:
                Pass = False
    if Pass:
        including = s[:start] + t + s[start + len_t:]
        replaced = including.replace('?', 'a')
        ans_ls.append(replaced)
ans_ls.sort()
if ans_ls:  
    print(ans_ls[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

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

for i in reversed(range(n - m + 1)):
    t_kamo = sd[i:i+m]
    for j in range(len(t)):
        if t_kamo[j] == "?":
            continue
        elif t_kamo[j] == t[j]:
            continue
        else:
            break
    else:
        ans = (sd[:i] + t + sd[i+m:]).replace("?", "a")
        print(ans)
        exit()

print("UNRESTORABLE")

Submission status: Accepted

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

print(ans if ans!='' else 'UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()[::-1]
T = input()[::-1]
n = len(S);m = len(T)
a = []
for i in range(0, n - m + 1):
    f = 1
    s = S[i:i+m]
    for j in range(m):
        if s[j] not in ["?",T[j]]:
            f = 0
            break
    if f:
        a = S[:i] + T + S[i+m:]
        break

if a:
    print(a.replace('?','a')[::-1])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc076/tasks/abc076_c
s = input()
t = input()

ans = []
for i in range(1, len(s) + 1):
    l = s[:i - 1]
    r = s[i + len(t) - 1:]
    temp = l + t + r
    if len(temp) == len(s):
        for i in range(len(s)):
            if s[i] == '?' or s[i] == temp[i]:
                continue
            else:
                break
        else:
            ans.append(temp)
if len(ans) == 0:
    print('UNRESTORABLE')
else:
    ans.sort()
    a = ans[0]
    a = a.replace('?', 'a')
    print(a)

Submission status: Accepted

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

Submission status: Accepted

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

st = []

for i,c in enumerate(s):
    if i + len(t) > len(s):
        break
    if c == t[0] or c == '?':
        isok = True
        for j, c2 in enumerate(t):
            if s[i+j] == c2 or s[i+j] == '?':
                continue
            else:
                isok = False
        if isok:
            st.append((s[:i] + t + s[i+len(t):]).replace('?', 'a'))
st.sort()
print('UNRESTORABLE' if len(st)==0 else st[0])

Submission status: Accepted

Solution code:
S = input()
T = input()
Ns = len(S)
Nt = len(T)
if Ns<Nt:
   print("UNRESTORABLE")
else:
   S_merge=""
   isok = True
   for i in range(Ns-Nt,-1,-1):
      isok = True
      # print(S[i:i+Nt], T)
      for j in range(Nt):
         if not (S[i+j] == T[j] or S[i+j]=="?"):
            isok = False
            break
      if isok:
         # if i > 0:
         S_merge = S[:i]+T+S[i+Nt:]
         # else:
         #    S_merge = T
         break
   if not isok:
      print("UNRESTORABLE")
   else:
      # print(S_merge)
      S_ans =""
      for i in range(Ns):
         if S_merge[i]=="?":
            S_ans = S_ans+'a'
         else:
            S_ans = S_ans+S_merge[i]
      print(S_ans)

Submission status: Accepted

Solution code:
import sys
S=input()
T=input()
sl=len(S)
tl=len(T)
init=(['z']*sl)
init=''.join(init)
ans=init
for i in range(sl-tl+1):
    st=S[i:i+tl]
    flag=False
    for j in range(tl):
        if st[j]==T[j] or st[j]=='?':
            flag=True
        else:
            flag=False
            break
    if flag:
        anst=list(S)
        anst[i:i+tl]=T[:]
        anst=''.join(anst)
        anst=anst.replace('?','a')
        if anst<ans:
            ans=anst
if ans == init:
    print('UNRESTORABLE')
else:   
    print(ans)

Submission status: Accepted

Solution code:
s = input()
t = input()
tlen = len(t)
anslist = []
n = len(s)
'''
当てはまる文字列を全列挙してソート
'''
def mojiretu(a):
    global anslist
    a = list(a)
    for i in range(len(a)):
        if a[i] == '?':
            a[i] = 'a'
    anslist.append(''.join(a))

def hantei(ss):
    for i in range(len(ss)):
        if ss[i] != t[i] and ss[i] != '?':
            return False
    else:return True

for i in range(n+1-tlen):
    if s[i] == t[0] or s[i] == '?':
        if hantei(s[i:i+tlen]):
            mojiretu(s[:i] + t + s[i+tlen:])
anslist.sort()
if anslist:
    print(anslist[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
def resolve():
    ss = input()
    t = input()
    ans = 'UNRESTORABLE'

    # マッチ点が複数ある場合は、辞書順最小にしなければならないため、なるべく後ろのマッチ点でマッチさせたい。
    # そのため、ループは逆順で回す必要がある。
    for i in reversed(range(len(ss) - len(t) + 1)):
        flg = True
        for j in range(len(t)):
            if ss[i + j] != t[j] and ss[i + j] != '?':
                flg = False
                break

        if flg:
            ans = ss[:i] + t + ss[i + len(t):]
            ans = ans.replace('?', 'a')
            break

    print(ans)
    
resolve()

Submission status: Accepted

Solution code:
def resolve():

    # 辞書順最小のものを選びたいので、文字列は反転して判定を行っている
    # マッチする箇所が複数あった場合は、なるべく後ろのマッチ点でマッチさせて、前のマッチ点はすべて?をaに変更する
    ss = input()[::-1]
    t = input()[::-1]

    ans = 'UNRESTORABLE'
    for i in range(len(ss) - len(t) + 1):
        flg = True
        for j in range(len(t)):
            if ss[i + j] != t[j] and ss[i + j] != '?':
                flg = False
                break

        if flg:
            ans = ss[:i] + t + ss[i + len(t):]
            ans = ans.replace('?', 'a')[::-1]
            break

    print(ans)
    
resolve()

Submission status: Accepted

Solution code:
sd=list(input())
t=list(input())
#strsd="?tc????"
#strt="coder"

#strsd="??p??d??"
#strt="abc"

#strsd="ab??p??c"
#strt="abc"

#strsd="ab??p??c"
#strt="abcapcadc"

#sd=list(strsd)
#t=list(strt)

lens=len(sd)
lent=len(t)
yn="NG"
for i in range(lens-lent,-1,-1):
    yn=""
    for ii in range(lent):
        if sd[i+ii]==t[ii] or sd[i+ii]=="?":
            continue
        else:
            yn="NG"
            break
    if yn!="NG":
        yn="itchi"
        break
    
if yn=="NG":
    print("UNRESTORABLE")
else:
    s=""
    if i!=0:
        for ii in range(i):
            if sd[ii]=="?":
                si="a"
            else:
                si=sd[ii]
            s=s+si
    for ti in t:
        s=s+ti
    if i!=lens-lent:
        for ii in range(i+lent,lens,1):
            if sd[ii]=="?":
                si="a"
            else:
                si=sd[ii]
            s=s+si
    print(s)

Submission status: Accepted

Solution code:
import copy
s = input()
t = input()
for i in range(len(s)-len(t)+1):
  s1 = copy.deepcopy(s[len(s)-len(t)-i:len(s)-i])
  for j in range(len(t)):
    if t[j]==s1[j]:
      continue
    elif s1[j]=="?":
      s2 = list(s1)
      s2[j]=t[j]
      s1 = "".join(s2)
    else:
      break
  else:
    s3 = s[:len(s)-len(t)-i].replace("?","a")+s1+s[len(s)-i:].replace("?","a")
    print(s3)
    exit()
print("UNRESTORABLE")