poj2406 power strings 【KMP】
2015-08-07 16:13
423 查看
Power Strings
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 66 Accepted Submission(s) : 25
Problem 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
【分析】如果len可以被len-next[len]整除,则s存在循环子串,否则不存在。最短循环子串为str[len-next[len]],最大循环次数为len/(len-next[len]).
【代码】
#include<stdio.h> #include<string.h> const int maxn = 1000010; int p[maxn],len; char s[maxn]; void getp() { 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)&&strcmp(s,".")) { len=strlen(s); getp(); if(len%(len-p[len])==0) printf("%d\n",len/(len-p[len])); else printf("1\n"); } return 0; }
相关文章推荐
- 从C调用Lua
- JDK安装
- 游戏最高渲染帧率的垂直同步方式-----显示器的垂直刷新率
- 最小生成树之算法记录【prime算法+Kruskal算法】【模板】
- 异步时钟的同步化,俗称“慢打一拍",寄存一拍
- arpwatch
- java 多线程读取多个文件 和 不用线程读取多个文件
- Cause: java.sql.SQLException: Bad format for Time '308:42:13' in column
- HttpContext.Current.User.Identity.Name获得不了登录名
- 通过mmap函数快速实现文件复制
- hdu 5358 First One (2015多校第六场第6题)尺取法枚举区间和
- Android单元测试时如何使用log查看输出结果
- c#中动态对象的RuntimeTypeHandle
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Spring : 征服数据库 (两)
- jQuery过滤选择器
- selenium webdriver 元素定位方法
- maven 学习笔记
- Codeforces 567A 567B 567C 567D
- android 单元测试 radio group