3409: [Usaco2009 Oct]barn Echoes Bullpen Echo

Source: Internet
Author: User
Tags first string

3409: [Usaco2009 Oct]barn Echoes bullpen echo time limit:3 Sec Memory limit:128 MB
submit:57 solved:47
[Submit] [Status] [Discuss] Description

The cows often enjoy the moo in the barn because they can hear the echoes of their moo. Although sometimes it is not possible to fully hear the full echo. Bessie used to be an excellent secretary, so she accurately recorded all the screams and their echoes. She wondered how long the repetition of the two voices was.

Enter two strings (1 to 80 letters long) to represent two Moo calls. You want to determine the length of the longest repeating part. A repeating part of two strings refers to a string that is both a prefix of a string and a suffix of another string.

We use an example to understand the topic. Consider the following two Moo sounds:

    Moyooyoxyzooo    Yzoooqyasdfljkamo

The last part of the first string "Yzooo" repeats with the first part of the second string. The last part of the second string "Mo" repeats with the first part of the first string. So "yzooo" and "Mo" are the repeating parts of these 2 strings. Among them, "yzooo" is longer, so the length of the longest repeating part is 5.

Input

Two lines: Each row is a 1 string representing the cow's moo sound or its echo.

Output

First line: Contains a single integer representing the length of the longest repeating part of the input 2 string, one string prefix and the suffix of the other string.

Sample Input
Abcxxxxabcxabcd
Abcdxabcxxxxabcx
Sample Output11

"Abcxxxxabcx" is the prefix for the first string and a suffix for the second string.
HINT Source

Gold

The problem: I don't know when bzoj come out of a bunch of universal group topics Qaq

It's so easy to think of a, so it's just a little bit of a hole in the head.

Method one: Direct \ (O\left ({n}^{2} \right) \) Blind engage ... (PS: Don't ask me why there is only one layer of loops, in fact the complexity of copy is O (N))

1/**************************************************************2Problem:34093 User:hansbug4 language:pascal5 result:accepted6Time:0Ms7Memory:416KB8****************************************************************/9  Ten var One I,j,k,l,m,n:longint; A s1,s2,s3:ansistring; - functionMax (x,y:longint): Longint; -          begin the               ifX>y ThenMax:=xElsemax:=y; -          End; - begin - readln (S1); +READLN (S2); l:=0; -       forI:=max (Length (S2)-length (S1) +1,1) toLength (S2) Do +          begin AS3:=copy (s2,i,length (S2) +1-i); at               ifCopy (S1,1, Length (S2) +1-I.) =S3 Then -                  begin -L:=length (S2) +1-i; - Break ; -                  End; -          End; in       forI:=max (Length (S1)-length (S2) +1,1) toLength (S1) Do -          begin toS3:=copy (S1,i,length (S1) +1-i); +               ifCopy (S2,1, Length (S1) +1-I.) =S3 Then -                  begin theL:=max (L,length (S1) +1-i); * Break ; $                  End;Panax Notoginseng          End; - Writeln (l); the Readln; + End.

Method Two: This is my first reaction to the practice (but n<=80 is what rhythm = =)--string hash (hash Dafa good OTL), so blind to engage, very basic ... (I measured n<=3000000 can be 1s inside out)

1/**************************************************************2Problem:34093 User:hansbug4 language:pascal5 result:accepted6Time:0Ms7Memory:4944KB8****************************************************************/9  Ten Constp=314159; q=951413; One var A I,j,k,l,m,n,x0,y0,x,y:longint; -List,a,b:Array[0..5000000,1..2] ofInt64; - s1,s2:ansistring; the functionMax (x,y:longint): Longint; -          begin -               ifX>y ThenMax:=xElsemax:=y; -          End; +   - begin +list[0,1]:=1; list[0,2]:=1; A readln (S1); atREADLN (S2); l:=0; -N:=max (Length (S1), Length (S2)) +1; -       fori:=1  toN Do -          begin -List[i,1]:= (list[i-1,1]*P)MoDQ; -List[i,2]:= (list[i-1,2]*Q)MoDp; in          End; -a[0,1]:=0; a[0,2]:=0; to       fori:=1  toLength (S1) Do +          begin -A[i,1]:= (a[i-1,1]+ (List[i,1]*ord (S1[i]))MoDQMoDQ; theA[i,2]:= (a[i-1,2]+ (List[i,2]*ord (S1[i]))MoDPMoDp; *          End; $b[0,1]:=0; b[0,2]:=0;Panax Notoginseng       fori:=1  toLength (S2) Do -          begin theB[i,1]:= (b[i-1,1]+ (List[i,1]*ord (S2[i]))MoDQMoDQ; +B[i,2]:= (b[i-1,2]+ (List[i,2]*ord (S2[i]))MoDPMoDp; A          End; the       forI:=max (1, Length (S1)-length (S2) +1) toLength (S1) Do +          begin -J:=length (S1)-i+1; $X:= (list[i-1,1]*b[j,1])MoDQ; $Y:= (list[i-1,2]*b[j,2])MoDp; -X0:= (A[length (S1),1]-a[i-1,1])MoDQ+Q)MoDQ; -Y0:= (A[length (S1),2]-a[i-1,2])MoDP+P)MoDp; the               if(x=x0) and(Y=Y0) Then -                  beginWuyil:=J; the Break ; -                  End; Wu          End; -       forI:=max (1, Length (S2)-length (S1) +1) toLength (S2) Do About          begin $J:=length (S2)-i+1; -X:= (list[i-1,1]*a[j,1])MoDQ; -Y:= (list[i-1,2]*a[j,2])MoDp; -x0:= (b[length (S2),1]-b[i-1,1])MoDQ+Q)MoDQ; Ay0:= (b[length (S2),2]-b[i-1,2])MoDP+P)MoDp; +               if(x=x0) and(Y=Y0) Then the                  begin -l:=Max (j,l); $ Break ; the                  End; the          End; the Writeln (l); the Readln; - End.

3409: [Usaco2009 Oct]barn Echoes Bullpen Echo

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.