POJ topic 2774 Long long Message (suffix array, longest common substring length)

Source: Internet
Author: User

A long long Message
Time Limit: 4000MS Memory Limit: 131072K
Total Submissions: 23696 Accepted: 9705
Case Time Limit: 1000MS

Description

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days:his mother is getting ill. Being worried about spending so much in railway tickets (Byterland is such a big country, and he have to spend all shours on Train to him hometown), he decided only to send SMS with his mother.

The little cat lives in a Unrich family, so he frequently comes to the Mobile service center, to check how much money he Have spent on SMS. Yesterday, the computer of service center was broken, and printed, very long messages. The brilliant little cat soon found out:

1. All characters in messages is lowercase Latin letters, without punctuations and spaces.
2. All SMS have been appended other– (i+1)-th SMS comes directly after the i-th one–that are why those Es is quite long.
3. His own SMS have been appended together, but possibly a great many redundancy characters appear leftwards and rightwards Due to the broken computer.
E.g:if his SMS was "motheriloveyou", either long message printed by, the would possibly be one of "Hahamotherilov Eyou "," motheriloveyoureally "," Motheriloveyouornot "," bbbmotheriloveyouaaa ", etc.
4. For these broken issues, the little cat have printed his original text twice (so there appears, very long messages). Even though the original text remains the same in both printed messages, the redundancy characters on both sides would is P Ossibly different.

You are given those-very long messages, and you had to output the length of the longest possible original text Writte n by the little cat.

Background:
The SMS in Byterland Mobile service is charging in Dollars-per-byte. That's why the little cat was worrying about how long could the longest original text being.

Why do I ask you to write a program? There is four resions:
1. The little cat is so busy these days with physics lessons;
2. The little cat wants to keep what he said to his mother Seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some money from POJ, and try to persuade he mother to see the Doctor:(

Input

The strings with lowercase letters on the input lines individually. Number of characters in each one would never exceed 100000.

Output

A number–what is the maximum length of the original text written by the little cat.

Sample Input

Yeshowmuchiloveyoumydearmotherreallyicannotbelieveityeaphowmuchiloveyoumydearmother

Sample Output

27

Source

POJ Monthly--2006.03.26,zeyuan Zhu, "Dedicate to my great beloved mother."

AC Code

/*problem:2774  user:kxh1995 memory:5024k  time:891ms language:c++  result:accepted * *

#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #define MIN (A, b  ) (A&GT;B?B:A) using namespace std;  Char str1[200100],str2[2000100];int sa[220020],c[220020],t2[220020];int t1[220020],s[200100]; int rank[220020],height[220020]; int len1,len2;      void Build_sa (int s[],int n,int m) {int i,j,p,*x=t1,*y=t2;      for (i=0;i<m;i++) c[i]=0;      for (i=0;i<n;i++) c[x[i]=s[i]]++;      for (i=1;i<m;i++) c[i]+=c[i-1];      for (i=n-1;i>=0;i--) sa[--c[x[i]]]=i;          for (j=1;j<=n;j<<=1) {p=0;          for (i=n-j;i<n;i++) y[p++]=i;          for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;          for (i=0;i<m;i++) c[i]=0;          for (i=0;i<n;i++) c[x[y[i]]]++;          for (i=1;i<m;i++) c[i]+=c[i-1];         for (i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; Swap (x, y);          P=1;          x[sa[0]]=0;          for (i=1;i<n;i++) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;          if (p>=n) break;      M=p;      }} void GetHeight (int s[],int n) {int i,j,k=0;      for (i=0;i<=n;i++) rank[sa[i]]=i;          for (i=0;i<n;i++) {if (k) k--;          J=SA[RANK[I]-1];          while (S[i+k]==s[j+k]) k++;      Height[rank[i]]=k; }}int judge (int len,int k) {int i;for (i=1;i<len;i++) {if (height[i]>=k) {if (sa[i]>len1&&sa[i-1]<= LEN1) return 1;if (SA[I-1]&GT;LEN1&AMP;&AMP;SA[I]&LT;=LEN1) return 1;}} return 0;} int main () {while (scanf ("%s%s", STR1,STR2)!=eof) {int I;len1=strlen (STR1); Len2=strlen (str2); for (i=0;i<len1;i++) { s[i]=str1[i]-' a ' +1;} S[len1]=27;int n=len1+1;for (i=0;i<len2;i++) s[n++]=str2[i]-' a ' +1;s[n]=0;build_sa (s,len1+len2+3,28); GetHeight ( s,len1+len2+2); int l=0,r=min (LEN1,LEN2), Ans=0;while (l<=r) {iNT mid= (L+R) >>1;if (judge (Len1+len2+2,mid)) {ans=mid;l=mid+1;} Elser=mid-1;} printf ("%d\n", ans);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ topic 2774 Long long Message (suffix array, longest common substring length)

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.