[Kuangbin take you to fly] KMP & topic sixteen extended KMP & Manacher J - Simpsons' Hidden Talents HDU - 2594 (kmp prefixes and suffixes)

J - Simpsons' Hidden Talents HDU - 2594

Topic links: https://vjudge.net/contest/70325#problem/J

topic:

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.

InputInput consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.OutputOutput 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 
meaning of the questions: give you two strings, find a prefix of the first string and second string is a prefix of suffix, if not, output -1;
ideas: You can re-establish a strings, these strings into two, with a middle! To connect, connected prevented prefix, then nextt [len] is equal to the prefix and suffix put together the string, len is the length of the piece of string,
// 
// Created by HJYL on 2019/8/16.
//
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
char str[maxn],str1[maxn],str2[maxn];
int nextt[maxn];
void getnext()
{
    int i=0,j=-1;
    nextt[0]=-1;
    int len=strlen(str2);
    while(i<len)
    {
        if(j==-1||str2[i]==str2[j])
        {
            i++,j++;
           // if(str2[i]!=str2[j])
                nextt[i]=j;
          //  else
               // nextt[i]=nextt[j];
        } else
            j=nextt[j];
    }
}

int main()
{
   // freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text","r",stdin);
    while(~scanf("%s%s",str,str1))
    {
        int len=strlen(str);
        int len1=strlen(str1);
        int pos=0;
        for(int i=0;i<len;i++)
            str2[pos++]=str[i];
        str2[pos++]='!';
        for(int i=0;i<len1;i++)
            str2[pos++]=str1[i];
        getnext();
//        cout<<len2<<"=len2"<<endl;
//        cout<<nextt[len2]<<"hhh"<<endl;
//        cout<<"str2="<<str2<<endl;
        if(nextt[pos]<=0)
            printf("0\n");
        else {
            for (int i = 0; i < nextt[pos]; i++)
                printf("%c", str2[i]);
            printf(" %d\n", nextt[pos]);
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/11365307.html