码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 2594 Simpsons’ Hidden Talents (KMP)

时间:2019-05-13 21:34:22      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:bst   style   while   bsp   nes   bit   一个   desc   sample   

 

链接: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)

标签:bst   style   while   bsp   nes   bit   一个   desc   sample   

原文地址:https://www.cnblogs.com/whdsunny/p/10859074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!