Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)

参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html

总结一下,如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且

循环节长度为:   i - next[i]

循环次数为:       i / ( i - next[i] )

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 #pragma comment(linker, "/STACK:102400000,102400000")
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define E(x)        (1 << (x))
31 #define iabs(x)     (x) < 0 ? -(x) : (x)
32 #define OUT(x)  printf("%I64d\n", x)
33 #define lowbit(x)   (x)&(-x)
34 #define Read()  freopen("a.txt", "r", stdin)
35 #define Write() freopen("b.txt", "w", stdout);
36 #define maxn 1000000000
37 #define N 2510
38 #define mod 1000000000
39 using namespace std;
40
41 char a[1000010];
42 int p[1000010];
43 void next(int l)
44 {
45     int j=0;
46     p[1]=0;
47     for(int i=2;i<=l;i++)
48     {
49         while(j>0&&(a[j+1]!=a[i])) j=p[j];
50         if(a[j+1]==a[i]) j+=1;
51         p[i]=j;
52     }
53 }
54 int main()
55 {
56     //freopen("a.txt","r",stdin);
57     int n,j=1;
58     while(~scanf("%d",&n))
59     {
60         if(n==0) break;
61         printf("Test case #%d\n",j++);
62         scanf("%s",a+1);
63         int l=strlen(a+1);
64         next(l);
65         for(int i=1;i<=l;i++)
66         {
67             if(i%(i-p[i])==0&&p[i]!=0)
68             {
69                 printf("%d %d\n",i,i/(i-p[i]));
70             }
71         }
72         printf("\n");
73     }
74     return 0;
75 }
时间: 2024-08-26 18:19:27

Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)的相关文章

UVALive - 3026 - Period (KMP)

UVALive - 3026 Period Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want

hdu1358 KMP求字符串最小循环节

对于一个字符串S,长度为L,如果由长度为len的字符串s(字符串s的最小循环节是其本身)循环k次构成,那么字符串s就是字符串S的最小循环节 那么字符串有个很重要的性质和KMP挂钩,即  i - next[i] 为字符串s的长度 i%(i - next[i]) ==0 证明:字符串S由s循环k次构成,那么有S[0-->L-len-1] == S[len-->L-1],即前k-1个循环节和后k-1个循环节构成的字符串相等 那么此时KMP数组的next[L] = k-1个循环节的长度, 也即 nex

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the

codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w

UVA - 10298 Power Strings (KMP求字符串循环节)

Description Problem D: Power Strings 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, exponentiati

【暑假】[实用数据结构]UVAlive 3026 Period

UVAlive 3026 Period 题目: Period Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive),

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

KMP解决字符串最小循环节相关问题

经典问题 : 给出一个由某个循环节构成的字符串,要你找出最小的循环节,例如 abababab 最小循环节当是 ab ,而类似 abab 也可以成为它的循环节,但并非最短. 分析 : 对于上述问题有两个结论 如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且 循环节长度为:    i - next[i] 循环次数为:       i / ( i - next[i] ) 水平有限,用自己的语言描述怕

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 = "