Power Strings kmp
来源:互联网 发布:mac的相册导出 编辑:程序博客网 时间:2024/06/03 22:52
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
题意
题解:
利用kmp中的next数组 (表示最前缀后缀最长匹配长度)
计算该串的next 即next[len]
用总长减去这个值就可以得到非重复串长度 即单位长度
单位长度必须整除总长 否则输出1
AC代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1e6+10;char st[MAXN];int nxt[MAXN];void getnext(int m){ int i = 0,j = 0; nxt[0] = -1;j = nxt[0]; while (i < m){ if (j == -1 || st[i] == st[j]){ nxt[++i] = ++j; }else j = nxt[j]; }}int main(){ while (scanf("%s",st)!=EOF){ if (st[0]=='.') break; memset(nxt,0,sizeof(nxt)); int len = strlen(st); getnext(len); int d; d =len-nxt[len]; if (len%d==0) printf("%d\n",len/d); else printf("1\n"); } return 0;}
- 【KMP】 poj2406 Power Strings
- poj2406 Power Strings-------KMP
- [POJ2406] Power Strings[KMP]
- poj2406 Power Strings kmp
- POJ Power Strings(KMP)
- POJ2406 Power Strings 【KMP】
- Power Strings +poj+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)
- Power Strings --KMP
- poj2046 Power Strings(KMP)
- Power Strings--KMP
- C++之friend关键字
- Linux命令行安装配置Android SDK
- bzoj1087 [SCOI2005]互不侵犯King(状压dp)
- 理解MySQL——索引与优化
- js弹窗
- Power Strings kmp
- Python做接口测试生成测试报告失败
- 指针数组和数组指针的区别
- Shell中参数信息及if条件句含义
- spring @Scheduled注解执行定时任务
- 使用python开发桌面应用程序的框架选择及一些可能出现的问题记录
- MySQL——索引与优化
- 大数据准备-虚拟机系统安装和网络配置(2)
- PHP之数组索引,关联