poj 2406 Power Strings(kmp循环节)

题目链接:http://poj.org/problem?id=2406

题目大意:如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]。

例如:      a    b    a    b    a    b

next:-1   0    0    1    2    3    4

next[n]==4,代表着,前缀abab与后缀abab相等的最长长度,这说明,ab这两个字母为一个循环节,长度=n-next[n];

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

poj 2406 Power Strings(kmp循环节)

时间: 2024-10-11 05:52:16

poj 2406 Power Strings(kmp循环节)的相关文章

poj 2406 Power Srings (kmp循环节) (经典)

<题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length==0,那么周期就为len/lenght,如果不能整除,则说明该字符串的字串不具有周期性,输出1. KMP最小循环节的证明 >>> #include <cstdio> #include <cstring> const int maxn = 1000000 + 100;

poj 2406 Power Strings KMP匹配

对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],假设t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比如字符串"abababab", a  b a b a b a b * next     -1 0 1 2 3 4 5 6  7 考虑这样的模式匹配,将"abababab#"当做主串,"abababab*"当做模式串,于是进行匹配到n(n=8)时,出现了不匹配: 主串   

POJ 2406 Power Strings KMP运用题解

本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都觉得是可以利用next数组的,但是自己想了很久没能这么简洁地总结出来,也只能查查他人代码才恍然大悟,原来可以这么简单地区求一个周期字符串的最小周期的. 有某些大牛建议说不应该参考代码或者解题报告,但是这些大牛却没有给出更加有效的学习方法,比如不懂KMP,难倒不应该去看?要自己想出KMP来吗?我看不太可能有哪位大牛可以直接自己"重新创造出KMP"来吧. 好吧,不说"创造KMP

poj 2406 Power Strings(KMP&amp;思维)

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

poj 2406 Power Strings(kmp的nxt数组找最小循环节)

题目链接:poj 2406 Power Strings 题意: 给你一个字符串,让你找出这个字符串的最大循环次数,及最小循环节. 题解: 用kmp的nxt数组搞搞,L=j-nxt[j],为前缀j的最小循环节. 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define F(i,a,b) for(int i=(a);i<=(b);++i) 5 using namespace std;

POJ - 2406 Power Strings (KMP循环节)

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

poj 2406 Power Strings KMP

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

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

poj 2406 Power Strings(KMP求循环次数)

题目链接:http://poj.org/problem?id=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, e