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;
}
#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;
}
相关文章推荐
- POJ 2406 Power Strings(后缀数组或KMP或扩展KMP)
- poj 2406 Power Strings (用kmp代替后缀数组求解)
- poj 2406 Power Strings(KMP求循环次数)
- POJ2406 Power Strings 解题报告【字符串】【KMP】
- poj2406 Power Strings(KMP)
- 【POJ 2406】 Power Strings(KMP求循环节)
- poj 2406 Power Strings【kmp】
- POJ - 2406 Power Strings KMP循环节出现次数
- POJ 2406 Power Strings KMP 求最小循环节
- poj 2406 Power Strings 【kmp】
- poj_KMP_2406_Power Strings
- POJ 2406 Power Strings (kmp求循环节)
- POJ -2406 Power Strings(循环节,kmp专题)
- POJ 2406 Power Strings 求最小循环节数(KMP)
- poj--2406--Power Strings(KMP)
- POJ 2406 Power Strings [KMP+循环节]
- POJ 2406 Power Strings KMP 或 后缀数组
- POJ 2406 Power Strings (KMP找字符串的循环节)
- POJ 2406 Power Strings 简单KMP
- |poj 2406|KMP|Power Strings