您的位置:首页 > 其它

poj 2406 Power Strings(kmp)

2017-08-07 10:06 411 查看
注意判断剩下的一节是否能够被n整除

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=1000010;
int next[maxn],n;
char s[maxn];
void get_next()
{
int j=0,k=-1;
next[0]=-1;

while(j<=n)
{

if(k==-1||s[j]==s[k])
{
j++;k++;

next[j]=k;
}
else k=next[k];
}

}
int main() {
while(~scanf("%s",s))
{
if(s[0]=='.')break;
n=strlen(s);
get_next();
if(n%(n-next
)==0)//判断是否能被n整除
printf("%d\n",n/(n-next
));
else printf("1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: