超时。。。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
char ans[20] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int nexts[maxn];
void get_next(char t[])
{
	nexts[0] = -1;
	int i = 0, j = -1;
	while(i < strlen(t))
	{
		if(j == -1 || t[i] == t[j])
		{
			i++;	j++;	nexts[i] = j;
		}
		else j = nexts[j];
	}
}
int kmp(string s, char t[])
{
	int len1 = s.length(), len2 = strlen(t);
	int i = 0, j = 0;
	while(i < len1 && j < len2)
	{
		if(j == -1 || s[i] == t[j])
		{
			i++;	j++;
		}
		else j = nexts[j];
	}
	if(j == len2)	return 1;
	else return 0;
}
int main()
{
	string s[20];
	int n;	cin >> n;
	char t[maxn];	cin >> t;
	get_next(t);
	for(int k = 2; k <= 16; k++)
	{
		for(int i = 1; i <= n; i++)
		{
			int x = i;	string temp;
			while(x > 0)
			{
				temp += ans[x % k];
				x /= k;
			}
			for(int x = temp.length() - 1; x >= 0; x--)
				s[k] += temp[x];
		}
		if(kmp(s[k], t))
		{
			cout << "yes"<< endl;
			return 0;
		}
	}
	cout << "no" << endl;
	return 0;
}
样例过了80%,超时了,为什么啊??求解优化
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务