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
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=ST()
    T=ST()
    N=len(S)
    M=len(T)
    ans = []
    for i in range(N-M+1)[::-1]:
        can = True
        for s,t in zip(S[i:],T):
            if s!=t and s!="?":
                can = False
        if can:
            for j,s in enumerate(S):
                if j<i:
                    if s=="?":
                        ans.append("a")
                    else:
                        ans.append(s)
                else:
                    if len(T)>j-i:
                        ans.append(T[j-i])
                    else:
                        if s=="?":
                            ans.append("a")
                        else:
                            ans.append(s)
            break
    if ans:
        print(*ans,sep="")
    else:
        print("UNRESTORABLE")
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S_=input()
T=input()
ns=len(S_)
nt=len(T)
cand=[]

def f(x, y):
  n=len(x)
  out=True
  for i in range(n):
    if x[i]!=y[i] and x[i]!='?':
      out=False
  return out

for i in range(ns-nt+1):
  if f(S_[i:i+nt], T)==True:
    temp=''
    for j in range(i):
      if S_[j]=='?':
        temp+='a'
      else:
        temp+=S_[j]
    temp+=T
    for j in range(i+nt, ns):
      if S_[j]=='?':
        temp+='a'
      else:
        temp+=S_[j]
        
    cand.append(temp)
    
cand.sort()
cand.append('UNRESTORABLE')
print(cand[0])

Submission status: Accepted

Solution code:
def solve():
    S = list(input())
    ans = S[:]
    T = input()
    Sn = len(S)
    Tn = len(T)
    
    restorable = False
    for i in range(Sn - Tn, -1, -1):
        flag = True
        if S[i] == T[0] or S[i] == '?':
            for j in range(Tn):
                if S[i+j] != T[j] and S[i+j] != '?':
                    flag = False
                    break
            if flag:
                restorable = True
                for j in range(Tn):
                    ans[i+j] = T[j]
                break
    
    if not restorable:
        print('UNRESTORABLE')
    else:
        for i in range(Sn):
            if ans[i] == '?':
                ans[i] = 'a'
        print(''.join(ans))


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

S = input()
T = input()
N = len(S)
M = len(T)

def check(S, T):
    N = len(S)
    for i in range(N):
        if S[i] != '?' and S[i] != T[i]:
            return False
    return True

# 前の方はなるべくaで埋めたいので、後ろからTとの一致を探す
ans = [''] * N
for i in range(N-M, -1, -1):
    # 当てはまる部分列を見つけたら、答えに追記してbreak
    if check(S[i:i+M], T):
        for j in range(i, i+M):
            ans[j] = T[j-i]
        break
else:
    # 見つからなければNG
    print('UNRESTORABLE')
    exit()

for i in range(N):
    if S[i] != '?':
        ans[i] = S[i]
    # 辞書順最小にしたいので、余っている?はaで埋める
    elif ans[i] == '':
        ans[i] = 'a'
print(''.join(ans))

Submission status: Accepted

Solution code:

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = False
show_flg = True


def main():
    S = input()
    T = input()
    canditates = []

    for i in range(len(S)):
        idx = 0
        for j in range(i, len(S)):
            if idx < len(T) and (S[j] == T[idx] or S[j] == '?'):
                idx += 1
            else:
                break

        # print(idx, S, len(T))
        if idx == len(T):
            canditates.append(S[:i] + T + S[i+len(T):])
            # print(S[:i], T, S[i+len(T):])
            # print(i, i+len(T))

    if len(canditates) > 0:
        # print(canditates)
        canditates = [c.replace("?", "a") for c in canditates]
        canditates.sort()
        print(canditates[0])
    else:
        print("UNRESTORABLE")

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    s = input()
    t = input()
    T = len(t)
    candidate = []
    for i in range(len(s)-T+1):
        buf = s[i:i+T]
        flag = True
        for b, tc in zip(buf, t):
            if b == "?" or b == tc:
                continue
            else:
                flag = False
                break
        if flag == True:
            candidate.append(i)
    if len(candidate) == 0:
        print("UNRESTORABLE")
    else:
        print((s[:candidate[-1]] + t + s[candidate[-1]+T:]).replace("?", "a"))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = "UNRESTORABLE"

i = 0

while i < len(s)-len(t)+1:
    if s[i] == t[0] or s[i] == "?":
        flag = True
        for j in range(1,len(t)):
            if s[i+j] == "?" or s[i+j] == t[j]:
                continue
            else:
                flag = False
                break
        if flag:
            tmps = ""
            k = 0
            while k < len(s):
                if k < i or k >= i+len(t):
                    if s[k] == "?":
                        tmps += "a"
                        k += 1
                    else:
                        tmps += s[k]
                        k += 1
                else:
                    tmps += t
                    k += len(t)
            ans = tmps
    i += 1

print(ans)

Submission status: Accepted

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

def compaierStr(expected: list, actual: list) -> bool:
    for e, a in zip(expected, actual):
        if e != a and a != "?":
            return False

    return True

candidates = []
for i in range(len(sd)-len(t)+1):
    if compaierStr(t, sd[i:i+len(t)]):
        candidates.append((sd[:i] + t + sd[i+len(t):]).replace("?", "a"))

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

Submission status: Accepted

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

def compaierStr(expected: list, actual: list) -> bool:
    for e, a in zip(expected, actual):
        if e != a and a != "?":
            return False

    return True

candidates = []
for i in range(len(sd)-len(t)+1):
    if compaierStr(t, sd[i:i+len(t)]):
        candidates.append((sd[:i] + t + sd[i+len(t):]).replace("?", "a"))

if len(candidates) == 0:
    print("UNRESTORABLE")
else:
    ans = "z"*(len(sd))
    for _c in candidates:
        if _c < ans:
            ans = _c

    print(ans)

Submission status: Accepted

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

def compaierStr(expected: list, actual: list) -> bool:
    for e, a in zip(expected, actual):
        if e != a and a != "?":
            return False

    return True

candidates = []
for i in range(len(sd)-len(t)+1):
    if compaierStr(t, sd[i:i+len(t)]):
        candidates.append((sd[:i] + t + sd[i+len(t):]).replace("?", "a"))

if len(candidates) == 0:
    print("UNRESTORABLE")
else:
    ans = "z"
    for _c in candidates:
        if _c < ans:
            ans = _c

    print(ans)

Submission status: Accepted

Solution code:
import sys
import re



def check(Sda, T, Slen, Tlen):
    matarr=[]
    endarr=[]

    getS = 'UNRESTORABLE'

    if(Slen<Tlen):
        return getS

    for i in reversed(range(0, Slen-Tlen+1)):


        mat = re.match(Sda[i:i+Tlen],T)
        try:
            if(mat.group() == T):
                matarr.append(Sda[:i]+T+Sda[i+Tlen:])

        except AttributeError:
            pass

        mat = 'None'

    if(matarr != []):
        for x in matarr:
            x = x.replace('.', 'a')
            endarr.append(x)

        getS = min(endarr)

    return getS



def main():
    Sda = input()
    T = input()
    Slen = len(Sda)
    Tlen = len(T)

    Sda = Sda.replace('?', '.')

    print(check(Sda, T, Slen, Tlen))


main()

Submission status: Accepted

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

n = len(sd)
m = len(t)
s = []

for i in range(n-m, -1, -1):  # n-mするとm字分だけちょうど文字列取れる
    t_kamo = sd[i:i + m]
    for j in range(m + 1):
        # 1文字ずつ順に入りうるか調べる
        if j == m:  # 最後までいったら"?"を"a"に置き換えて出力
            print((sd[:i] + t + sd[i+len(t):]).replace("?", "a"))
            exit()
        if t_kamo[j] == "?":
            continue
        elif t_kamo[j] != t[j]:  # 文字列t_kamoと文字列tの同じ位置の文字が違うならbreak
            break

# 全部の文字列t_kamoにおいて最後まで文字列tが入らなかったらUNRESTORABLEを出力する
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
import sys

s_prime, t = sys.stdin.read().split()
ls = len(s_prime)
lt = len(t)

def main():
    for i in range(ls-lt, -1, -1):
        t_prime = s_prime[i:i+lt]
        for j in range(lt):
            if not t_prime[j] in ['?', t[j]]:
                break
        else:
            s = (s_prime[:i] + t + s_prime[i+lt:]).replace('?', 'a')
            return s
    return 'UNRESTORABLE'
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
import sys

s_prime, t = sys.stdin.read().split()
s = list(s_prime)
ls = len(s)
lt = len(t)

def main():
    ng = 'UNRESTORABLE'

    if ls < lt:
        return ng

    for i in range(ls-lt, -1, -1):
        t_prime = s[i:i+lt]
        for j in range(lt):
            letter = t_prime[j]
            if (letter != '?') and (letter != t[j]):
                break
        else:
            for j in range(lt):
                s[i+j] = t[j]
            break
    else:
        return ng
    
    ans = ''.join(s).replace('?', 'a')
    return ans
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
s,t = input(), input()
ls,lt = len(s), len(t)
for i in range(ls-lt,-1,-1):
  for j,s_ in enumerate(s[i:i+lt]):
#    print(i,s_)
    if (s_ == '?') | (s_ == t[j]):
      pass
    else:
      break
  else:
    s = s[:i] + t + s[i+lt:]
    s = s.replace('?','a')
    print(s)
    break
else:
  print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S=input();T=input();
M=-1;
for i in range(len(S)-len(T)+1):
  t=True;
  for j in range(len(T)):
    t*=(S[i+j]==T[j] or S[i+j]=='?');
  if(t):
    M=i;
if(M==-1):
  print("UNRESTORABLE");
  exit();
print((S[:M]+T+S[M+len(T):]).replace("?","a"));

Submission status: Accepted

Solution code:
s=input()
t=input()
ls=len(s)
lt=len(t)
if ls<lt:
    print('UNRESTORABLE')
    exit()
n=ls-lt+1
ans=[]
for i in range(n):
    tt='?'*i+t+'?'*(n-i-1)
    a=''
    for j in range(ls):
        if tt[j]!='?':
            if tt[j]!=s[j] and s[j]!='?':
                break
            a+=tt[j]
        else:
            if s[j]=='?':
                a+='a'
            else:
                a+=s[j]
    else:
        ans.append(a)
if ans:
    ans=sorted(ans)
    print(ans[0])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
S = input()
T = input()
s, t = len(S), len(T)
p = 'UNRESTORABLE'
n = -1
if t <= s:
  for i in range(s-t+1):
    f = 1
    for j in range(t):
      if S[i+j] != '?' and S[i+j] != T[j]:
        f = -1
    if f == 1:
      n = i
  if n >= 0:
    p = ''
    for k in range(s):
      if k < n or k > n + t - 1:
        if S[k] == '?':
          p = p[:] + 'a'
        else:
          p = p[:] + S[k]
      else:
        p = p[:] + T[k - n]
print(p)

Submission status: Accepted

Solution code:
s = input()
t = input()
result = ''
length = len(s)-len(t)
while length>=0:
  skip = False
  for i in range(len(t)):
    if s[length+i]!='?' and s[length+i]!=t[i]:
      skip = True
      break
  if skip==False:
    break
  length-=1
    
if length<0:
  print("UNRESTORABLE")
else:
  i = 0
  while i<len(s):
    if i==length:
      result+=t
      i+=len(t)-1
    else:
      if s[i]=='?':
        result+='a'
      else:
        result+=s[i]
    i+=1
  print(result)

Submission status: Accepted

Solution code:
s=list(input())
t=list(input())
ls=len(s)
lt=len(t)
if ls<lt:
  print("UNRESTORABLE")
  exit()
for i in range(ls-lt,-1,-1):
  for j in range(i,i+lt):
    if s[j]==t[j-i] or s[j]=="?":
      continue
    else:
      break
  else:
    for k in range(i,i+lt):
      s[k]=t[k-i]
    for l in range(ls):
      if s[l]=="?":
        s[l]="a"
    print(*s,sep="")
    exit()
else:
  print("UNRESTORABLE")

Submission status: Accepted

Solution code:
def getLastMatch(s,t):
    for i in range(len(s)-len(t), -1, -1):
        if s[i] == '?' or s[i] == t[0]:
            match = True
            for j in range(len(t)):
                if s[i+j] == '?': continue
                if s[i+j] != t[j]:
                    match = False
                    break
            if match: return i
    return -1

def getS(s,t,lastMatch):
    s = list(s)
    for i in range(len(t)):
        s[i+lastMatch] = t[i]
    for i in range(len(s)):
        if s[i]=='?': s[i] = 'a'
    res = ''
    for c in s: res+=c
    return res


s,t = input(), input()
lastMatch = getLastMatch(s,t)
if lastMatch == -1:
    print('UNRESTORABLE')
else:
    print(getS(s,t,lastMatch))

Submission status: Accepted

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

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

match = -1

for i in range((len_s - len_t), -1, -1):
    for j in range(len_t):
        if T[j] != S[i+j] and S[i+j] != "?":
            break

        if j == len_t-1:
            match = i

    if match != -1:
        break

if match == -1:
    print("UNRESTORABLE")
    exit()

list_s = list(S)

for i in range(match, match+len_t):
    list_s[i] = T[i - match]

list_s = [x if x != "?" else "a" for x in list_s]

print("".join(list_s))

Submission status: Accepted

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

now = 0

flag2 = False

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

S = ''
for i in S_:
    if i == '?':
        S += 'a'
    else:
        S += i

if flag2 == True:
    print(S)
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

n = len(sd)
m = len(t)
s = []

for i in range(n - m, -1, -1):
    t_kamo = sd[i:i + m]

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

print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s_dash = input()
t = input()
match_index = []
candidate = []
for i in range(len(s_dash) - len(t) + 1):
    for j in range(len(t)):
        if not(t[j] == s_dash[i + j] or s_dash[i + j] == '?'):
            break
        if j == len(t) - 1:
            match_index.append(i)
for index in match_index:
    s = s_dash[:index] + t
    if index != len(s_dash) - len(t):
        s += s_dash[index + len(t):]
    s = list(s)
    for i in range(len(s)):
        if s[i] == '?':
            s[i] = 'a'
    s = "".join(s)
    candidate.append(s)
candidate.sort()
if len(candidate) == 0:
    print('UNRESTORABLE')
else:
    print(candidate[0])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    s = str(readline().rstrip().decode('utf-8'))
    t = str(readline().rstrip().decode('utf-8'))
    lt = len(t)
    ans = []
    for i in range(len(s) - len(t), -1, -1):
        ts = s[i:i+lt]
        b = True
        for j in range(lt):
            if ts[j] != "?" and ts[j] != t[j]:
                b = False
                break
        if b:
            for j in range(len(s)):
                if i <= j < i + lt:
                    ans.append(t[j-i])
                else:
                    ans.append(s[j] if s[j] != "?" else "a")
            print("".join(ans))
            exit()
    print("UNRESTORABLE")


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

def strmap(s, c_from='?', c_to='a'):
    return "".join(map(lambda c: c_to if c == c_from else c, s))

keys = []
for first in range(M - N + 1):
    match = True
    for i in range(N):
        if S[first + i] != '?' and S[first + i] != T[i]:
            match = False
            break
    if match:
        keys.append(strmap(S[: first]) + T + strmap(S[first + N:]))

if keys:
    print(min(keys))
else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
from copy import deepcopy

def judge_a(S,T,i,j):
    return S[i+j]==T[j]

def judge_b(S,T,i,j):
    return S[i+j]!=T[j] and S[i+j]=="?"

def ans(ans_list):
    if not ans_list:
        print("UNRESTORABLE")
    else:
        ans_list_2=[]
        for l in ans_list:
            temp=[s if s!="?" else "a" for s in l]
            ans_list_2.append("".join(temp))
        print(min(ans_list_2))

if __name__ == '__main__':
    S_dash=list(input())
    T=list(input())
    N_S=len(S_dash)
    T_N=len(T)
    ans_list=[]

    for i in range(N_S-T_N+1):
        S_2=deepcopy(S_dash)
        flag=True
        for j in range(T_N):
            if judge_a(S_2,T,i,j) or judge_b(S_2,T,i,j):
                S_2[i+j]=T[j]
            else:
                flag=False
                break
        if flag:
            ans_list.append(S_2)
    ans(ans_list)

Submission status: Accepted

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

ans = "UNRESTORABLE"

ls = []

for i in range(len(t)):
    for j in range(i, len(s)-len(t)+i+1):
        if s[j] == t[i]:
            for k in range(len(t)):
                if t[k] != s[j+k-i] and s[j+k-i] != "?":
                    break
                if k == len(t)-1:
                    tmps = s[:j-i] + t + s[j-i+len(t):]
                    for p in range(len(s)):
                        if tmps[p] == "?":
                            tmps = tmps[:p] + "a" + tmps[p+1:]
                    ls.append(tmps)

for i in range(len(s)-len(t)+1):
    if s[i] == "?":
        if s[i:i+len(t)] == "?"*len(t):
            tmps = s[:i] + t + s[i+len(t):]
            for p in range(len(s)):
                if tmps[p] == "?":
                    tmps = tmps[:p] + "a" + tmps[p+1:]
            ls.append(tmps)

ls.sort()
if len(ls) != 0:
    ans = ls[0]

print(ans)

Submission status: Accepted

Solution code:
import re

s = str(input())
t = str(input())


#Sの?を正規表現　英文字1つに変換する
#tがsのどこに含まれるかどうかを、開始位置を一文字ずつずらしながら調査
#最初に見つかった場所を対象にtを代入
#残りの文字は'a'に変換
#リストに格納
#sortして最初に来たものがanswer
#リストがからの場合は UNRESTORABLE

NS = []

for i in range(len(s)):
    #対象外のものは?をaに変換
    s_res = s[:i]
    s_res = s_res.replace('?', 'a')
    
    #対象外のものは?を.に変換
    #探索範囲は探る文字列tと同じ長さ
    s_obj = s[i:i+len(t)]
    s_obj = s_obj.replace('?', '.')
    s_comp = re.compile(s_obj)

    s_res_f = s[i+len(t):]
 
    matchobj = s_comp.fullmatch(t)

    if matchobj:
        start = matchobj.start()
        new_s = s_res + t + s_res_f
        new_s = new_s.replace('?', 'a')
        NS.append(new_s)

if len(NS) == 0:
    ans = 'UNRESTORABLE'
else:
    ans = sorted(NS)[0]

print(ans)

Submission status: Accepted

Solution code:


S=input()
T=input()

def match(S,T):
    for i in range(len(T)):
        if S[i]==T[i] or S[i]=='?':
            continue
        else:
            return False
    return True

if len(T)>len(S):
    print('UNRESTORABLE')
    exit()

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

Submission status: Accepted

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

ans = []
for i in range(S - T + 1):
    tmp = list(s)
    cnt = 0
    for j in range(T):
        if s[i + j] == t[j] or s[i + j] == "?":
            cnt += 1
            tmp[i + j] = t[j]
        if cnt == T:
            ans.append("".join(tmp).replace("?", "a"))

if len(ans) > 0:
    print(min(ans))
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

ans = []
for i in range(S):
    if S - i >= T:
        tmp = list(s)
        cnt = 0
        for j in range(T):
            if s[i + j] == t[j] or s[i + j] == "?":
                cnt += 1
                tmp[i + j] = t[j]
            if cnt == T:
                ans.append("".join(tmp).replace("?", "a"))

if len(ans) > 0:
    print(min(ans))
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

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

if len_S < len_T:
    print("UNRESTORABLE")
    quit()

candidates = tuple((S[:i] + T + S[i + len_T:]).replace("?", "a") for i in range(len_S - len_T + 1) if all(s in ("?", t) for s, t in zip(S[i:], T)))

print(min(candidates) if candidates else "UNRESTORABLE")

Submission status: Accepted

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

last = -1
for i in range(len(s) - len(t)+1):
    for a,b in zip(s[i:i+len(t)], t):
        if a == "?":
            continue
        if a != b:
            break
    else:
        last = i

if last == -1:
    print("UNRESTORABLE")
    import sys
    sys.exit()


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

for i in range(len(t)):
    ans[i+last] = t[i]

print("".join(ans))

Submission status: Accepted

Solution code:
ss, t = input(), input()
lt = len(t)
lss = len(ss)
lst = []
for i in range(lss - lt + 1):
    tss = ss[i:i + lt]
    tb = True
    for a, b in zip(tss, t):
        if a == '?':
            continue
        elif a != b:
            tb = False
            break
    if tb:
        lst.append(ss[:i] + t + ss[i + lt:])
if len(lst):
    lst.sort()
    print(lst[0].replace('?', 'a'))
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
ss, t = input(), input()
lt = len(t)
lss = len(ss)
lst = []
for i in range(lss - 1, lt - 2, -1):
    tss = ss[i - lt + 1:i + 1]
    tb = True
    for a, b in zip(tss, t):
        if a == '?':
            continue
        elif a != b:
            tb = False
            break
    if tb:
        lst.append(ss[:i - lt + 1] + t + ss[i + 1:])
if len(lst):
    lst.sort()
    print(lst[0].replace('?', 'a'))
else:
    print('UNRESTORABLE')

Submission status: Accepted

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

ANS=[]
for i in range(n-m+1):
    for j in range(m):
        #print(s[i+j],t[j])
        if s[i+j] != "?" and s[i+j] != t[j]:
            break
    else:
        ans = list(s)
        ans[i:i+j+1] = list(t)
        ans="".join(ans)
        ans = ans.replace('?','a')
        ANS.append(ans)
if len(ANS) == 0:
    print("UNRESTORABLE")
else:
    ANS.sort()
    print(ANS[0])

Submission status: Accepted

Solution code:
import re


S_ = input()
T = input()

S_ = S_[::-1]
T = T[::-1]

t = ''
for c in T:
    t += '[?' + c + ']'
regex = re.compile(t)

S, cnt = regex.subn(T, S_, count=1)
if cnt:
    S = S.replace('?', 'a')
    print(S[::-1])
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys
s = input()[::-1]
t = input()[::-1]
ans = 'UNRESTORABLE'
if len(t)>len(s):
  print(ans)
  sys.exit()
for i in range(len(s)-len(t)+1):
  flag = True
  for j in range(len(t)):
    if t[j] != s[i+j] and s[i+j] != '?':
      flag = False
  if flag:
    for j in range(len(t)):
      ans = s[:i]+t+s[i+len(t):]
      ans = ans.replace('?','a')
      ans = ans[::-1]
      print(ans)
      sys.exit()
print(ans)

Submission status: Accepted

Solution code:
S = list(input())
lenS = len(S)
T = list(input())
lenT = len(T)
Flag = False
for i in range(lenS - lenT, -1 , -1):
    for j in range(lenT - 1, -1 , -1):
        if S[i + j] != "?" and T[j] != S[i + j]:
            break
    else:
        Flag = True
        for j in range(lenT - 1 , -1 , -1):
            S[i + j] = T[j]
        break
if Flag:
    for i in range(lenS):
        if S[i] == "?":
            S[i] = "a"
    print("".join(S))
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

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

pw = []
for i in range(len(S)-len(T)+1):
    if test(i):
        x = SA[:i] + T + SA[i+len(T):]
        pw.append(x)
if pw:
    answer = sorted(pw)[0]
else:
    answer = 'UNRESTORABLE'
print(answer)

Submission status: Accepted

Solution code:
def ABC076C_DubiousDocument2():
    Sd = list(input().strip())
    T = list(input().strip())

    tlen = len(T)
    slen = len(Sd)

    ans = 'UNRESTORABLE'
    nowPos = 0
    for pos in range(slen - 1, -1, -1):
        if pos < (tlen - 1):
            break
        if(T[tlen - 1] == Sd[pos]) or (Sd[pos] == '?'):
            nowPos = pos
            match = True
            for chk in range(nowPos, nowPos - tlen, -1):
                if(T[tlen - (nowPos - chk) - 1] != Sd[chk]) and (Sd[chk] != '?'):
                    match = False
                    break

            if match:
                # 書き換え
                for chk in range(nowPos, nowPos - tlen, -1):
                    Sd[chk] = T[tlen - (nowPos - chk) - 1]
                for i in range(0, slen):
                    if Sd[i] == '?':
                        Sd[i] = 'a'
                # 答えはこれ
                ans = ''.join(Sd)

    print(ans)

def main():
    ABC076C_DubiousDocument2()

if __name__ == "__main__":
    main()

Submission status: Accepted

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


def match(x, y):
   #print(x,y)
    return all(x[i] == y[i] or y[i] == '?' for i in range(N))


for i in range(len(T)-N+1):
    if match(S, T[-N - i: -i] if i else T[-N:]):
        if i==0:
            print ((T[:-N]+S).replace('?','a'))
            exit(0)
        print((T[:-N-i]+S+T[-i:]).replace('?','a'))
        exit(0)
print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
s_l = len(S)
t_l = len(T)
t=list(T)
ans = 'UNRESTORABLE'
for i in range(s_l-t_l+1):
  s = list(S[i:i+t_l])
  count=0
  for a,b in zip(s,t):
    if (a==b)or(a=='?'):
      count+=1
      if count==t_l:
        ans = S[:i]+T+S[i+t_l:]
        
    else:break
        
print(ans.replace('?','a'))

Submission status: Accepted

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

    rs = S.replace("?","a")

    candidate = None

    for i in range(len(S)-len(T)+1):
        ts = S[i:i+len(T)]
        #print("checking:",ts)
        is_mismatched = False
        for ii in range(len(T)):
            if ts[ii] != "?" and ts[ii] != T[ii]:
                is_mismatched = True
                break
        if not is_mismatched:
            candidate = rs[:i] + T + rs[i+len(T):]
           
    return candidate if candidate else "UNRESTORABLE"

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:

S = input()
T = input()

ans = "UNRESTORABLE"
for i in range(len(S)-len(T)+1):
    cnt = 0
    for j in range(len(T)):
        if S[i+j] == "?" or S[i+j] == T[j]:
            cnt += 1
        else:
            break

        if cnt == len(T):
            # Sのi~i+jまでがTと一致させられる
            ans = S[:i] + T + S[i+len(T):]
            ans = ans.replace("?", "a")
            # 複数箇所でTを入れられる場合、後ろに入れたいのでSの最後まで見ていく
            
        
print(ans)