HDU 1358 Period(kmp)

next数组的应用

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
using namespace std;
#define N 1000005

int len,next[N];
char a[N];

void getfail(char *a)
{
	int i,j;
	i=0;j=-1;
	next[0]=-1;

	while(i<len)
	{
		if(j==-1||a[i]==a[j])
		{
			i++;
			j++;
			next[i]=j;
		}
		else
			j=next[j];
	}
}

int main()
{
	int i,j,ca=0;
	while(~scanf("%d",&len),len)
	{
		scanf("%s",a);
		getfail(a);
		printf("Test case #%d\n",++ca);
		for(i=2;i<=len;i++)
		{
			if(next[i]&&i%(i-next[i])==0)
				printf("%d %d\n",i,i/(i-next[i]));
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-12-18 20:16:31

HDU 1358 Period(kmp)的相关文章

poj1961 &amp; hdu 1358 Period(KMP)

poj 题目链接:http://poj.org/problem?id=1961 hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 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

HDU - 1358 - Period (KMP)

Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4325    Accepted Submission(s): 2087 Problem Description For each prefix of a given string S with N characters (each character has an ASCI

HDU 1358 Period(kmp简单解决)

Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3196    Accepted Submission(s): 1603 Problem Description For each prefix of a given string S with N characters (each character has an ASCII

hdu 1686 Oulipo (kmp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int next[10010],sum,lens,lenc; 6 char str[10010],ch[1000010]; 7 8 int get_nex

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

HDU 5510 Bazinga(KMP)

Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4572    Accepted Submission(s): 1459 Problem Description Ladies and gentlemen, please sit up straight. Don't tilt your head. I'm serious.F

HDU 1686 Oulipo(kmp)

Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s'affirmait faux. Tout avait Fair

Period(kmp)

Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4437    Accepted Submission(s): 2145 Problem Description For each prefix of a given string S with N characters (each character has an ASCII

HDU 2594 Simpsons’ Hidden Talents (KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就可以做出来,不过我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. KMP算法: #include<stdio.h> #include<iostream> #include<string.h> using namespace std; int next[50001]; char p[50000],s[50000]; void getnex