AtCoder AtCoder Beginner Contest 076 Dubious Document 2(set与字典序)

来源:互联网 发布:麦迪和艾弗森对位数据 编辑:程序博客网 时间:2024/05/20 14:43

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:

ST'

Output

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

Sample Input 1

Copy
?tc????coder

Sample Output 1

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


Sample Input 2

Copy
??p??d??abc

Sample Output 2

Copy
UNRESTORABLE

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

题意:给定S,T 2个字符串,判断T字符串是否是S的子串,'?'可以代替任何字符,如果存在,输出S的最小字典序的字符串,否则

输出UNRESTORABLE

思路:利用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();}


原创粉丝点击