牛客练习赛70 ------ 拼凑

题目:

牛牛还是很喜欢字符串"puleyaknoi"。
牛牛有T个超长超长的字符串,不过这次他更懒了,他希望直接在字符串中看见他喜欢的字符串。
如果一个子串中含有一个子序列是”puleyaknoi“,那么他就把这个子串称作好的子串。
牛牛是个懒人,他不喜欢看太长的子串,那样他会觉着眼镜很累。
你能帮他求出对于每个字符串,最短的好的子串的长度是多少吗?
如果没有,请输出-1

输入

第一行一个T表示数据组数
接下来T行每行一个字符串(保证字符串只含小写字母)

输出

共T行每行一个答案

样例

输入

3
sxpuleyaaknoip
pionkaayelupxs
yydspwuwlwewywawkwnwowiw

输出:

11
-1
19

思路

菜鸡只会暴力,扫一遍字符串,当扫到P的时候,就停止,然后开始判断是否符合题意,进行匹配

AC代码:

弱鸡只会暴力

#include<stdio.h>
#include<iostream>
#include<map>
#include<queue>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;

#define ll long long

const int N = 1e5 + 15;
char str[N];
int main()
{
	int t; scanf("%d", &t);
	while (t--) {
		scanf("%s", str);
		int len = strlen(str);
		int res = N;
		int num = 0;
		for (int i = 0; i < len; i++) {
			if (str[i] == 'p') {
				num = 1;
				map<char, int>mp;
				mp['p'] += 1;
				for (int j = i; j < len; j++) {
					if (mp['p'] && str[j] == 'u')
						mp[str[j]] += 1;
					if (mp['u'] && str[j] == 'l')
						mp[str[j]] += 1;
					if (mp['l'] && str[j] == 'e')
						mp[str[j]] += 1;
					if (mp['e'] && str[j] == 'y')
						mp[str[j]] += 1;
					if (mp['y'] && str[j] == 'a')
						mp[str[j]] += 1;
					if (mp['a'] && str[j] == 'k')
						mp[str[j]] += 1;
					if (mp['k'] && str[j] == 'n')
						mp[str[j]] += 1;
					if (mp['n'] && str[j] == 'o')
						mp[str[j]] += 1;
					if (mp['o'] && str[j] == 'i') {
						mp[str[j]] += 1;
						res = min(num, res);
						break;
					}
					num++;
				}
			}
		}
		if (res == N)
			printf("-1\n");
		else
			printf("%d\n", res);
	}
	return 0;
}
全部评论

相关推荐

投递腾讯等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务