Power Strings POJ - 2406

Power Strings

POJ - 2406

时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u

提交 状态

已开启划词翻译

问题描述

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).

输入

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.

输出

For each s you should print the largest n such that s = a^n for some string a.

样例输入

abcd
aaaa
ababab
.

样例输出

1
4
3

提示

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

来源

Waterloo local 2002.07.01

题意:一个字符串可以由其循环节循环n次得到,问n最大可以是多少……

kmp练习。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 #define maxn 10000008
 8
 9 char s[maxn];
10 int next[maxn];
11
12 void getNext()
13 {
14     int j, k, i;
15     i = strlen(s);
16
17     j = 0;
18     k = -1;
19     next[0] = -1;
20     while(j < i)
21     {
22         if(k == -1 || s[j] == s[k])
23         {
24             j++;
25             k++;
26             next[j] = k;
27         }
28         else
29             k = next[k];
30     }
31 }
32
33 int main()
34 {
35     while(1)
36     {
37         scanf("%s", s);
38         if(s[0] == ‘.‘)
39             break;
40
41         int q = strlen(s);
42
43         memset(next, 0, sizeof(next));
44         getNext();
45
46         int k = next[q];   // k表示s串 q位置的最长的前缀和后缀相等的长度。q-k代表最小循环节的长度
47
48         if(q % (q - k) == 0)  // 如果q-k>q 的一半即k 小于s串的一半长度,那么这个式子肯定不成立,输出1.如果q-k小于等于q的一半即k 大于s串的一半长度,那么q-k就是最小循环节的长度
49             printf("%d\n", q / (q - k));
50         else
51             printf("1\n");
52     }
53     return 0;
54 }
时间: 2024-08-11 01:34:47

Power Strings POJ - 2406的相关文章

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: 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

G - Power Strings POJ 2406 (字符串的周期)

G - 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

(求循环节的个数)Power Strings -- poj -- 2406

链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&qu

Power Strings (poj 2406 KMP)

Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   Accepted: 13804 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def"

Power Strings POJ - 2406,字符串hash

题目链接:POJ - 2406 题目描述 定义两个字符串s1和s2的乘积s1*s2为将s1和s2连结起来得到的字符串. 例如:s1="xy",s2="z",那么s1*s2="xyz". 由此可以定义s1的幂次:s1^0="",s1^n=s1*s1^(n-1),n>0. 输入 输入包含多组测试数据. 每组数据由一行构成,包含一个字符串s. 输入数据以"."结束. 输出 对于每组输入数据输出一行,找出最大

Power Strings POJ 2406【KMP Next的应用】

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

Power Strings POJ - 2406 后缀数组

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 defin

Power Strings - POJ 2406(求循环节)

题目大意:叙述的比较高大上,其实就是一个字符串B = AAAAAAA,求出来这个A最短有多长   分析:注意如果这个串不是完全循环的,那么循环节就是就是它本身.   代码如下: #include<stdio.h> #include<string.h> const int MAXN = 1e6+7; const int oo = 1e9+7; char s[MAXN]; int next[MAXN]; void GetNext(int N) { int i=0, j=-1; next

Power Strings POJ - 2406(next水的一发 || 后缀数组)

后缀数组专题的 emm.. 就next 循环节../ 有后缀数组也可以做 从小到大枚举长度i,如果长度i的子串刚好是重复了len/i次,应该满足len % i == 0和rank[0] - rank[i] == 1(整个串的等级比 i位置开始的后缀的等级大1  (i位置开始的后缀即为比总串低一个等级的后缀)) 和height[rank[0]] == len-i (整个串 和 比它低一个等级的串的最长公共前缀的长度 是总长度减去这个循环节的长度)这些条件的 #include <iostream>