Simpsons’ Hidden Talents hdu 2594(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

题意:给你两串字符串a,b要你输出即是第一串的前缀又是第二串的后缀的最大子串以及子串的长度

思路一:直接将两串连接起来然后求一下next数组,next[len]的值就是子串的长度,但是要注意一个问题就是可能next[len]的值比两字符串的长度要长,那么这个next[len]的值就不正确了,我们只需记录一下两字符串长度中较小的那个输出即可。

思路二:把a字符串看成是模式串求得next数组,b字符串看成是文本串,跟next数组匹配,最后返回next数组的最后匹配下标即可,即文本串b的最后一位匹配到了模式串中的第几位,那么这就是相同的前后缀了。

//思路一
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std;
const int N=5e4+10;
int net[N*2],len;
char a[N],b[N*2];
void getnext()
{
    net[0]=-1;
    int k=-1,j=0;
    while(j<len)
    {
        if(k==-1||b[j]==b[k])
            net[++j]=++k;
        else k=net[k];
    }
}
int main()
{
    while(~scanf("%s%s",b,a))
    {
        int m=strlen(a),n=strlen(b);
        strcat(b,a);
        len=strlen(b);
        getnext();
        if(net[len]*2>len)
        {
            int t=min(m,n);
            for(int i=0;i<t;i++)
                printf("%c",b[i]);
            printf(" %d\n",t);
        }
        else if(net[len]==0) printf("0\n");
        else
        {
            for(int i=0;i<net[len];i++)
                printf("%c",b[i]);
            printf(" %d\n",net[len]);
        }
    }
}
//思路二
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std;
const int N=5e4+10;
int net[N];
char a[N],b[N],tmp[N],ans[N];
void getnext()
{
    net[0]=-1;
    int k=-1,j=0,len=strlen(b);
    while(j<len)
    {
        if(k==-1||b[j]==b[k])
            net[++j]=++k;
        else k=net[k];
    }
}
int kmp()
{
    int i=0,j=0,len=strlen(a);
    getnext();
    while(i<len)
    {
        if(j==-1||a[i]==b[j])
        {
            i++;
            j++;
        }
        else j=net[j];
    }
    return j;
}
int main()
{
    while(~scanf("%s%s",b,a))
    {
        int tmp=kmp();
        for(int i=0;i<tmp;i++)
            printf("%c",b[i]);
        if(tmp) printf(" ");
        printf("%d\n",tmp);
    }
}

猜你喜欢

转载自blog.csdn.net/never__give__up/article/details/80516354
今日推荐