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
?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的最小字典序的字符串,否则
输出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();}
- AtCoder AtCoder Beginner Contest 076 Dubious Document 2(set与字典序)
- AtCoder Beginner Contest 076 D
- AtCoder Beginner Contest 055
- AtCoder Beginner Contest 052
- AtCoder Beginner Contest 057
- AtCoder Beginner Contest 063
- AtCoder Beginner Contest 069
- AtCoder Beginner Contest 069
- AtCoder Beginner Contest 070
- AtCoder Beginner Contest 072
- AtCoder Beginner Contest 072
- AtCoder Beginner Contest 072
- AtCoder Beginner Contest 073
- AtCoder Beginner Contest 072
- AtCoder Beginner Contest 075
- AtCoder Beginner Contest 081
- AtCoder Beginner Contest 081
- AtCoder Beginner Contest 082
- 解密DNA机器人“巨大的一小步”:血液循环中精准送达药物
- 3分钟学会sessionStorage用法
- 博世投资张翠波:自动驾驶需要突破芯片、传感器、深度学习算法等关键技术
- Google智能生态链的演进路径
- 任正非最新讲话透露:华为在加快开发统一的人工智能平台
- AtCoder AtCoder Beginner Contest 076 Dubious Document 2(set与字典序)
- Hive学习笔记001
- 详解C/C++预处理器
- HDU 2159(动态规划-完全背包)
- Qt编程中qmake的使用详解
- 微擎全局变量和预定义常量
- 安利一则深度学习新手神器:不用部署深度学习环境了!也不用上传数据集了!
- 深度丨是的,我们正处于数字与实体混合生态的交叉路口
- 快讯 | 旷视科技Face++宣布完成C轮4.6亿美元融资,刷新AI领域融资纪录