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≤|S'|,|T|≤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
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.
题意:给定S,T 2个字符串,判断T字符串是否是S的子串,'?'可以代替任何字符,如果存在,输出S的最小字典序的字符串,否则输出UNRESTORABL思路:利用set自动排序的特性,构造出多个T是S子串的字符串,然后挑选字典序最小的即可,(之前我是想用KMP算法,直接判断是否存在子串,然后再进行变化,不过事实证明这样的算法可行性太低,但是自身学习了下KMP算法也不错)
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<map>
#include<set>
using namespace std;
string s, t;
set<string> S;
void Work()
{
int lens = s.size();
int lent = t.size();
for(int i = 0; i < lens; i++)
{
if(s[i] == '?' || s[i] == t[0])//如果字符相等
{
string tmp = s; //构造判断子串之前的字符
for(int j = 0; j < i; j++)
if(tmp[j] == '?')
tmp[j] ='a';
bool can = false;
for(int j = 0; j < lent; j++) //判断是否为子串
{
if(tmp[i+j]!= '?' && tmp[i+j] != t[j]) //字符匹配
break;
tmp[i+j] = t[j];
if(j == lent - 1) can = true;
}
if(can)
{
for(int j = i + lent; j < lens; j++) if(tmp[j] == '?') tmp[j] = 'a';//处理匹配后的字符
S.insert(tmp); //压入容器
}
}
}
if(S.size() == 0) cout << "UNRESTORABLE" << endl;
else cout << *S.begin() << endl;
}
int main()
{
cin >> s >> t;
Work();
}