kmp简单应用
2014-02-11 18:57
351 查看
Power Strings
Time Limit: 1000MS Memory limit: 65536K
题目描述
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, exponentiationby a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
![](http://acm.sdut.edu.cn/image/2475.png)
输入
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. Aline containing a period follows the last test case.
输出
For each s you should print the largest n such that s = a^n for some string a.示例输入
abcd aaaa ababab .
示例输出
1 4 3
提示
This problem has huge input, use scanf instead of cin to avoid time limit exceed.来源
题目的意思看起来麻烦,但是就是求最大连续字串。示例程序
#include<stdio.h> #include<string.h> int main() { int len,i,j,ok; char a[10000001]; while(gets(a)) { if(strcmp(a,".")==0)break; int len=strlen(a); for(int i=1;i<=len;i++) if(len%i==0) { int ok=1; for(int j=i;j<len;j++) if(a[j]!=a[j%i]) { ok=0; break; } if(ok) { printf("%d\n",len/i); break; } } } }
相关文章推荐
- Linux C函数参考手册(PDF版)
- C#实现打造气泡屏幕保护效果
- C/C++数据对齐详细解析
- C 语言基础教程(我的C之旅开始了)[三]
- C 语言基础教程(我的C之旅开始了)[七]
- C/C++ 宏详细解析
- 用C#生成不重复的随机数的代码
- 在c和c++中实现函数回调
- 浅析C/C++中被人误解的SIZEOF
- C 语言基础教程(我的C之旅开始了)[六]
- C/C++函数调用的几种方式总结
- c/c++中变量的声明和定义深入解析
- 浅析c与c++中struct的区别
- 深入详解C编写Windows服务程序的五个步骤
- 深入理解C/C++混合编程
- C 语言基础教程(一)颜色加亮
- 关于《C和指针》的学习笔记
- c_str()的用法详细解析
- C/C++中可变参数的用法详细解析