CF #724(div2)B. Prinzessin der Verurteilung, BFS枚举

举报
小哈里 发表于 2022/05/11 00:58:01 2022/05/11
【摘要】 problem B. Prinzessin der Verurteilung time limit per test2 seconds memory limit per test256 megabyte...

problem

B. Prinzessin der Verurteilung
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
I, Fischl, Prinzessin der Verurteilung, descend upon this land by the call of fate an — Oh, you are also a traveler from another world? Very well, I grant you permission to travel with me.

It is no surprise Fischl speaks with a strange choice of words. However, this time, not even Oz, her raven friend, can interpret her expressions! Maybe you can help us understand what this young princess is saying?

You are given a string of n lowercase Latin letters, the word that Fischl just spoke. You think that the MEX of this string may help you find the meaning behind this message. The MEX of the string is defined as the shortest string that doesn’t appear as a contiguous substring in the input. If multiple strings exist, the lexicographically smallest one is considered the MEX. Note that the empty substring does NOT count as a valid MEX.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Find out what the MEX of the string is!

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). Description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤1000) — the length of the word. The second line for each test case contains a single string of n lowercase Latin letters.

The sum of n over all test cases will not exceed 1000.

Output
For each test case, output the MEX of the string on a new line.

Example
inputCopy
3
28
qaabzwsxedcrfvtgbyhnujmiklop
13
cleanairactbd
10
aannttoonn
outputCopy
ac
f
b

solution

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
string p = "abcdefghijklmnopqrstuvwxyz";


struct cmp{
	bool operator() (const string &a, const string &b){
		if(a.size()<b.size())return 1;
		else return a<b;
	}
};

int main(){
	int T;  cin>>T;
	while(T--){
		int n;  string s;  cin>>n>>s;
		int ok = 0; string ans;
		queue<string>q;
		for(int i = 0; i < 26; i++){
			string ns = string(1,p[i]);
			q.push(ns);
			if(s.find(ns)==string::npos){
				ok = 1; ans = ns;
				break;
			}
		}
		while(q.size() && ok==0){
			string t = q.front(); q.pop();
			//cout<<t<<"\n";
			if(t.size()==26)break;
			for(int i = 0; i < 26; i++){
				string ns = t+string(1,p[i]);
				q.push(ns);
				if(s.find(ns)==string::npos){
					ok = 1;  ans = ns;
					break;
				}
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}



  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。

原文链接:gwj1314.blog.csdn.net/article/details/118189703

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。