POJ2406 Power Strings
2016-07-17 09:07
197 查看
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 42637 | Accepted: 17787 |
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
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数组,设字符串长度为len,若len/(len-next[len])为整数,那么这个数就是答案。
类似的题见http://www.cnblogs.com/SilverNebula/p/5550595.html
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int next[1000010]; char s[1000010]; int len,ans; void getnext(){ int i,j; next[0]=0; next[1]=0; for(i=1,j=0;i<len;i++){ while(s[j]!=s[i] && j)j=next[j]; if(s[j]==s[i])j++; next[i+1]=j; } return; } int main(){ while(scanf("%s",s)!=EOF){ if(s[0]=='.')break; len=strlen(s); // memset(next,0,sizeof next); getnext(); ans=0; if(len%(len-next[len])==0) printf("%d\n",len/(len-next[len])); else printf("1\n"); } return 0; }
相关文章推荐
- 细说JavaScript对象(4): for in 循环
- C++实现 vector 的四则运算
- 细说JavaScript对象(3):hasOwnProperty
- 细说JavaScript对象(2):原型对象
- 关于unity中的update、Lateupdate和FixedUpdate。
- 【YMGJ 6】请选择一条记录
- 博客说明
- 细说JavaScript对象(1):对象的使用和属性
- egret3D与2D混合开发,画布尺寸不一致的问题
- Collections类常用方法
- 【译】PHP之道(PHP the right way)
- 《OD学hive》第四周0717
- Linq中疏漏的几个知识点
- 关于Activity的描述————Android面试总结(1)
- hadoop mapreduce join原理、方法讲解
- 7月第2周——周记
- DownloadManager学习笔记
- java之继承并重写
- javaScript--05 DOM基础 12.12
- 【安居客】资深PHP软件开发工程师