HDOJ 2594 Simpsons’ Hidden Talents (KMP)

Posted whdsunny

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ 2594 Simpsons’ Hidden Talents (KMP)相关的知识,希望对你有一定的参考价值。

 

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594

 


 

 

题目:

Problem Description
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
 

 

题意:

给出s1,s2两个串 求出既为s1的前缀又为s2后缀的子串

 

思路:

把s1 s2两个串合并成一个新字符串S 把题目转换成求S串的既是前缀又是后缀的最长子串

因为是s1 s2串的子串 要留意在S串中求出来子串的长度是否超过s1和s2的长度 如果超过 就是直接将s1或s2串当作子串

 


 

 

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=5e4+10;
int nx[maxn*2];
string s,t;

void getnx(string S,int len){
    int j=0,k=-1;
    nx[0]=-1;
    while(j<len){
        if(k==-1 || S[j]==S[k]) nx[++j]=++k;
        else k=nx[k];
    }
}

int main(){
 //   freopen("1.in","r",stdin);
    while(cin>>s){
        cin>>t;
        memset(nx,0,sizeof(nx));
        string S=s+t;
        int len=S.length();
        getnx(S,len);
        if(nx[len]>0){
            if(nx[len]>min(s.length(),t.length())){
                if(s.length()>t.length()){
                    cout<<t<<" "<<t.length()<<endl;
                }
                else cout<<s<<" "<<s.length()<<endl;
            }
            else{
                string tmp=S.substr(0,nx[len]);
                int tlen=tmp.length();
                cout<<tmp<<" "<<tlen<<endl;
            }
        }
        else cout<<"0"<<endl;
    }
    return 0;
}

 

以上是关于HDOJ 2594 Simpsons’ Hidden Talents (KMP)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2594 Simpsons’ Hidden Talents

hdu2594 Simpsons' Hidden Talentsnext数组应用

HDU2594 Simpsons’ Hidden Talents

HDU 2594 - Simpsons’ Hidden Talents(KMP)

HDU 2594 Simpsons’ Hidden Talents (KMP)

HDU 2594 Simpsons’ Hidden Talents(KMP)