Codeforces Round #724 (Div. 2) 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 𝑛 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 𝑎 is lexicographically smaller than a string 𝑏 if and only
if one of the following holds:

𝑎 is a prefix of 𝑏, but 𝑎≠𝑏; in the first position where 𝑎 and 𝑏
differ, the string 𝑎 has a letter that appears earlier in the
alphabet than the corresponding letter in 𝑏. A string 𝑎 is a
substring of a string 𝑏 if 𝑎 can be obtained from 𝑏 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 𝑡 (1≤𝑡≤1000). Description of the test cases
follows.

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

The sum of 𝑛 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

题意

即找出字符串中没有出现的字典序最小的字符串

题解

#include<bits/stdc++.h>
using namespace std;
string conv(int n)//按照字典序大小遍历字符串
{
    if(n==0)return "";
    else return conv((n-1)/26)+(char)((n-1)%26+'a');
}
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        string s;
        cin>>s;
        for(int i=0;;i++)//i代表想要找的字符串有多少位
        {
            if(s.find(conv(i))==s.npos)//这里的s.npos代表着不存在 如果s中没有找到这个子串 就输出它
            {
                cout<<conv(i)<<endl;
                break;
            }
        }
    }
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,pos,f;
string s;

signed main()
{
	cin>>t;while(t--)
	{
		queue<string>q;
		f=1;
		cin>>n>>s;
		for(int i=1;i<=26;i++)
		{
			string r;
			r=(char)(i+'a'-1);
			q.push(r);
		}	
		while(f==1)
		{
			string a;
			a=q.front();
			q.pop();
			if(s.find(a)!=s.npos)
			{
				for(int i=1;i<=26;i++)
				{
					a.push_back((char)(i+'a'-1));
					q.push(a);
					a.pop_back();
				}
			}
			else 
			{
				cout<<a<<endl;
				break;
			}
		}
	}
	return 0;
}

按照字典序大小遍历字符串的方法

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


void solve()
{
    queue<string> q;
    for(int i=1;i<=26;i++)
    {
        string r;
        r=(char)(i+'a'-1);
        q.push(r);
    }
    int cnt=0;
    while(1)
    {
        cnt++;
        string a;
        a=q.front();
        cout<<a<<endl;
        q.pop();
        for(int i=1;i<=26;i++)
        {
            a.push_back((char)(i+'a'-1));
           
            q.push(a);
            a.pop_back();
        }
        if(cnt==1000)break;
    }
   
}


int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值