您的位置:首页 > 其它

poj 1961 Period KMP

2013-12-03 15:32 441 查看
题目大意:给定字符串S,求其前n位所组成的字符串,其最小单元重复的次数。是上一篇的拓展;

上一篇

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAXN = 1000010;

int next[MAXN];

void Get_Next(char* str)
{
int i = 0, j = -1;
next[0] = -1;
int len = strlen(str);
while(i < len)
{
if(j == -1 || str[i] == str[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
}

int main()
{
int kcase = 1;
int length;
char str[MAXN];
while(cin>>length)
{
if(length == 0)
break;
cin>>str;
Get_Next(str);
int i = 2;
int len = strlen(str);
cout<<"Test case #"<<kcase++<<endl;
while(i <= len )
{
if(i%(i - next[i]) == 0)
if(i/(i-next[i]) > 1)//别忘了题目要求k>1
cout<<i<<" "<<i/(i-next[i])<<endl;
++i;
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: