HDU-2595 Simpsons’ Hidden Talents(kmp)

Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.

Input

Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.

Output

Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
The lengths of s1 and s2 will be at most 50000.

Sample Input

clinton
homer
riemann
marjorie

Sample Output

0
rie 3

题目大意:字符串s和字符串p,求s的前缀和p的后缀的最大共同长度且输出,并且要输出相同的前后缀。

解题思路:第一种方法套用kmp的模板,我一开始错了一遍,这里应该是把s作为子串,p作为主串,

即kmp(p,s),是s的前缀去配p的后缀。

代码:

#include <stdio.h>
#include <string.h>
int next[50010];
char s[50010],p[50010];
 
void getnext(char p[])
{
	int lp=strlen(p);
	int j=0;
	int k=-1;
	next[0]=-1;
	while(j<lp)
	{
		if(k==-1||p[j]==p[k])
		{
			k++;
			j++;
			next[j]=k;
		}
		else
			k=next[k];
	} 
}
 
int kmp(char s[],char p[])
{
	getnext(p);
	int i=0;
	int j=0;
	int ls=strlen(s);
	int lp=strlen(p);
	while(i<ls)
	{
		if(j==-1||s[i]==p[j])
		{
			i++;
			j++;
		}
		else
			j=next[j];
	}
	return j;//返回的是匹配的最大长度 
}

int main ()
{ 
	int k,i;
    while(scanf("%s%s",s,p)!=EOF)
	{
        k=kmp(p,s);
        //printf("%d\n",k);
        if(k)
		{
            for(i=0;i<k;i++)
                printf("%c",s[i]);
            printf(" %d\n",k);
        }
        else 
			printf("0\n");
    }
    return 0;
}

方法二:用strcat()函数把p接到s后面,当成一个新串s0,求next数组,next[len](len是s0的长度)就是题目要输出的,

但是注意求出来的next[len]不能大于s的长度和p的长度。

代码:

#include <stdio.h>
#include <string.h>
char s[100010],p[50010];
int next[100010];

void getnext(char s[])
{
	int len=strlen(s);
	int j=0;
	int k=-1;
	next[0]=-1;
	while(j<len)
	{
		if(k==-1||s[j]==s[k])
		{
			k++;
			j++;
			next[j]=k;
		}
		else
			k=next[k];
	} 
}

int main()
{
	int ls,lp,len,n,i;
	while(scanf("%s%s",s,p)!=EOF)
	{
		ls=strlen(s);
		lp=strlen(p);
		strcat(s,p);
		len=strlen(s);
		getnext(s);
		n=next[len];
		if(n>ls)
			n=ls;
		if(n>lp)
			n=lp;
		if(n)
		{
			for(i=0;i<n;i++)
				printf("%c",s[i]);
			printf(" %d\n",n);
		}	
		else
			printf("0\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hello_cmy/article/details/81176018
今日推荐