POJ 2774 Long Long message (suffix array)

Source: Internet
Author: User
Tags lowercase

Long Long message

Time Limit: 4000MS Memory Limit: 131072K
Total submissions: 18965 accepted: 7818
Case Time Limit: 1000MS

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

The little cat lives in a Unrich family, so his frequently comes to the mobile service center. has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out:

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

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

Background:
The SMS in Byterland Mobile service are charging in Dollars-per-byte. This is why the little cat are worrying about how long could the longest original text be.

Why Ask and write a program? There are four resions:
1. The little cat is so busy, with physics lessons;
2. The little cat wants to keep what him said to his mother Seceret;
3. POJ is such a great Online Judge;
4. The little cat wants to earn some the POJ, and try to persuade his mother:(

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

Output a single line with a single integer number–what was the maximum length of the original text written by the little Cat.

Sample Input

Yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
Yeaphowmuchiloveyoumydearmother

Sample Output

27

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

The longest identical substring of 2 strings is asked:

Solution: Put 2 strings into one, separated by a character that doesn't appear in the middle, and then find the height array, iterate through it once, and then try to update the answer if the suffix array is adjacent to the 2 suffixes from different strings.


#include <stdio.h> #include <string.h> #define MAXN 200008 int RANKX[MAXN],RANKY[MAXN];
int SA[MAXN],HEIGHT[MAXN];
int BUCKET[MAXN],R[MAXN];
Char S1[MAXN],S2[MAXN]; int MIN (int x,int y) {return x<y?x:y;} int cmp (int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+
L];

    } void Suffix_array (int *r,int *sa,int n,int m) {int i,j,p,*x=rankx,*y=ranky,*t;
    for (i=0;i<m;i++) bucket[i]=0;
    for (i=0;i<n;i++) bucket[x[i]=r[i]]++;
    for (i=1;i<m;i++) bucket[i]+=bucket[i-1];
    for (i=n-1;i>=0;i--) sa[--bucket[x[i]]]=i;
        for (p=j=1;p<n;j*=2,m=p) {for (p=0,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++) bucket[i]=0;
        for (i=0;i<n;i++) bucket[x[y[i]]]++;
        for (i=1;i<m;i++) bucket[i]+=bucket[i-1];
        for (i=n-1;i>=0;i--) sa[--bucket[x[y[i]]]]=y[i];
        t=x,x=y,y=t; for (p=i=1,x[sa[0]]=0;i<n;i++) x[sa[i]]=cmp (Y,sa[i-1],sa[i],j)? p-1:p++;
    } void cul_height (int *r,int *height,int *sa,int *x,int n) {int i,j,k=0;
    for (i=0;i<=n;i++) x[sa[i]]=i;
        for (i=0;i<=n;height[x[i++]]=k) {if (x[i]==0) continue;
    for (k?k--:0,j=sa[x[i]-1];r[i+k]==r[j+k];k++);
    } int Check (int x,int len) {if (Sa[x]<len&&sa[x-1]>len) return 1;
    if (Sa[x]>len&&sa[x-1]<len) return 1;
return 0;

    int main () {int len1,len2,i,j,res,n;
        while (scanf ("%s%s", S1,s2) >0) {Len1=strlen (S1);
        
        Len2=strlen (S2);
        for (i=0;i<len1;i++) r[i]=s1[i]-' a ' +2;
        r[i]=1,r[i+len2+1]=j=0;
        for (i++;j<len2;j++,i++) r[i]=s2[j]-' a ' +2;
        Suffix_array (r,sa,len1+1+len2+1,30);
        
        Cul_height (R,HEIGHT,SA,RANKX,LEN1+1+LEN2); for (n=i,res=0,i=2;i<=n;i++) {if (Height[i]>res&&check (I,LEN1)) Res=heigh
        T[i];
    printf ("%d\n", res); } RetuRN 0;
 }


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.