poj 2406 Power Strings 【kmp】
2015-08-07 20:29
375 查看
Power Strings
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
Sample Output
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
Source
Waterloo local 2002.07.01
分析:
查找最大序列数。
代码:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 37564 | Accepted: 15532 |
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
abcd aaaa ababab .
Sample Output
1 4 3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
Source
Waterloo local 2002.07.01
分析:
查找最大序列数。
代码:
#include<cstdio> #include<cstring> const int max= 1000100; char str[max]; int p[max]; int cnt; int len; void getp() { len=strlen(str); int i=0,j=-1; p[0]=-1; while(i<len) { if(j==-1||str[i]==str[j]) { i++,j++; p[i]=j; } else j=p[j]; } } int main() { while(scanf("%s",str)!=EOF) { if(strcmp(str,".")==0) break; getp(); cnt=1; // for(int i=0;i<len;i++) // printf("%d ",p[i]); if(len%(len-p[len])==0) cnt=len/(len-p[len]); printf("%d\n",cnt); } return 0; }
相关文章推荐
- Vs2013中空格出现了绿色的点(已解决)
- Android实现程序第一次安装的引导界面
- DP专题练习题解
- HDU 5361 In Touch(最短路 + 线段树)
- markup.h
- 正则表达式
- android Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Must specify unique
- 单变量线性回归
- 工作小记 之更改数据恢复,
- 线程的生命周期
- 九个衡量 Rails 应用性能的小方法
- IO流
- C语言 用汇编来看看变量和指针实现
- Bootstrap的js插件之警告框(alert.js)
- Poj 2752 Seek the Name, Seek the Fame
- 跨域问题解决
- java 序列化与反序列化
- UBOOT目录解释(一)
- MyBatis -- 对表进行增删改查(基于注解的实现)
- 媒体播放器三大底层架构