Hdu 2594 Simpsons ' Hidden talents KMP

Source: Internet
Author: User

Simpsons ' Hidden Talents

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4683 Accepted Submission (s): 1702


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

SOURCEHDU 2010-05 Programming Contest test Instructions: given two strings S1 and S2, you want to find the longest prefix of S1, and this prefix is also S2 suffix. Puzzle: kmp compute the suffix of the string S2 to match the S1 prefix is how long
#include <cstdio>#include<string>#include<iostream>using namespacestd;intmaxl[50002],p[50002];intMain () {stringb;  while(cin>>b>>a) {b=" "+b; A=" "+A; intm=b.length (); intn=a.length (); N--; M--; Memset (P,0,sizeof(p)); memset (MAXL,0,sizeof(MAXL)); p[0]=p[1]=0; intj=0;  for(intI=2; i<=m;i++)        {             while(j>0&&b[j+1]!=b[i]) j=P[j]; if(b[j+1]==b[i]) J + +; P[i]=J; } J=0;  for(intI=1; i<=n;i++)        {             while(j>0&&b[j+1]!=a[i]) j=P[j]; if(b[j+1]==a[i]) J + +; Maxl[i]=J; }        if(maxl[n]==0) cout<<0<<Endl; Else {             for(intI=1; i<=maxl[n];i++) cout<<B[i]; cout<<" "<<maxl[n]<<Endl;    } getchar (); }    return 0;}
Code

Hdu 2594 Simpsons ' Hidden talents KMP

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.