poj2406 Power Strings

Power Strings

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33273   Accepted: 13825

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

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

Source

Waterloo local 2002.07.01

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 int next[1000010],m;
 8 char s[1000010];
 9
10 void get_next()
11 {
12     int i=0,j=-1;
13     next[0]=-1;
14     while(i<m)
15     {
16         if(j==-1||s[i]==s[j])
17         {
18             i++;
19             j++;
20             next[i]=j;
21         }
22         else j=next[j];
23     }
24 }
25 int main()
26 {
27     while(scanf("%s",s)!=EOF)
28     {
29         if(s[0]==‘.‘)break;
30         m=strlen(s);
31         get_next();
32         if(m%(m-next[m])==0)printf("%d\n",m/(m-next[m]));
33         else printf("1\n");
34     }
35     return 0;
36 }

时间: 2024-12-18 15:51:53

poj2406 Power Strings的相关文章

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 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 = "

poj2406 Power Strings (KMP)

这题跟HDU 1358 Period (KMP)差不多,稍微修改代码就行了. 关于KMP的更多知识,请关注从头到尾彻底理解KMP(2014年8月4日版). #include<stdio.h> #include<string.h> int n,next[1000000]; char p[1000000]; void getnext() { int k=0,j=1; next[0]=-1;next[1]=0; while (j<n) { if (k==-1||p[j]==p[k]

poj2406 Power Strings(kmp失配函数)

Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 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 = "abcd

POJ2406:Power Strings(后缀数组DC3)

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 inte

poj2406 Power Strings 2012-01-11

http://162.105.81.212/JudgeOnline/problem?id=2406 ____________________________ 求最长重复字串.即将字符串自我匹配,然后字符串长度减去最后一位的next值即为最长重复字串的长度.注意他求的是由多少个最长重复字串组成. ____________________________ 1 Program stone; 2 var i,j:longint; 3 s:ansistring; 4 b:array[1..1000005]

POJ2406 Power Strings(KMP,后缀数组)

这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即为结果. 若lcp(suffix(0), suffix(k))的长度为n- k,则将串每k位分成一段,则第1段与第2段可匹配,又可推得第2段与第3段可匹配……一直递归下去,可知每k位都是相同的,画图可看出匹配过程类似于蛇形. 用倍增算法超时,用dc3算法2.5秒勉强过. #include<cstdi

POJ2406 Power Strings【KMP】

题目链接: http://poj.org/problem?id=2406 题目大意: 给定两个字符串a和b,定义a*b为两个字符串的链接.比如,a = "abc",b = "def",则 a*b ="abcdef".这个定义当作是多项式.则一个字符串的非负整数次幂可定义如下: a^0 = "",a^(n+1) = a*a^n. 现在给你一个字符串s,求出最大的n,满足s = a^n(a为s的某个子串).比如s = "

POJ2406 Power Strings next数组应用

题目描述: 给定一个字符串,求其最大循环次数(即求最小循环节长度) 输入样例 abcd ababab aaaa . 输出样例 1 3 4 解题思路: KMP算法中next数组的应用. len-next[len]表示的是字符串相同前缀空出来的一段,由next数组性质可知,这一段可以不断向前推出相等,所以只要判断len是否可以整除len-next[len]就可以了.否则就是1. 代码如下 #include <cstdio> #include <cstring> const int ma

【后缀数组】poj2406 Power Strings

连续重复子串(pku2406)给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的,求 R 的最大值.算法分析:做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足.判断的时候,先看字符串 L 的长度能否被 k 整除,再看 suffix(1)和 suffix(k+1)的最长公共前缀是否等于 n-k.在询问最长公共前缀的时候, suffix(1)是固定的,所以 RMQ问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到height[rank[1]]之间