B. Prinzessin der Verurteilung

https://codeforces.com/contest/1536/problem/B
有点啰嗦的题解
在这里插入图片描述
INPUT

3
28
qaabzwsxedcrfvtgbyhnujmiklop
13
cleanairactbd
10
aannttoonn

OUTPUT

ac
f
b

题意
给T,循环T,给n,然后给出长度为n的字符串,求找出在字符串中未出现的,最短的,最小字典序的子串
例如:给出串为
abc
其子串并按字典序排序为a,ab,abc,ac,b,bc,c,(有误请指正)
然后所求为未出现的,则符合要求**(最短)**的字串为 d
因为要求最短为前提,所以从单个字母开始,再到双位字母。。。。下去
做法
开个map进行存给出字串的所有子串
然后开一个bfs经行搜索,详情见代码
1.存所有子串

map<string,ll>q;
	ll n;
	cin>>n;
	scanf("%s",a+1);
	for(int i = 1;i<=n;i++)
	{
		string m;
		for(int j = i;j<=n;j++)
		{
			m.push_back(a[j]);
			q[m]++;
		}
	}
即假设原字符串为qwe,
则存如map映射为:
q  qw  qwe  w  we  e
1   1   1  1   1   1

然后开bfs搜索

void bfs()
{
	queue<string>qq;
	string b;			//开一个空的,第一轮不做判断
	qq.push(b);
	bool flag = false;	//只是判断是否为第一次用,
	while(1)
	{
		string now = qq.front();
		qq.pop();
		if(q[now] == 0 && flag)		//q[now] == 0则表示在原字符串的子串中未出现
		{
			ans = now;
			return;
			
		}
										
		for(int x = 0;x<=25;x++)				
		{
			char xx =  x + 'a';
			string xxx = now;
			xxx.push_back(xx);
			qq.push(xxx);
		}
		flag = true;
	}
	return;
}

做打表就能懂得怎么搜索了(注意是以未出现的字串最短为前提)

第一轮加入到qq搜索队列中
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
第二轮
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
aa
ab
ac
ad
ae
af
ag
ah
ai
aj
ak
al
am
an
ao
ap
aq
ar
as
at
au
av
aw
ax
ay
az

AC代码(dalao牛逼)

#include<bits/stdc++.h>
#define ll long long
using namespace std;

char a[1005];
map<string,ll>q;
string ans;
ll n;

void bfs()
{
	queue<string>qq;
	string b;
	qq.push(b);
	bool flag = false;
	while(1)
	{
		string now = qq.front();
		qq.pop();
		if(q[now] == 0 && flag)
		{
			ans = now;
			return;
			
		}
		for(int x = 0;x<=25;x++)
		{
			char xx =  x + 'a';
			string xxx = now;
			xxx.push_back(xx);
			qq.push(xxx);
		}
		flag = true;
	}
	return;
}

int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		scanf("%s",a+1);
		for(int i = 1;i<=n;i++)
		{
			string m;
			for(int j = i;j<=n;j++)
			{
				m.push_back(a[j]);
				q[m]++;
			}
		}
		bfs();
		cout<<ans<<endl;
		for(int i = 1;i<=n;i++)
		{
			string m;
			for(int j = i;j<=n;j++)
			{
			m.push_back(a[j]);
				q[m]--;
			}
		}
	}
	return 0;
}

蒟蒻只配敬 jie 仰 jian大佬题解
如果有问题请指正

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值