数据结构之KMP算法---poj2406---Power Strings
来源:互联网 发布:mac地址一样 编辑:程序博客网 时间:2024/05/18 00:17
http://poj.org/problem?id=2406
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
abcdaaaaababab.
Sample Output
143代码实现:
#include<iostream>#include<cstring>#define max 1000001using namespace std;int next[max];char str[max];int getnext(char *str){ int i=0,j=-1; int len=strlen(str); next[0]=-1; while(i<len) { if(j==-1||str[i]==str[j]) { i++; j++; next[i]=j; } else j=next[j]; } i=len-j; if(len%i==0) return len/i; else return 1;}int main(){ while(cin>>str) { if(str[0]=='.')break; int ans=getnext(str); cout<<ans<<endl; } return 0;}
0 0
- 数据结构之KMP算法---poj2406---Power Strings
- 【KMP】 poj2406 Power Strings
- poj2406 Power Strings-------KMP
- [POJ2406] Power Strings[KMP]
- poj2406 Power Strings kmp
- POJ2406 Power Strings 【KMP】
- poj2406 Power Strings (KMP)
- poj2406-Power Strings(kmp)
- poj2406 Power Strings(KMP)
- POJ2406 Power Strings【KMP】
- poj2406 power strings 【KMP】
- poj2406 Power Strings(KMP)
- POJ2406 Power Strings(kmp)
- 【kmp】【poj2406】Power Strings
- [poj2406]Power Strings KMP
- poj2406 Power Strings(KMP)
- POJ2406 Power Strings(KMP)
- poj2406--Power Strings (KMP)
- 【java】向文本文件中写入随机整数(供测试)
- 网页表格内容导入excel
- 如何轻松学习Python
- WIN7下硬盘安装ubuntu12.04出现的种种问题及解决方案
- [Leetcode] LRU Cache (Java)
- 数据结构之KMP算法---poj2406---Power Strings
- 远程调用原理与对比RMI、MINA、ESB、Burlap、Hessian、SOAP、EJB
- 开发笔记之20140217
- 强势领导力-4-历史人物借鉴及经验教训
- java -verbose 命令
- 关于ContentProvider 使用replace
- 花影缭乱,人已黯然
- 【java】求大数的阶乘
- 线程同步