poj2406 KMP算法
来源:互联网 发布:毒品网络 百度云 编辑:程序博客网 时间:2024/06/06 10:45
题目 :http://poj.org/problem?id=2406
题目大意 :字符串s1 = “ab”, s2=”bc” 则定义s1×s2 = “abbc”; 又,对于一个字符串s1,
s1^0 = “”。S1^n = s1 * (s1 ^ (n - 1)) ; 给定一个串s,求出最大的n,使得A^n = s。
len[s] <1000000
算法 : KMP
思路 : 由于求的是一个串自身匹配的问题, 于是很容易想到KMP算法,KMP可以在O(n)
的时间内求出模式串pi的最大值k,k满足max(k
提交情况 : Accepted
收获和经验 : 练习了KMP算法,测试了模板
#include <stdio.h>
#include <string.h>
#define MAXN 1000100
char P[MAXN];
int pre[MAXN];
void Get_pre(char P[], int m){
int i, q = -1;
for(i = 0; i < m; i ++){
pre[i] = q;
while(q > -1 &&P[q + 1] != P[i
q = pre[q];
if(P[q + 1] == P[i + 1]) q ++;
}
}
int main(){
int m, i, falg;
while(~scanf("%s", P), P[0] != '.'){
m = strlen(P);
Get_pre(P, m);
if(m % (m - pre[ m - 1] - 1) == 0) printf("%d\n", m / (m - pre[m - 1] - 1));
else printf("1\n");
}
return 0;
}
- poj2406 KMP算法
- POJ2406 KMP算法
- 数据结构之KMP算法---poj2406---Power Strings
- poj2406(KMP)
- poj2406 KMP
- Poj2406【KMP】
- HihoCoder第三周与POJ2406:KMP算法总结
- (POJ2406)Power Strings <KMP算法求最小循环节>
- kmp算法 in POJ【POJ3461,POJ2752,POJ2406,POJ1961】
- poj2406(kmp,后缀数组)
- 【KMP】 poj2406 Power Strings
- poj2406 Power Strings-------KMP
- POJ2406-暴搜,kmp
- poj2406之kmp应用
- [POJ2406] Power Strings[KMP]
- poj2406 Power Strings kmp
- POJ2406 KMP前缀周期
- POJ2406 Power Strings 【KMP】
- hdu2815扩展小步大步攻击
- poj1006 中国剩余定理
- poj2447 RSA公钥加密破译
- 2011年03月21日的日记
- activemq-jms --zhuanzai
- poj2406 KMP算法
- poj1961 KMP
- AC自动机(确定性有限状态自动机)
- 2011年04月06日
- HTML5之Web Worker
- 2011年04月06日的日记
- 模拟退火 poj3422 run away
- hdu3689 AC自动机Dp
- 平面最近圆对儿 hdu3124