HDU 2594 Simpsons ' Hidden talents (The Simpsons ' potential talent)

Source: Internet
Author: User

HDU 2594 Simpsons ' Hidden Talents (The Simpsons potential talent)

Time limit:2000/1000 MS (java/others)

Memory limit:32768/32768 K (java/others)

"Description"

"Title description"

Homer: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 Clinton ' s name t Hat 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.

Homer: Maggie, it suddenly occurred to me that there was a way to explore our potential.

Maggie: Well, what's that?

Homer: For example I want to know if I have the potential to wade officialdom ...

Maggie: Yes.

Homer: So I wrote a politician's name, like Clinton, and then found the maximum length of the prefix of his name that matched my name suffix. It means how much I want to be a politician like Clinton.

Maggie: So why on earth is the longest prefix suffix???

Homer: After all, everyone is a real luxiang, Maggie.

Maggie: What about your results?

Homer: 0!

Maggie: no surprise.

Homer: But I know, you must have a potential mathematical talent.

Maggie: Where did you get that?

Homer: Riemann and Marjorie match 3!!!

Maggie: Who is Riemann?

Homer: I don't understand.

Tap a program, for the given string S1 and S2, find the maximum matching length of the S1 prefix with the S2 suffix.

"Input"

Input

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

The input has two lines. The first line is S1, and the second line is S2. You can think of only lowercase letters.

"Output"

Output

Output consists of a single line this contains the longest string that was a prefix of S1 and a suffix of S2, followed by t He length of that 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.

Outputs a row, including a string that matches the longest S1 prefix to the S2 suffix, and outputs the length of the prefix. If the string does not exist, the output is 0.

The length of S1 and S2 is less than 50000.

"Sample Input-Enter sample"

"Sample output-Output sample"

Clinton

Homer

Riemann

Marjorie

0

Rie 3

Exercises

KMP Compare parts of the deformation, using the next array to move forward S1, matching S2 to save the last match length.

"Code C + +"

1#include <cstdio>2#include <cstring>3 #defineMX 500054 CharS1[MX], s2[mx];5 intNEXTS1[MX], opts2[mx], s1ed, s2ed;6 voidRdy () {7     inti =0, J;8nexts1[0] = j =-1;9      while(I <s1ed) {Ten         if(j = =-1|| S1[i] = = S1[j]) Nexts1[++i] = + +J; One         Elsej =-1; A     } - } - intcount () { the     inti =0, j =0; -      while(I <s2ed) { -         if(j = =-1|| S2[i] = = S1[j]) Opts2[++i] = + +J; -         Elsej =Nexts1[j]; +     } -     returnopts2[s2ed]; + } A intMain () { at     intLen, I; -      while(~SCANF ("%s%s", S1, S2)) { -s1ed = strlen (S1); s2ed =strlen (S2); - Rdy (); -         if(len =count ()) { -              for(i =0; i < Len; Putchar (s1[i++])); inprintf"%d\n", Len); -         } to         ElsePuts"0"); +     } -     return 0; the}

HDU 2594 Simpsons ' Hidden talents (The Simpsons ' potential talent)

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.