LA 3026(Period-MP算法)[Template:KMP]

3026 - Period

Time limit: 3.000 seconds

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 largest K > 1 (if there is
one) such that the prefix of S with length i can be written as AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 ≤ N ≤ 1 000 000) the size of the string S. The second line contains the string S. The input file ends with a line, having the number zero on
it.

Output

For each test case, output ‘Test case #‘ and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes
must be in increasing order. Print a blank line after each test case.

Sample Input

3
aaa
12
aabaabaabaab
0

Sample Output

Test case #1
2 2
3 3

Test case #2
2 2
6 2
9 3
12 4

本题用了MP算法

显然i能和f[i]匹配,那么字符串刚好可以连着等下去

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
// kmp
class KMP
{
public:
	int f2[MAXN]; //字符串从0开始,但是f[i]表示匹配第i个字符,前面留一个 f[0]--a-->f[1]--...这样的
	char T2[MAXN],P2[MAXN]; //T is long,P is model str
	void mem(){MEM(f2) MEM(T2) MEM(P2)	}
	int getFail(char *P=0,int* f=0)
	{
		if (P==0) P=P2;if (f==0) f=f2;
		int m=strlen(P);
		f[0]=f[1]=0;
		For(i,m-1)
		{
			int j=f[i];
			while(j&&P[i]!=P[j]) j=f[j];
			f[i+1]= P[i] == P[j] ? j+1 : 0;
		}
	}
	int find(char* T=0,char* P=0,int* f=0)
	{
		if (T==0) T=T2;if (P==0) P=P2;if (f==0) f=f2;
		int n=strlen(T),m=strlen(P);
		getFail(P,f);
		int j=0;
		Rep(i,n)
		{
			while(j&&T[i]!=P[j]) j=f[j];
			if (T[i]==P[j]) j++;
			if (j==m) return i-m+1;
		}
	}
}S;
int n;
int main()
{
//	freopen("la3026.in","r",stdin);
//	freopen(".out","w",stdout);
	int tt=0;
	while(scanf("%d%s",&n,S.P2)==2)
	{
		printf("Test case #%d\n",++tt);
		S.getFail(S.P2,S.f2);
		Fork(i,2,n)
		{
			if (i%(i-S.f2[i])==0&&S.f2[i]) printf("%d %d\n",i,i/(i-S.f2[i]));
		}
		putchar('\n');
		S.mem();
	}

	return 0;
}
时间: 2024-08-09 15:15:00

LA 3026(Period-MP算法)[Template:KMP]的相关文章

LA 3026 Period (strings)

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 to know whether

LA 3026 Period

这只是蓝书上的一道KMP水题...然后对于最长前缀的循环证明我就不说了... #include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<algorithm> #define ll long long #define maxn 1000005 using namespace std; int f[maxn],n; char s[maxn]; in

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

【暑假】[实用数据结构]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),

BF算法与KMP算法

BF(Brute Force)算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T的第二个字符:若不相等,则比较S的第二个字符和T的第一个字符,依次比较下去,直到得出最后的匹配结果. BF算法实现: 1 int BF(char S[],char T[],int pos) 2 {//c从第pos位开始搜索匹配 3 int i=pos,j=0; 4 while(S[i+j]!='\0'&&T[j]!='\0')

经典串匹配算法(KMP)解析

一.问题重述 现有字符串S1,求S1中与字符串S2完全匹配的部分,例如: S1 = "ababaababc" S2 = "ababc" 那么得到匹配的结果是5(S1中的"ababc"的a的位置),当然如果S1中有多个S2也没关系,能找到第一个就能找到第二个.. ------- 最容易想到的方法自然是双重循环按位比对(BF算法),但在最坏的情况下BF算法的时间复杂度达到了m * n,这在实际应用中是不可接受的,于是某3个人想出来了KMP算法(KMP

MP算法、OMP算法及其在人脸识别的应用

主要内容: 1.MP算法 2.OMP算法 3.OMP算法的matlab实现 4.OMP在压缩感知和人脸识别的应用 一.MP(Matching Pursuits)与OMP(Orthogonal Matching Pursuit)算法 内容:稀疏信号的表示(字典.稀疏系数).MP算法.MP算法的缺点.OMP.OMP的实现 参考文章:http://blog.csdn.net/scucj/article/details/7467955 二.OMP的matlab实现 %A-稀疏系数矩阵%D-字典/测量矩阵

字符串匹配——朴素算法、KMP算法

字符串匹配(string match)是在实际工程中经常会碰到的问题,通常其输入是原字符串(String)和子串(又称模式,Pattern)组成,输出为子串在原字符串中的首次出现的位置.通常精确的字符串搜索算法包括朴素搜索算法,KMP, BM(Boyer Moore), sunday, robin-karp 以及 bitap.下面分析朴素搜索算法和KMP这两种方法并给出其实现.假设原字符T串长度N,子串P长度为M. 1.NAIVE-STRING-MATCHING. 朴素算法,该方法又称暴力搜索,

算法学习-KMP(字符串匹配)解释

KMP算法 BF算法 BF算法就是我们最基本的求解字符串匹配的算法,算法的时间复杂度为O(M*N),空间复杂度为O(1),具体过程如下: 串 第一次 第二次 第三次 第四次 模式串S[i] abcababc abcababc abcababc abcababc 匹配串T[j] ababc ababc ababc ababc 可以看到在第三次匹配失败的时候,我们要回溯,直接S串直接i+=1,然后T串j=0从头继续开始.这样复杂度就比较高了. KMP算法 而KMP算法就是为了解决BF算法的复杂度比