Poj 1961 KMP

题意:给定一个字符串,求他每一个前缀,如果他是周期的,求len/最短循环节。

分析:

复习一下KMP,之前有详细解析。

由于朴素匹配每次移动一位,KMP可以多移动 f[i] 位,f 就是失配函数,失配函数怎么得到,是通过模式串自己匹配自己得到。

地推 f[i+1] ,如果 i+1 失配,那么先看前一个数字,他是否适合,不适合,继续向前。

本题:

应用 f,如果字符串周期,那么 他的最短循环节 可以通过 f 得到。

f[i] : 根据定义, 前缀 和后缀的最长公共长度,那么 i - f[i] 就是最短循环节。

需要注意的是,i+1,!!!(自己体会)

 1 #include <cstdio>
 2 #include <algorithm>
 3
 4 using namespace std;
 5
 6 const int maxn = 1000000 + 5;
 7 char P[maxn];
 8 int f[maxn];
 9
10 int main()
11 {
12     int n,kase = 1;
13     while(scanf("%d",&n),n) {
14         scanf("%s",P);
15         f[0] = 0;f[1] = 0;
16         for(int i=1;i<n;i++) {
17             int j = f[i];
18             while(j&&P[i]!=P[j])
19                 j = f[j];
20             f[i+1] = (P[i] == P[j] ? j+1:0);
21         }
22
23         printf("Test case #%d\n",kase++);
24         for(int i=2;i<=n;i++) {
25             if(f[i]>0&&i%(i-f[i])==0)
26                 printf("%d %d\n",i,i/(i-f[i]));
27         }
28         puts("");
29
30     }
31     return 0;
32 }

时间: 2024-11-04 13:48:16

Poj 1961 KMP的相关文章

poj 1961 KMP运用之循环节的问题

#include<stdio.h> #include<string.h> #include<iostream> using namespace std; char str[1000010]; int next[1000010]; int get_next(int len) { next[0]=-1; int i=0,k=-1; while(i<len) { if(k==-1||str[i]==str[k]) { i++; k++; next[i]=k; } els

Period POJ - 1961

Period POJ - 1961 时限: 3000MS   内存: 30000KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 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 prefix is a per

(简单) POJ 1961 Period,扩展KMP。

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 prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the larges

【poj 1961】Period(字符串--KMP循环节)

题意:给你一个字符串,求这个字符串到第 i 个字符为止的重复子串的个数. 解法:判断重复子串的语句很重要!!if (p && i%(i-p)==0) printf("%d %d\n",i,i/(i-p)); 我之前一直不是很理解,而实际上多枚举几种情况就好了.若是重复的,那么next[i]肯定是最大值,值余下一个循环节不同:而若不是,next[i]表示的前缀和后缀串的和重叠部分不一样以外的部分就肯定空出来,不能整除的.(P.S.我在说些什么......m(._.)m)

Period (poj 1961&amp;&amp;hdu 1358)KMP

Language: Default Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 13504   Accepted: 6365 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 t

POJ 1961 Period (KMP)

解题思路: 利用next 数组的性质求解重复子串.循环节的长度为i - next[i]; #include <iostream> #include <cstring> #include <cstdlib> #include <vector> #include <cmath> #include <algorithm> #include <cstdio> using namespace std; const int maxn

poj 1961 Period(KMP求周期)

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 15963   Accepted: 7644 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

POJ 1961 Period

Period Time Limit: 3000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 196164-bit integer IO format: %lld      Java class name: Main For each prefix of a given string S with N characters (each character has an ASCII code bet

POJ Oulipo KMP 模板题

http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41051   Accepted: 16547 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a mem