poj 2406 Power Strings【kmp循环节】
来源:互联网 发布:mac截图快捷键 保存 编辑:程序博客网 时间:2024/05/21 10:10
Power Strings
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 50448 Accepted: 21038
Description
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcdaaaaababab.
Sample Output
143
思路:KMP,next表示模式串如果第i位(设str[0]为第0位)与文本串第j位不匹配则要回到第next[i]位继续与文本串第j位匹配。则模式串第1位到next[n]与模式串第n-next[n]位到n位是匹配的。所以思路和上面一样,如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]。
例如:a b a b a b
next:-1 0 0 1 2 3 4
next[n]==4,代表着,前缀abab与后缀abab相等的最长长度,这说明,ab这两个字母为一个循环节,长度=n-next[n];
/* *********************************************** ┆ ┏┓ ┏┓ ┆ ┆┏┛┻━━━┛┻┓ ┆ ┆┃ ┃ ┆ ┆┃ ━ ┃ ┆ ┆┃ ┳┛ ┗┳ ┃ ┆ ┆┃ ┃ ┆ ┆┃ ┻ ┃ ┆ ┆┗━┓ 马 ┏━┛ ┆ ┆ ┃ 勒 ┃ ┆ ┆ ┃ 戈 ┗━━━┓ ┆ ┆ ┃ 壁 ┣┓┆ ┆ ┃ 的草泥马 ┏┛┆ ┆ ┗┓┓┏━┳┓┏┛ ┆ ┆ ┃┫┫ ┃┫┫ ┆ ┆ ┗┻┛ ┗┻┛ ┆ ************************************************ */ #include<stdio.h> #include<string.h>char S[1000005];int next[1000005],l;void GetNext(){int i=0,j=-1;next[0]=-1;j=next[i];while(i<l){if(j==-1||S[i]==S[j]){next[i+1]=j+1;i++; j++;}else{j=next[j];}} }int main(){while( ~scanf("%s",S) ){if(S[0]=='.')break;l=strlen(S);GetNext();if(l%(l-next[l])==0) printf("%d\n",l/(l-next[l])); else printf("1\n");}return 0;}
阅读全文
0 0
- 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) 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 Power Strings(kmp循环节)
- 【POJ 2406】 Power Strings(KMP求循环节)
- POJ 2406 Power Strings [KMP+循环节规律]
- POJ 2406Power Strings kmp算法求循环节
- POJ 2406 Power Strings (kmp求循环节)
- .net通过继承HttpModule类实现Url重写
- 推广
- selenium的常见异常
- iOS开发笔记之六十七——Category使用过程中的一些注意事项
- TDK电感选型指南
- poj 2406 Power Strings【kmp循环节】
- 3ds MAX 2014安装指南
- Activiti 生成图片乱码问题解决备忘录
- Android 7.0 行为变更 通过FileProvider在应用间共享文件吧
- 从JDK源码角度看Integer
- hdu1233(floyd+并查集)
- 回首
- python基础--list[]
- Git Flow工作流