"POJ2774" Long long Message (suffix array)

Source: Internet
Author: User

"POJ2774" Long long Message (suffix array) facet

Vjudge

Description

Little Cat is majoring in physics at the capital of Byterland. These days he received a sad message: his mother was ill. Worried about paying too much for the train ticket (Byterland is a huge country, so he takes 16 hours to get home by train), he decided to send text messages only to his mother.
Little Cat is not wealthy, so he often goes to the business hall to see how much money he spends on texting. The computer in the business Hall broke down yesterday and printed two very long messages. The witty little cat soon discovers:
1. All characters in the message are lowercase English letters with no punctuation or spaces.
2. All the text messages are linked together-section i+1 text messages are directly behind the text of article I-this is why these two messages are so long.
3. Although his text messages are linked together, they may have many redundant characters on either the left or the right because the computer is broken.
For example: If the text message is "Motheriloveyou", every piece of information printed by the computer may be "hahamotheriloveyou", "motheriloveyoureally", "Motheriloveyouornot", " Bbbmotheriloveyouaaa ", and so on.
4. Because of these messy problems, little cat prints two times (so there are two very long messages). Although the original text message is the same in both messages, the two messages may have different redundant characters on either side of the text.
Given these two very long messages, the output little cat writes the maximum possible length of the text of the original text message.
Background:
In Byterland, text messages are denominated in dollar/byte units. This is why little cat wants to know the maximum possible length of the original text.
Why do you want to write a program? There are four reasons:
1.little Cat is busy with his physics courses these days.
2.little cat doesn't want to reveal what he said to his mother.
3.POJ is a good website.
4.little Cat wants to earn some money from POJ and try to persuade his mother to go to the hospital

Input

Two lines of two strings made up of lowercase English letters. The string length is no more than 100000

Output

A line of integers, the longest possible length of the original text written by Little Cat.

Sample Input

Yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
Yeaphowmuchiloveyoumydearmother

Sample Output

27

Exercises

Connect the two strings.
Now the problem can be transformed into
Find two suffixes
Maximum number of public prefixes to ask for their longest

This maximum value is obviously a \ (height\) value
Because we're going to pick a string from each of the two strings.
So to determine if \ (sa[i]\) and \ (sa[i-1]\) are in two different strings

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <set>#include <map>#include <vector>#include <queue>using namespaceStd#define MAX 220000inline intRead () {intx=0, t=1;CharCh=getchar (); while((ch<' 0 '|| Ch>' 9 ') &&ch!='-') Ch=getchar ();if(ch=='-') t=-1, Ch=getchar (); while(ch<=' 9 '&&ch>=' 0 ') x=x*Ten+ch-48, Ch=getchar ();returnX*t;}CharCh[max];intSa[max],x[max],y[max],t[max];intRank[max],height[max],a[max];intN,N1,N2;BOOLcmpintIintJintK) {returnY[I]==Y[J]&AMP;&AMP;Y[I+K]==Y[J+K];}BOOLSame (intIintj) {return((I&LT;=N1) && (j<=n1)) | ((I&GT;N1) && (J&GT;N1));}voidGetsa () {intm= -; for(intI=1; i<=n;++i) t[x[i]=a[i]]++; for(intI=1; i<=m;++i) T[i]+=t[i-1]; for(inti=n;i>=1; i.) sa[t[x[i]]--]=i; for(intk=1; k<=n;k<<=1)    {intp=0; for(intI=0; i<=n;++i) y[i]=0; for(intI=n-k+1; i<=n;++i) y[++p]=i; for(intI=1; i<=n;++i)if(sa[i]>k) Y[++p]=sa[i]-k; for(intI=0; i<=m;++i) t[i]=0; for(intI=1; i<=n;++i) t[x[y[i]]]++; for(intI=1; i<=m;++i) T[i]+=t[i-1]; for(inti=n;i>=1;----) sa[t[x[y[i]]]--]=y[i];        Swap (x, y); x[sa[1]]=p=1; for(intI=2; i<=n;++i) x[sa[i]]=cmp (sa[i],sa[i-1],k)? p:++p;if(p>=n) Break;    M=p; } for(intI=1; i<=n;++i) rank[sa[i]]=i; for(intI=1, j=0; i<=n;++i) {if(j) j--; while(A[i+j]==a[sa[rank[i]-1]+J]) ++j;    Height[rank[i]]=j; }}intMain () {scanf ("%s"Ch+1); N=n1=strlen (Ch+1); for(intI=1; i<=n1;++i) A[i]=ch[i]-96; scanf"%s"Ch+1); for(intI=1, L=strlen (Ch+1); i<=l;++i) A[++n]=ch[i]-96;    N2=N-N1; Getsa ();intans=0; for(intI=2; i<=n;++i)if(!same (sa[i],sa[i-1])) Ans=max (Ans,height[i]); printf"%d\n", ans);return 0;}

"POJ2774" Long long Message (suffix array)

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.