E - Power Strings,求最小周期串

E - Power Strings

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 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

abcd
aaaa
ababab
.

Sample Output

1
4
3

讲了这么多,其实就是一个求一个字符串里最小周期的问题。限时有3S,我们可以用枚举。这里需要注意的是一个:求周期串的基本算法:for(i=1;i<=len;i++){  ok=1;  if(len%i==0)    {     for(j=i;j<len;j++)      {        if(s[j]!=s[j%i]){ok=0;break;}      }    }if(ok==1){ printf("%d\n",len/i);  break;//记得要break}}
 1 #include<cstdio>
 2 #include<string.h>
 3 using namespace std;
 4 char s[1000100];
 5 int main()
 6 {
 7     while(scanf("%s",s)==1&&strcmp(s,".")!=0)
 8     {
 9         int len=strlen(s);
10
11         for(int i=1;i<=len;i++)
12             if(len%i==0)
13         {
14              int ok=1;
15             for(int j=i;j<len;j++)
16             {
17                 if(s[j]!=s[j%i])
18                 {
19                     ok=0;
20                     break;
21                 }
22             }
23             if(ok)
24             {
25                  printf("%d\n",len/i);
26                  break;
27             }
28
29         }
30     }
31     return 0;
32 }

E - Power Strings,求最小周期串

时间: 2024-10-13 15:58:42

E - Power Strings,求最小周期串的相关文章

POJ 2406 Power Strings (求字符串循环节出现的次数)

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

算法篇——最小周期串

来源:<算法竞赛入门经典>例题5.1.3 题目:如果一个字符串可以由某个长度为k的字符串重复多次得到,我们说该串以k为周期.例如,abcabcabcabc以3为周期(注意,它也以6和12为周期).输入一个长度不超过80的串,输出它的最小周期. 样例输入:HoHoHo 样例输出:2 分析:题目中说过,字符串可能会有多个周期.但因为只需求出最小的一个,可以从小到大枚举各个周期,一旦符合条件就立即输出. 源码: #include<stdio.h> #include<string.h

poj 2406 Power Strings求子串在主串中最多叠加次数

#include<stdio.h> #define M 1000010 int n,next[M]; char s[M]; void getNext() { int i=1,j=-1; next[0]=-1; for(;s[i];i++){ while(j!=-1&&s[j+1]!=s[i])j=next[j]; if(s[j+1]==s[i])j++; next[i]=j; } n=i; } int main() { while(scanf("%s",s)

uva 周期串(求模分组)

/*本题题意求出周期串的最短长度:eg : hohoho 那么此周期串的长度可以为2 (ho) 同时也可以为 6(hohoho)  最短长度当然就是2本题思路:    求出周期串, 必然存在一个周期, 设定一个循环, i代表周期的长度,从1递增,最长的长度为字符串的长度 s.size(), 通过周期 将 字符串分组分组的字符串长度必须为整数, 因此只用考虑整数长度的字符串, 如果存在字符串所有分组都与第一组的字符相同, 那么此时就满足题意要求,输出对应的的周期长度 也就是最短的周期串长度, 本题

KMP + 求最小循环节 --- POJ 2406 Power Strings

Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少次得到. analyse: KMP之next数组的运用.裸的求最小循环节. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Accepted* Submission Date: 20

POJ--2406Power Strings+KMP求字符串最小周期

题目链接:点击进入 事实上就是KMP算法next数组的简单应用.假设我们设这个字符串的最小周期为x 长度为len,那么由next数组的意义,我们知道len-next[len]的值就会等于x.这就是这个题目的关键点. 代码例如以下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=1000000+100; char str[maxn]; in

POJ - 2406 - Power Strings (字符串求周期--next函数的妙用)

题目传送:POJ - 2406 思路:就是利用kmp中next数组的特性来求最大周期的 AC代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000005; char s[maxn]; int next[maxn]; int main() { while(scanf("%s", s) != EOF)

UVa 455 Periodic Strings (周期串)

Periodic Strings Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Periodic Strings  A character string is said to have period k if it can be formed by concatenating one or more repetitions of anothe

poj2406--Power Strings(KMP求最小循环节)

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