poj 2406
来源:互联网 发布:js string.format 编辑:程序博客网 时间:2024/06/05 07:02
Power Strings
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 26943 Accepted: 11281
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
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
Source
Waterloo local 2002.07.01
主要是next函数,才发现n个字符是n+1个next值(next0-----nextn),然后l%(l-nextn)==0代表字符串由l/(l-nextn)个相同字符串组成。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char s[1000005];int next[1000005];void getnext(){ int l=strlen(s); int j=0,k=-1; next[0]=-1; while(j<l) { if(k==-1||s[j]==s[k]) { j++; k++; next[j]=k; } else k=next[k]; } int h=l-k; if(l%h==0)cout<<l/h<<endl; else cout<<1<<endl;}int main(){ while(scanf("%s",&s)) { if(s[0]=='.')return 0; getnext(); }}
- POJ 2406
- POJ 2406
- poj 2406
- POJ 2406
- poj 2406
- poj 2406
- poj 2406
- poj 2406
- poj 2406
- poj 2406
- 【POJ 2406 】
- POJ 2406
- poj 2406
- POJ 2406
- poj 2406
- POJ-2406
- POJ 2406
- poj--2406
- poi读取word文档
- VS2005 快捷键
- 在linux下was安装成功但无法找到概要文件
- 根据身份证号判断性别、生日(excel表格,公式)
- C++课程设计 之 俄罗斯方块
- poj 2406
- 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别
- WinCE6.0虚拟串口的若干关键问题
- SQLSERVER数据类型
- Android Launcher2代码跟踪
- android权限大全
- 我们公司的用SANGFOR将内部员工的网封掉后,有员工破解掉,还可以上网,麻烦问下有没有什么办法给阻止掉》
- 模拟器运行时,xxx has stopped unfortunately
- 深入研究了解Windows的消息处理机制