Hdu 2594 Simpsons ' Hidden talents

Source: Internet
Author: User
Tags acos

Title Address: http://acm.hdu.edu.cn/showproblem.php?pid=2594

Topic:

Problem Descriptionhomer:marge, I just figured out a-to discover some of the talents we weren ' t aware we had.
Marge:yeah, what's it?
Homer:take me for example. I want to find out if I had 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 the Clinton ' s name is a suffix in my name. That's how close I'm to being a politician like Clinton
Marge:why on Earth Choose the longest prefix a suffix???
Homer:well, our talents is deeply hidden within ourselves, Marge.
Marge:so How close is you?
homer:0!
Marge:i ' m not surprised.
Homer:but know, you must has some real math talent hidden deep in.
Marge:how come?
Homer:riemann and Marjorie gives 3!!!
Marge:who the heck is Riemann?
Homer:never mind.
Write a program this, when given strings S1 and S2, finds the longest prefix of S1, which is a suffix of s2.

Inputinput consists of the lines. The first line contains S1 and the second line contains S2. Assume all letters is in lowercase.

Outputoutput consists of a single line that contains the longest string which is a prefix of S1 and a suffix of S2, followe D by the length of the. prefix. If the longest such string is the empty string and then the output should be 0.
The lengths of S1 and S2 would be in most 50000.

Sample Inputclintonhomerriemannmarjorie

Sample Output0rie 3

Idea: Slag residue I first wrote is KMP, later too troublesome, directly wrote a violence, and then 1a, or 390MS. (or violent Dafa is good, although it is likely to be T, but in the game when you do not have other ideas, you might as well, if the AC?) The fate of this matter who said clear? Manual blush. jpg)

Weak weak sentence: The data is really weak

Wait for the KMP to fill up the practice = =

#include <bits/stdc++.h>#definePB push_back#defineMP Make_pairusing namespaceStd;typedefLong LongLl;typedef pair<int,int>PII;#definePI ACOs ((double)-1)#defineE exp (double (1))#defineK 1000000+9intNt[k];stringSA,SB;intSolvevoid){    intmx=0;  for(intI=0, Len=min (Sa.length (), sb.length ()); i<len;i++)        if(Sa.compare (0, i+1, Sb,sb.length ()-i-1, i+1)==0) MX=max (mx,i+1); returnMX;}intMainvoid){     while(cin>>sa>>SB) {        intk=solve (); if(k) { for(intI=0; i<k;i++) printf ("%c", Sa[i]); printf ("%d\n", K); }        Elseprintf ("0\n"); }    return 0;}

KMP: See the prefix and suffix of the match must be KMP Ah, put SB in the back of the SA to find an next array can be = =

However, when Next[sa.length () -1]>min (Sa.length (), Sb.length ()) (the first SA is the stitched string, the latter two are the original string).

Do not short-fall back to the appropriate location.

See the code for details:

#include <bits/stdc++.h>#definePB push_back#defineMP Make_pairusing namespaceStd;typedefLong LongLl;typedef pair<int,int>PII;#definePI ACOs ((double)-1)#defineE exp (double (1))#defineK 1000000+9intNt[k];stringSA,SB;voidKmp_next (void) {nt[0]=0;  for(intI=1, j=0, Len=sa.length (); i<len;i++)    {         while(J&&sa[i]!=sa[j]) j=nt[j-1]; if(Sa[i]==sa[j]) J + +; Nt[i]=J; }}intMainvoid){     while(cin>>sa>>SB) {        intlen=min (Sa.length (), sb.length ()); SA+=SB;        Kmp_next (); intK=nt[sa.length ()-1];  while(K>len) k=nt[k-1]; if(k) { for(intI=0; i<k;i++) printf ("%c", Sa[i]); printf ("%d\n", K); }        Elseprintf ("0\n"); }    return 0;}

Hdu 2594 Simpsons ' Hidden talents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.