POJ 2406 Power Strings KMP 求最小循环节
2014-01-19 22:58
429 查看
Power Strings
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 |
利用kmp中next(我的数组名是p)数组的性质。
表达能力拙计。。。分析样例的next数组找数学规律吧。
还有,用cin是可以AC的。
#include <iostream> using namespace std; string s; int l,i,j,p[1000010]; int main() { ios::sync_with_stdio(false); while (cin>>s && s!="."){ l=s.size(); j=-1; p[0]=-1; for (i=1;i<l;++i){ while (j>-1 && s[i]!=s[j+1]) j=p[j]; if (s[i]==s[j+1]) ++j; p[i]=j; } i=l-p[l-1]-1; if (l%i==0) cout<<l/i<<endl; else cout<<"1\n"; } return 0; }
kdwycz的网站: http://kdwycz.com/
kdwyz的刷题空间:http://blog.csdn.net/kdwycz
相关文章推荐
- Tyvj 1034 尼克的任务 DP+邻接表
- 2014年Java培训哪里好?
- 基于Android的SDIO-WIFI移植(2) .
- 给button添加弧度的方法
- 广州传智播客Java培训模式一直被模仿从未被超越
- sgu 101 Domino 欧拉通路
- Juju 冰淇淋图标设计
- IOS国际化,本地化全过程
- Android WIFI框架分析(1)
- 基于Android的SDIO-WIFI移植(2)
- linux命令:time
- 每日一练(六)多窗口显示
- navigationItem的返回item的title不生效的解决
- ACM POJ 1204 (Word Puzzles)
- sgu 111 Very simple problem 高精开平方
- android 转屏时的数据保存方法
- UIButton---iOS
- UIButton---iOS
- 网站优化策略
- 广州传智播客PHP培训基础就业班都学习哪些课程