Problem:


Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

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

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

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

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

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

Print the string S.

If such a string does not exist, print UNRESTORABLE.  

Constraints

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

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

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

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

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

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

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



Sample input:
?tc????
coder


Sample output:
atcoder

Submission status: Accepted

Solution code:
s = list(input()[::-1])
t = list(input()[::-1])

for shift in range(len(s) - len(t) + 1):
  for i in range(len(t)):
    if s[i + shift] != "?" and s[i + shift] != t[i]:
      break
  else:
    s[shift:shift+len(t)] = t
    print("".join(s[::-1]).replace("?", "a"))
    break
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

fin=[]

flag=0
for i in range(len(s)-len(t)+1):
    if(s[i]==t[0] or s[i]=='?'):
        ind=0
        f=1
        for j in range(i,i+len(t)):
            if(s[j]!='?' and s[j]!=t[ind]):
                f=0
                break
            ind+=1
        ind=0
        temp = s[:]
        if(f==1):
            flag=1
            for j in range(i,i+len(t)):
                if(s[j]=='?'):
                    temp[j]=t[ind]
                ind+=1
            fin.append(temp)
if(flag==0):
    print('UNRESTORABLE')
else:
    for i in range(len(fin)):
        for j in range(len(fin[i])):
            if(fin[i][j]=='?'):
                fin[i][j]='a'
    fin.sort()
    print(''.join(fin[0]))

Submission status: Accepted

Solution code:
s = list(input())
t = list(input())
f = -1
for i in range(len(s)-len(t)+1):
  for j,k,l in zip(s[i:i+len(t)],t,range(len(t))):
    if j != k and j != '?':
      break
    if l == len(t) -1:
      f = i
if f != -1:
  s[f:f+len(t)] = t
  for i in range(len(s)):
    if s[i] == '?':
      s[i] = 'a'
  print(*s,sep='')
else:
  print('UNRESTORABLE')

Submission status: Accepted

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

L = []
for i in range(len(S)-len(T)+1):
    cnt = 0
    for j in range(len(T)):
        if T[j] == S[i+j] or S[i+j] == "?":
            cnt += 1
    if cnt == len(T):
        temp = []
        for j in range(len(S)):
            if i <= j < i+len(T):
                temp.append(T[j-i])
            elif S[j] == "?":
                temp.append("a")
            else:
                temp.append(S[j])
        L.append("".join(temp))
        
if L:
    L.sort()
    print(L[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

n = len(s)
m = len(t)
if n < m:
  print("UNRESTORABLE")
  exit()
x = -1
for i in range(n-m+1):
  for j in range(m):
    if s[n-j-i-1] == "?" or s[n-j-i-1] == t[m-j-1]:
      continue
      #print("{} {}".format(i,j))
    else:
      #print("break1")
      #print(j)
      #print(s[n-j-i])
      #print(t[m-j-1])
      break
  else:
    x = i
    #print("break2")
    break
#print(x)
if x == -1:
  print("UNRESTORABLE")
else:
  ans = s[:n-m-x]+t+s[n-x:]
  #print(ans)
  for i in range(n):
    if ans[i] == "?":
      ans = ans[:i] + "a" + ans[i+1:]
  print(ans)

Submission status: Accepted

Solution code:
def is_matched(i, inputS, T):
  for s,t in zip(inputS, T):
    if s == '?': continue
    if s != t: return False
  return True

S, T = input(), input()
lenS, lenT = len(S), len(T)
matched_i = None
for i in range(lenS-lenT+1):
  if is_matched(i, S[i:i+lenT], T):
    matched_i = i
if matched_i is None: print('UNRESTORABLE')
else: print(''.join([s for s in S[:matched_i] + T + S[matched_i+lenT:]]).replace('?', 'a'))

Submission status: Accepted

Solution code:
def is_matched(i, inputS, T):
  for s,t in zip(inputS, T):
    if s == '?': continue
    if s != t: return False
  return True

S, T = input(), input()
lenS, lenT = len(S), len(T)
matched_i = None
ans = []
for i in range(lenS-lenT+1):
  if is_matched(i, S[i:i+lenT], T):
    tmpS = S.replace('?', 'a')
    ans.append(''.join([s for s in S[:i].replace('?', 'a') + T + S[i+lenT:].replace('?', 'a')]))
if ans == []: print('UNRESTORABLE')
else: print(sorted(ans)[0])

Submission status: Accepted

Solution code:
S = input()
T = input()
t_len = len(T)
s_len = len(S)
output_list = [''] * len(S)
flag = False
hatena_to_a_str = ''
for i in range(s_len):
    if S[i] == '?':
        hatena_to_a_str += 'a'
    else:
        hatena_to_a_str += S[i]
#print(hatena_to_a_str)
current_min = 'z' * (s_len+1)
for i in reversed(range(s_len-t_len+1)):
    #print(i)
    text = S[i:i+t_len]
    if S[i:t_len] == T:
        flag = False
    else:
        for j in range(t_len):
            if text[j] != T[j] and text[j] != '?':
                break 
            if j == t_len-1:
                flag = True
                for s in range(t_len):
                    output_list[i+s] = T[s]
    if flag:
        tmp = hatena_to_a_str[:i] + T + hatena_to_a_str[i+t_len:s_len]
        current_min = min(current_min,tmp)
        flag = False

if current_min == 'z' * (s_len+1):
    print('UNRESTORABLE')
else:
    print(current_min)

Submission status: Accepted

Solution code:
sd = input()
t = input()
sf = ""
for i in sd:
  if i == "?":
    sf += "a"
  else:
    sf += i
S = []
for i in range(len(sd)-len(t)+1):
  b = True
  for j in range(len(t)):
    if sd[i+j] != t[j] and sd[i+j] != "?":
      b = False
  if b:
    s = sf[:i] + t + sf[i+len(t):]
    S.append(s)
if S == []:
  print("UNRESTORABLE")
else:
  print(sorted(S)[0])

Submission status: Accepted

Solution code:
import pprint
s = input()
t = input()
candidate = []
for i in range(len(s) - len(t) + 1):
    can = True
    for j in range(len(t)):
        if s[i + j] == "?" or s[i + j] == t[j]:
            continue
        else:
            can = False
            break
    if can:
        res = []
        for j in range(i):
            if s[j] == "?":
                res.append("a")
            else:
                res.append(s[j])
        for j in range(len(t)):
            res.append(t[j])
        for j in range(len(s) - len(t) - i):
            if s[j + len(t) + i] == "?":
                res.append("a")
            else:
                res.append(s[j + len(t) + i])
        candidate.append("".join(res))

if len(candidate) != 0:
    candidate.sort()
    #pprint.pprint(candidate)
    print(candidate[0])

else:
    print("UNRESTORABLE")

Submission status: Accepted

Solution code:
S = input()
T = input()
ans = []

for left in range(len(S)):
    subS = S[left: left + len(T)]
    if len(subS) < len(T):
        break
    for s, t in zip(subS, T):
        if s == '?':
            continue
        if s != t:
            break
    else:
        U = S[:left] + T + S[left + len(T):]
        ans.append(U.replace('?', 'a'))

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

Submission status: Accepted

Solution code:
from sys import stdin

s, t = stdin.read().split()
s = list(s)

possible_indexes = []
for i in range(len(s) - len(t) + 1):
    substring = s[i: i+len(t)]
    for j in range(len(t)):
        if substring[j] == t[j] or substring[j] == '?': continue
        else: break
    else:
        possible_indexes.append(i)

if not possible_indexes: print('UNRESTORABLE'); exit()

ma = max(possible_indexes)
for j in range(len(t)):
    s[ma+j] = t[j]

s = ''.join(s)

s = s.replace('?', 'a')
print(s)

Submission status: Accepted

Solution code:
s=input()
t=input()
if len(s)<len(t):
  print("UNRESTORABLE")
  exit()
L=[]
for i in range(len(s)-len(t)+1):
  k=s[i:i+len(t)]
  flag=True
  for j in range(len(t)):
    if t[j]!=k[j] and k[j]!="?":
      flag=False
  if flag:
    u=s[:i]+t+s[i+len(t):]
    L.append(u.replace('?','a'))
if len(L)!=0:
  print(min(L))
else:
  print("UNRESTORABLE")

Submission status: Accepted

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

Sd = input().strip()
T = input().strip()

def same(s, t):
    s = re.sub('\?', '.', s)
    return re.match(s, t)

flag = False
for i in range(len(T), len(Sd) + 1):
    if same(Sd[len(Sd) - i:len(Sd) - i + len(T)], T):
        Sd = Sd[:len(Sd) - i] + T + Sd[len(Sd) - i + len(T):]
        flag = True
        break

if flag:
    Sd = re.sub('\?', 'a', Sd)
else:
    Sd = 'UNRESTORABLE'

print(Sd)

Submission status: Accepted

Solution code:
S=input()
T=input()
if len(S)<len(T):
    print('UNRESTORABLE')
else:
    cand=[]
    for i in range(len(S)-len(T)+1):
        if all(s=='?' or s==t for s,t in zip(S[i:i+len(T)],T)):
            cand.append(S[:i].replace('?','a')+T+S[i+len(T):].replace('?','a'))
    print('UNRESTORABLE' if len(cand)==0 else min(cand))

Submission status: Accepted

Solution code:
def main():
    s=list(input())
    t=list(input())
    key=0
    index=0
    if len(s)<len(t):
        print("UNRESTORABLE")
        return
    key2=0
    for i in range(len(s)-len(t)+1):
        for j in range(len(t)):
            if s[i+j]!="?" and s[i+j]!=t[j]:
                key2=1
                break
        if key2==0:
            index=i
            key=1
        key2=0
    if key==0:
        print("UNRESTORABLE")
        return
    for i in range(len(t)):
        s[index+i]=t[i]
    for i in range(len(s)):
        if s[i]=="?":
            s[i]="a"
    print(''.join(s))
if __name__ == "__main__":
    main()

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=input()
n=len(s)
t=input()
m=len(t)
data=[]
for i in range(n-m+1):
    ans=""
    for j in range(i):
        if s[j]=="?":
            ans+="a"
        else:
            ans+=s[j]
    for j in range(m):
        if s[i+j]=="?":
            ans+=t[j]
        elif s[i+j]==t[j]:
            ans+=t[j]
        else:
            break
    else:
        for j in range(n-i-m):
            if s[m+i+j]=="?":
                ans+="a"
            else:
                ans+=s[m+i+j]
        data.append(ans)
data.sort()
if data:
    print(data[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

length_1 = len(s)
length_2 = len(t)


def check_word(num1, length):
    a = s[num1:num1+length]
    b = t[:]
    flag = True
    for i in range(length):
        if a[i] == '?':
            continue
        elif a[i] == b[i]:
            continue
        else:
            flag = False
            break
    if flag is True:
        c = s.copy()
        c[num1:num1+length] = t[:]
        c = fill_a(c)
        return c
    elif flag is False:
        return []


def fill_a(li):
    for i in range(len(li)):
        if li[i] == '?':
            li[i] = 'a'
    return li


word_list = []
for i in range(length_1 - length_2 + 1):
    x = check_word(i, length_2)
    if x != []:
        word_list.append(x)

if word_list == []:
    print('UNRESTORABLE')
else:
    word_list.sort()
    while word_list[0] == []:
        x.pop(0)
    for i in word_list[0]:
        print(i, end='')

Submission status: Accepted

Solution code:
s=input()
T=input()
k=[]
for i in range(len(s)-len(T)+1):
    k.append(s[i:i+len(T)])
#print(k)

z=-1
for i in range(len(k)):
    f=1
    for j in range(len(T)):
        if k[i][j]!=T[j] and k[i][j]!='?':
            f=0
            break
    if f==1:
        z=i

if z==-1:
    print('UNRESTORABLE')
else:
    s+=' '*z
    S=s[:z]+T+s[len(T)+z:]
    S=list(S)
    for i in range(len(S)):
        if S[i]=='?':
            S[i]='a'
    print(''.join(S))

Submission status: Accepted

Solution code:
s = input()
t = input()
n = len(s)
m = len(t)
ans = []
for i in range(n-m+1):
    flag = True
    for j in range(m):
        if s[i+j]!="?" and s[i+j]!=t[j]:
            flag = False
            break
    if flag:
        tmp = (s[:i] + t + s[(i+m):])
        a = []
        for j in range(n):
            if tmp[j]=="?":
                a.append("a")
            else:
                a.append(tmp[j])
        tmp = "".join(a)
        ans.append(tmp)

ans.sort()

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

Submission status: Accepted

Solution code:
# pythonはindexでstringの文字を変更できない
s = list(input())
t = list(input())
x = 0
ans = []
gotk = False

# pythonて関数にするとスコープの外の変数参照できないんだっけ？

# 0 <= k <= s - t
def match(k):
  for i in range(len(t)):
    if s[i + k] != t[i] and s[i + k] != "?":
      return False
  return True

def construct(k,s):
  for i in range(len(t)):
    s[i + k] = t[i]
  for i in range(len(s)):
    if s[i] == "?":
      s[i] = "a"
  return s

# 大きいkから探せば良い(今回逆順にしてるためrangeの第二引数が直前まで処理されることに注意！)
for i in range(len(s) - len(t),-1,-1):
  if match(i):
    x = i
    gotk = True
    break

if gotk == True:
  ans = construct(x,s)
  print("".join(ans))
else:
  print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s=input()
t=input()
sr = len(s)
tr = len(t)
cand = []
for i in range(sr-tr+1):
    flag = True
    for j in range(tr):
        if s[i+j] != '?' and s[i+j] != t[j]:
            flag = False
            break
    if flag:
        tmp = (s[:i] + t + s[(i+tr):]).replace("?", "a")
        cand.append(tmp)
        
cand.sort()

if cand:
    print(cand[0])
else:
    print("UNRESTORABLE")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
import bisect
import sys
import logging
import heapq

#logging.basicConfig(level=logging.DEBUG)

input = sys.stdin.readline

setrecursionlimit(1000000)


def main():
	s = input()[:-1]
	t = input()[:-1]
	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:
			ans = []
			for j in s:
				ans.append(j)
			for j in range(i, i+len(t)):
				ans[j] = t[j-i]
			a = ''
			for i in ans:
				a += i
			print(a.replace('?','a'))
			return
	print('UNRESTORABLE')


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

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def lcm(numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

class PriorityQueue():
	def __init__(self):
		self.queue = []
	def push(self,i):
		heapq.heappush(self.queue,-i)
	def pop(self):
		return -1 * heapq.heappop(self.queue)
	def sum(self):
		return -sum(self.queue)

main()

Submission status: Accepted

Solution code:
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    s = list(str(input().rstrip('\n')))
    t = list(str(input().rstrip('\n')))
    for i in range(len(s)-len(t), -1, -1):
        b = True
        for j in range(len(t)):
            if s[i+j] != "?" and s[i+j] != t[j]:
                b = False
                break
        if b:
            for j in range(len(t)):
                if s[i+j] == "?":
                    s[i+j] = t[j]
            for j in range(len(s)):
                if s[j] == "?":
                    s[j] = "a"
            print("".join(s))
            exit()
    print("UNRESTORABLE")


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

check = False
place = len_s - len_t
while place >= 0:
  temp_s = s[place:(place + len_t)]
  sub_s = [char if char != "?" else t[i] for i, char in enumerate(temp_s)]
  if t == "".join(sub_s):
    check = True
    break
  place -= 1

if check:
  result = [char if char != "?" else "a" for char in s]
  result[place:(place+len_t)] = list(t)
  print("".join(result))
else:
  print("UNRESTORABLE")

Submission status: Accepted

Solution code:
s_d = input()
t = input()
s_d = s_d[::-1]
t = t[::-1]
t_r = ''

for i in range(len(s_d)-len(t)+1):
    for j in range(len(t)):
        if s_d[i+j] == t[j] or s_d[i+j] == '?':
            t_r += s_d[i+j]
            continue
        else:
            t_r = ''
            break
    else:
        s = s_d.replace(t_r, t, 1)
        s = s.replace('?', 'a')
        print(s[::-1])
        break
else:
    print('UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    S = inputs[0]
    T = inputs[1]
    sn = len(S)
    tn = len(T)
    start = None

    for si, s in enumerate(S):
        if tn > (sn - si):
            break
        if s == T[0] or s == '?':
            sj = start_tmp = si
            for t in T:
                sj_c = S[sj]
                if not (t == sj_c or sj_c == '?'):
                    start_tmp = None
                    break
                sj += 1
            start = start_tmp if start_tmp is not None else start

    if start is None:
        return "UNRESTORABLE"

    ret = []
    ti = 0
    for si, s in enumerate(S):
        if si >= start and si < len(T) + start:
            ret.append(T[ti])
            ti += 1
        else:
            if s == '?':
                ret.append('a')
            else:
                ret.append(s)
    return ''.join(ret)


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(2))
    print(ret)

Submission status: Accepted

Solution code:
s = input()
t = input()
index = 0
s = ''.join(list(reversed(s)))
t = ''.join(list(reversed(t)))
#まず、？とtの統合性が取れているかを確認
#後ろから調べてく
exit_flag = True
for i in range(len(s)):
    #逆から見ていき、一文字目が一致するかどうかの確認
    if s[i] == t[0] or s[i] == '?':
        if i + len(t) > len(s):
            break
        for j in range(len(t)):
            if not (s[i + j] == t[j] or s[i + j] == '?'):
                break
            if j == len(t) - 1:
                start_index = i
                exit_flag = False
        if not exit_flag:
            break
#条件に合わなかったら終わり

if exit_flag:
    print("UNRESTORABLE")
    exit()
ans = s[:start_index] + t + s[start_index + len(t):]
ans = ''.join(list(reversed(ans)))
ans = ans.replace("?", "a")
print(ans)

Submission status: Accepted

Solution code:

s_dash = input()
t = input()
answer = 'UNRESTORABLE'
if len(s_dash) < len(t):
    pass
else:
    for i in range(len(s_dash)-len(t)+1):
        for j, k in zip(s_dash[i:i+len(t)], t):
            if j != '?' and j != k:
                break
        else:
            answer = s_dash[:i] + t + s_dash[i+len(t):]
            answer = answer.replace('?', 'a')
print(answer)

Submission status: Accepted

Solution code:
def equ(s,t):
    for i in range(len(t)):
        if t[i]!=s[i]!='?':
            return False
    return True

S1=input()
T=input()

l=len(S1)
m=len(T)

S=''
for i in range(l-m,-1,-1):
    if equ(S1[i:i+m],T):
        S=(S1[:i]+T+S1[i+m:]).replace('?','a')
        break

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

Submission status: Accepted

Solution code:
import sys

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

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

ans = False
con = True

for i in range(len(s)):
    for j in range(len(t)):
        if i+j>len(s)-1:
            con = False
            break
        if s[i+j] == t[j] or s[i+j] =='?':
            if j == len(t)-1:
                ans = True
                memo1 = i
                
        else:
            break
        

    if not(con):
        break

if not(ans):
    print("UNRESTORABLE")
else:
    for i in range(len(t)):
        s[memo1+i] = t[i]
    for i in range(len(s)):
        if s[i] == "?":
            s[i] = "a"

    
    for i in range(len(s)):
        print(s[i],end = "")

print()

Submission status: Accepted

Solution code:
s=input()
s=list(s)
t=input()
n=len(s)
m=len(t)
ind=-1 
def ok(a,b):
    for i in range(len(a)):
        if a[i]=='?':
            pass 
        else:
            if a[i]!=b[i]:
                return 0 
    return  1 
for i in range(n-m+1): 
    if ok(s[i:i+m],t):
        ind=i 
if ind==-1:
    print('UNRESTORABLE')
else: 
    s[ind:ind+m]=t  
    for i in range(len(s)):
        if s[i]=='?':
            s[i]='a'
    print(''.join(s))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import re
No = "UNRESTORABLE"

def main():
    S = input()
    T = input()
    T_LEN = len(T)
    match_index = []

    for i in range(len(S) - T_LEN + 1):
        for idx in range(T_LEN):
            if S[i:i+T_LEN][idx] == T[idx] or S[i:i+T_LEN][idx] == "?":
                continue
            else:
                break
        else:
            match_index.append(i)
    
    if not match_index:
        print(No)
    
    else:
        answer = list(S)
        answer[match_index[0]:match_index[0] + T_LEN] = list(T)
        answer = "".join(answer)
        answer = answer.replace("?","a")

        if len(match_index) == 1:
            print(answer)

        else:
            for i in match_index[1:]:
                tmp_ans = list(S)
                tmp_ans[i:i + T_LEN] = list(T)
                tmp_ans = "".join(tmp_ans)
                tmp_ans = tmp_ans.replace("?","a")
                answer = min(tmp_ans,answer)

            print(answer)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    S = input()
    T = input()
    # permit = ["?"*len(T)]
    LENT = len(T)
    # for i in range(1,LENT+1):
    #     permit.append(T[0:i] + "?"*(LENT-i))

    permit_index = []
    for i in range(len(S)-LENT+1):
        for si in range(LENT):
            if S[i:i+LENT][si] == T[si] or S[i:i+LENT][si]=="?":
                continue
            else:
                break
        else:
            permit_index.append(i)

    if not permit_index:
        print("UNRESTORABLE")
        return

    answer = S.replace("?","z")
    S = S.replace("?","a")
    for i in permit_index:
        s = S[:i]+T+S[i+LENT:]
        answer = min(answer,s)
    
    print(answer)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def change_to_a(sd):
    new_sd=""
    for i in range(len(sd)):
        if sd[i]=="?":
            new_sd+="a"
        else:
            new_sd+=sd[i]
    return new_sd

if lt > ls:
    print("UNRESTORABLE")
elif lt==ls:
    for i in range(ls):
        if s[i]!=t[i] and s[i]!="?":
            print("UNRESTORABLE")
            break
    else:
        print(t)
else:
    for i in range(0,ls-lt+1):
        for j in range(i,i+lt):
            if s[j]!=t[j-i] and s[j]!="?":
                break
            if j==i+lt-1:
                if i==0:
                    #print(0)
                    sa.append(t+change_to_a(s[lt:]))
                elif i==ls-lt:
                    #print(0)
                    sa.append(change_to_a(s[:ls-lt])+t)
                else:
                    #print(0)
                    sa.append(change_to_a(s[:i])+t+change_to_a(s[i+lt:]))

    if sa == []:
        print("UNRESTORABLE")
    else:
        print(min(sa))

Submission status: Accepted

Solution code:
s=input()
t=input()
m=len(s)
n=len(t)
check=False
for i in range(m-n,-1,-1):
  check2=True
  for j in range(i,i+n):
    if s[j]=='?' or s[j]==t[j-i]:
      continue
    else:
      check2=False
  if check2:
    check=True
    s=s[:i]+t+s[i+n:]
    s=s.replace('?','a')
    break
print(s if check else 'UNRESTORABLE')

Submission status: Accepted

Solution code:
import sys
S = input()
T = input()
ans = 'UNRESTORABLE'

tmp = True
anss = -1
anse = -1
for i in range(len(S) - len(T) + 1):
    tmp = True
    s = i
    e = i + len(T)
    for j in range(len(T)):
        if S[i + j] != '?' and\
            S[i + j] != T[j]:
            tmp = False
            break

    if tmp:
        anss = i
        anse = i + len(T)

if anss == -1:
    print(ans)

else:
    ans = S.replace('?', 'a')
    ans = ans[:anss] + T + ans[anse:]
    print(ans)

Submission status: Accepted

Solution code:
import sys
S = input()
T = input()
ans = 'UNRESTORABLE'

if len(S) < len(T):
    print(ans)
    sys.exit()

if len(S) < len(T):
    print(ans)
    sys.exit()

tmp = True
anss = -1
anse = -1
for i in range(len(S) - len(T) + 1):
    tmp = True
    s = i
    e = i + len(T)
    for j in range(len(T)):
        if S[i + j] != '?' and\
            S[i + j] != T[j]:
            tmp = False
            break

    if tmp:
        anss = i
        anse = i + len(T)

if anss == -1:
    print(ans)

else:
    ans = S.replace('?', 'a')
    ans = ans[:anss] + T + ans[anse:]
    print(ans)

Submission status: Accepted

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

n = len(S)
m = len(T)

for i in range(n-m,-1,-1):
    K = []
    K = S[i:i+m]
    #print(K)
    for j in range(m):
        if K[j] == "?":
            K[j] = T[j]
    if K == T:
        print("".join((S[:i]+T+S[i+m:])).replace("?","a"))
        sys.exit()

print("UNRESTORABLE")

Submission status: Accepted

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

def f(s,c):
    for i in range(len(s)):
        if s[i] == '?':
            s[i] = c
    return s

res = ['{']

for i in range(m-n+1):
    tmp = S0[:]
    for j in range(n):
        if tmp[i+j] == '?':
            tmp[i+j] = T[j]
        elif tmp[i+j] != T[j]:
            break
        if j == n-1:
            tmp = f(tmp,'a')
            res = min(res,tmp)

if res == ['{']:
    print('UNRESTORABLE')
else:
    print(''.join(res))

Submission status: Accepted

Solution code:
"""
出典:https://atcoder.jp/contests/abc076/tasks/abc076_c
"""

def main():
  S, T = input(), input()
  if len(S)<len(T):
    print("UNRESTORABLE")
    return

  p_flg=False

  for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
      if S[len(S)-len(T)-i:len(S)-i][j] == T[j]:
        continue
      else:
        if S[len(S)-len(T)-i:len(S)-i][j] == '?':
          continue
        else: 
          break
    else:
      S2 = S[:len(S)-len(T)-i] + T + S[len(S)-i:]
      p_flg=True
      break
  if p_flg:
    print(S2.replace('?','a'))
    return
  else:
    print("UNRESTORABLE")
    return

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
from itertools import permutations
import re


def main():
    s = input().replace('?', '.')
    t = input()

    for i in reversed(range(len(s) - len(t) + 1)):
        # print(s[i:i + len(t)])
        if re.search(s[i:i + len(t)], t):
            result = (s[:i] + t + s[i + len(t):]).replace('.', 'a')
            print(result)
            return

    print('UNRESTORABLE')

main()

Submission status: Accepted

Solution code:
w=input()
w1=input()
num=len(w)
num1=len(w1)
start=-1
if num<num1:
  b1=False
for i in range(num-num1+1):
  b=True
  for j in range(num1):
    if w[i+j]=="?" or w[i+j]==w1[j]:
      pass
    else:
      b=False
  if b:
      start=i
if start==-1:
  print("UNRESTORABLE")
else:
  word=""
  for i in range(num):
    if i<start:
      if w[i]=="?":
        word+="a"
      else:
        word+=w[i]
    elif start<=i and i<=start+num1-1:
      word+=w1[i-start]
    else:
      if w[i]=="?":
        word+="a"
      else:
        word+=w[i]
  print(word)

Submission status: Accepted

Solution code:
def Compare(Ca,Ta):
    if all(Ca[i]==Ta[i] or Ca[i] == "?" for i in range(len(Ca))):
        return 1
    else: return 0
           
S = input()
T = input()
ans = "UNRESTORABLE"
for i in range(1+len(S)-len(T)):
    Comp = S[len(S)-len(T)-i:len(S)-i]
    if Compare(Comp,T):
        ans = S[:len(S)-len(T)-i]+T+S[len(S)-i:]
        ans = ans.replace("?","a")
        break
print(ans)

Submission status: Accepted

Solution code:
sd = input()
t = input()
li_answer = list()
c_loop = len(sd) - len(t) + 1

for i in range(c_loop):
    if all((w_sd == "?" or w_sd == w_t) for w_sd, w_t in zip(sd[i:i + len(t) + 1], t)):
        s_tmp = str(sd[0:i] + t + sd[i + len(t):]).replace("?", "a")
        li_answer.append(s_tmp)

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

Submission status: Accepted

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