poj 2406 Power Strings
2015-11-08 17:16
441 查看
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
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 39097 | Accepted: 16230 |
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
#include<stdio.h> #include<string.h> int p[10000010]; char s[10000010]; int len; void kmp() { int i=0,j=-1; p[i]=j; while(i<len) { if(j==-1||s[i]==s[j]) { i++; j++; p[i]=j; } else j=p[j]; } } int main() { while(scanf("%s",s)!=EOF) { if(strcmp(s,".")==0) break; memset(p,0,sizeof(p)); len=strlen(s); kmp(); if(len%(len-p[len])==0) printf("%d\n",len/(len-p[len]));//循环次数为 else printf("1\n"); //printf("%d\n",len-p[len]);//循环节长度为 } return 0; }
相关文章推荐
- Golang学习笔记 (一)
- 阿里无线前端性能优化指南 (Pt.1 加载期优化)
- Ubuntu下安装libsvm
- hdu2084 数塔 (动态规划)
- IOS毛玻璃效果
- 汇编语言-[BX]和loop指令
- HDU 4515 模拟
- SQL基础学习6
- 汇编语言-[BX]和loop指令
- Activity的"singleTask"之谜
- Unix c 网络编程 UDP
- 知道的越多,思考得越深,下的决策越正确
- iOS开发计算文本尺寸
- Java基础3笔记
- Unix c 网络编程 TCP
- Groovy学习笔记(一)—— 初识Groovy
- 在 CSDN 开工
- nagios配置过程详解
- 去IOE关键技术分析_吕海波(新)
- (纪录片)鸟瞰中国 China from Above