POJ_2406_Power Strings[KMP]
来源:互联网 发布:怎么删掉淘宝购买记录 编辑:程序博客网 时间:2024/06/07 18:55
/*
Power Strings
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 47643 Accepted: 19867
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
题意:给出一个串,问你这个最多是多少个相同的字串重复连接而成的。如:ababab则最多有3个ab连接而成
Power Strings
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 47643 Accepted: 19867
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
题意:给出一个串,问你这个最多是多少个相同的字串重复连接而成的。如:ababab则最多有3个ab连接而成
*/
#include <cstdio>#include <cstring>using namespace std;const int MAX = 100000000+10;char s[MAX]; int next[MAX];void get_next(int len)//找到KMP中的NEXT数组 {int i=0,j=-1;next[0] = -1;while(i<len){if(j==-1 || s[j]==s[i]){i++; ++j;next[i] = j;}else{j = next[j];}}}int main(){while(scanf("%s",s) != EOF){if(s[0]=='.'){break;}int len = strlen(s);get_next(len);int ans = 1;int t = len-next[len];if(len % t==0) {ans = len / t;} printf("%d\n",ans);}return 0;}
0 0
- POJ_2406_Power Strings[KMP]
- 【KMP】 poj2406 Power Strings
- poj2406 Power Strings-------KMP
- Martian Strings (kmp应用)
- [POJ2406] Power Strings[KMP]
- poj2406 Power Strings kmp
- POJ Power Strings(KMP)
- POJ2406 Power Strings 【KMP】
- Power Strings +poj+KMP
- POJ2406Power Strings KMP算法
- poj2406 Power Strings (KMP)
- poj2406-Power Strings(kmp)
- poj2406 Power Strings(KMP)
- POJ2406 Power Strings【KMP】
- 2406 Power Strings【kmp】
- poj2406 power strings 【KMP】
- poj2406 Power Strings(KMP)
- POJ2406 Power Strings(kmp)
- c/c++和python的三目运算符
- Android 自定义简单实用Dialog
- 使用SwipeRefreshLayout和RecyclerView实现上滑和下拉刷新
- 利用cv_bridge与ROS的SUB通信
- 建立索引的优缺点
- POJ_2406_Power Strings[KMP]
- android 调用系统相册选取照片或者打开相机拍照获取图片
- java时间格式
- Nginx基础
- react native 监听 ios 的home键,监听前后台的变化
- shadowsocks安装配置
- 用javadoc提取此注释文档,并产生一个HTML文件,最后通过浏览器查看结果 直接Tools>Generate javadoc
- HDU
- Android 动态服务SPI的实现