Hdu 2594 Simpsons ' Hidden talents (KMP introduction)

Source: Internet
Author: User
Tags cmath

Simpsons ' Hidden Talents

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


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 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: Find two strings S1 with S2, s1 prefix with S2 suffix of the longest public part, and output the string and its length, if not, then direct output 0;

I can't believe I'm going to put two strings together and use next function ~

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstdlib>5#include <cstring>6#include <cmath>7#include <algorithm>8#include <cctype>9 #defineN 500015Ten #defineINF 1000000 One #definell Long Long A using namespacestd; - intnexts[50050*2]; - Charaa[50050*2],bb[50050]; the voidGet_next (Chara[]) - { -     intj =0, k =-1; -nexts[0] = -1; +      while(A[j]) -     { +         if(k = =-1|| A[J] = =A[k]) ANEXTS[++J] =++K; at         Else -K =Nexts[k]; -     } - } - intMainvoid) - { in      while(cin>>aa>>BB) -     { to         intLa = (int) strlen (AA); +         intLB = (int) strlen (BB); -strcat (AA,BB);//Connect two strings and find the two longest common parts by looking for a matching section the get_next (AA); *         intLen = la+lb; $          while(Nexts[len] > La | | nexts[len] > lb) len = Nexts[len];//if the matching portion exceeds the length of any one of the two strings, take the small onePanax Notoginsenglen = Nexts[len];//Fetch Match -          for(inti =0; i < Len; i++) printf ("%c", Aa[i]);//Output the         if(len) printf (" "); +printf"%d\n", Len); A     } the     return 0; +}

Hdu 2594 Simpsons ' Hidden talents (KMP introduction)

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.