1536B - Prinzessin der Verurteilung

题目:https://codeforces.com/problemset/problem/1536/B

题目大意:给你一串字符串,然后按字典序求第一个没在这个字符串中出现的子串,如样例:qaabzwsxedcrfvtgbyhnujmiklop,a-z都出现了,aa出现了,ab出现了,ac没出现,答案就是ac。

题解:先用set保存出现过后的子串,然后进行判断

         因为n<1000<27+27^2+27^3,所以子串最长长度为3 

//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <set>
#include <string>
using namespace std;
typedef long long int ll;

set<string>s;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		string b;
		cin >> n >> b;
	    s.clear();
		//1000<26+26^2+26^3
		for (int j = 1; j <= 3; j++)//字串长度
		{
			for (int i = 0; i < n; i++)
			{
				string a;
				//min(n,i + j)若超出n则用n
				int minn = min(n, i + j);;
				for (int k = i; k < minn; k++)
				{
					a += b[k];
				}
				//存下所有子串
				s.insert(a);
			}
		}

		string ans = "a";
		while (1)
		{
			if (!s.count(ans))//判断在不在set里面
			{
				cout  << ans << endl;
				break;
			}
			int len = ans.size();

			if (ans[len - 1] == 'z')//如果最后为z
			{
				while (len >= 1 && ans[len - 1] == 'z')//前一位还是z,进行循环
				{
					ans[len - 1] = 'a';
					len--;
				}
				if (len  ==  0)//循环到最前方,进行加法
				{
					ans = 'a' + ans;
					continue;
				}
				if ( ans[len - 1] != 'z')//循环到中间,进行加法
				 {
					ans[len - 1]++;
				 }
			}
			else
			{
				ans[ans.size() - 1]++;//不为z直接加
			}
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值