UVa 1328 (KMP求字符串周期) Period

当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错。

而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是一致的。

 1 #include <cstdio>
 2
 3 const int maxn = 1000000 + 10;
 4 char s[maxn];
 5 int f[maxn]; //失配函数
 6
 7 int main()
 8 {
 9     //freopen("in.txt", "r", stdin);
10
11     int n, kase = 0;
12     while(scanf("%d", &n) == 1 && n)
13     {
14         getchar();
15         for(int i = 0; i < n; i++) s[i] = getchar();
16
17         //递推求解失配函数
18         f[0] = f[1] = 0;
19         for(int i = 1; i < n; i++)
20         {
21             int j = f[i];
22             while(j && s[i] != s[j]) j = f[j];
23             f[i+1] = s[i] == s[j] ? j+1 : 0;
24         }
25
26         printf("Test case #%d\n", ++kase);
27         for(int i = 2; i <= n; i++)
28             if(f[i] && i % (i - f[i]) == 0)
29                 printf("%d %d\n", i, i / (i - f[i]));
30         printf("\n");
31     }
32
33     return 0;
34 }

代码君

时间: 2024-08-06 02:40:15

UVa 1328 (KMP求字符串周期) Period的相关文章

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

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

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

uva 1328(kmp)

被打哭了......神特么kmp,还能这么用!看来我理解的还不够透彻,这里最神奇的就是只有相同的字串才能往下延伸,而"i-f[i]"就正好是前面空出来的模板块,也就是f[i]中不计数的那块,而字符串都是从0开始的,对应的往下加一个却正好成立构成倍数关系 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace

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

kmp算法--求字符串子串--《数据结构》严蔚敏

// exam1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using namespace std; void get_next(int* &next,char* s) { int j=0; int i=1; int len=strlen(s); next=(int*)malloc(len*sizeof(int)); memset(next,0,len*sizeof(int));

迭代加深搜索求字符串最小周期

1 //==================================================== 2 //迭代加深搜索求字符串最小周期: 3 //==================================================== 4 5 #include <stdio.h> 6 #include <Windows.h> 7 #include <stdlib.h> 8 9 int main() 10 { 11 char *str; 1

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

poj3080(Blue Jeans)kmp求多个串公共子串

题意:给出1-10个长度为60的字符串,求出最长的公共子串(长度不能小于3),如果有多个一样长的,输出字典序最短的. 解法:想到kmp时,自己第一反应枚举第一个串的所有子串,在其他所有串中走一遍kmp,复杂度为10*60*60*60,但是发现只需枚举第一个串后缀就可以,每次枚举记录在所有串能走最远中走的最短的那个长度.这样复杂度就成了10*60*60,0ms AC. 代码: /**************************************************** * autho