poj 2406 KMP算法中next的一个性质
2013-01-20 10:23
176 查看
问题是:如何快速找出S的最小循环周期(循环节)呢?
Len是s的长度
给出结论:如果len%(len-next[len-1])==0,则字符串中必存在最小循环节,且循环次数即为 len/(len-next[len-1])
所以这题知道了上述结论,写出NEXT即可求得循环周期。
Len是s的长度
给出结论:如果len%(len-next[len-1])==0,则字符串中必存在最小循环节,且循环次数即为 len/(len-next[len-1])
所以这题知道了上述结论,写出NEXT即可求得循环周期。
#include <iostream> #include <string.h> using namespace std; char b[1000000]; int next[1000000]; //数组一开始没开大,runtime error了...- -、 int main() { int temp,i; while (cin>>b) { if (b[0]=='.') break; int len=strlen(b); memset(next, 0, sizeof(next)); for (i = 1; i < len; i++) { temp = next[i - 1]; while (temp && b[temp] != b[i]) temp = next[temp - 1]; if (b[temp] == b[i]) next[i] = temp + 1; else next[i] = 0; } if (len%(len-next[len-1])==0) cout<<len/(len-next[len-1])<<endl; else cout<<1<<endl; } return 0; }
相关文章推荐
- poj 2406 Power Strings(KMPnext性质)
- POJ 2406(KMP next数组性质)
- POJ 2406 KMP算法next数组理解
- POJ - 2406_Power Strings _kmp算法next数组的应用
- POJ 2406(KMP中next的性质)
- poj 2406:Power Strings(KMP算法,next[]数组的理解)
- poj 2406 kmp算法巩固之next数组的再理解
- poj 3522 Slim Span (最小生成树 的 一个性质 kruskal 的 应用 )
- KMP算法的练习题(poj 3461、poj 2752、poj 2406、poj 1961)
- poj 2406 Power Strings(kmp next数组的应用)
- poj 1961 2406 让你彻底理解KMP的next[]数组的两个题目
- POJ 1961 Period KMP算法next数组的应用
- [KMP-NEXT数组性质]POJ 2752 Seek the Name, Seek the Fame
- POJ2406 Power Strings(KMP next数组的运用)
- POJ 2406-巧用next数组
- POJ 2406 Power Strings (KMP next数组应用)
- POJ_1961 KMP next的典型应用 类似于 poj2406
- (串的模式匹配4.6.2)POJ 3461 Oulipo(KMP算法的应用——求一个单词在一行文本中的出现次数)
- POJ 2406-Power Strings(重复子串-KMP中的next数组)
- POJ_1961 KMP next的典型应用 类似于 poj2406