B. Prinzessin der Verurteilung

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
input
3
28
qaabzwsxedcrfvtgbyhnujmiklop
13
cleanairactbd
10
aannttoonn
output
ac
f
b

题意:
按照字典序找到给出的字符串里面没有出现过的顺序最小排列的字符串构造并输出。

思路:
暴搜

ps:一开始也想过暴搜,不过能力有限写不出来orz。记一下大佬的操作,绝绝子。

#include<iostream>
#include<string>
#include<queue>
using namespace std;

int n;
string a;

string bfs()
{
	queue<string> q;
	
	//罗列单字母 
	for(int i = 0; i <= 25; ++i)
	{
		char ch = 'a' + i;
		string o;
		o.push_back(ch);
		q.push(o);
	}

	while(!q.empty())
	{
		string u = q.front();
		q.pop();
		
		//string::npos参数,是string::type_size类型,即find()返回类型 
		//npos是一个常数,用来表示不存在的位置 
		//find()在找不到指定值的情况下会返回string::npos 
		if(a.find(u) == string :: npos)
			return u;
		
		//罗列多字母
		for(int i = 0; i <= 25; ++i)
		{
			char ch  = 'a' + i;
			u.push_back(ch);
			q.push(u);
			u.pop_back();
		 } 
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int T;
	cin >> T;
	while(T--)
	{
		cin >> n;
		cin >> a;
		cout << bfs() << endl;
	}
	return 0;
 } 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值