Poj2774:long Long Message (suffix array)

Source: Internet
Author: User
Tags first string

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
Looked for a long time, but still did not fully understand the suffix array of these template code all the meaning, so adhering to the previous style, first learn to use the template, in the process of slowly doing the problem slowly realized
This problem requires the longest common continuous substring of two strings, after finding the height array, then distinguish the SA array, as long as one of the SA array is the first string, the other belongs to the second string, then we can obtain its maximum value, the reason can do this, is because the SA array is already sorted by dictionary order for the string
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <bitset># Include <algorithm> #include <climits>using namespace std; #define LS 2*i#define RS 2*i+1#define Up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x;i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define GCD (A, B) __gcd (A, b) #define LL long long#define N 200005#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e- 8int wa[n],wb[n],wsf[n],wv[n],sa[n];int rank[n],height[n],s[n];char str1[n],str2[n];//sa:str string The suffix of the first position is ranked in the sa[i] bit// Rank: is the suffix of the row i bits is the suffix of the rank[i] bit starting with the longest common prefix of//height:i and i-1 bits int cmp (int *r,int a,int b,int k) {return r[a]==r[b]&&r[a +K]==R[B+K];}    void Getsa (int *r,int *sa,int n,int m)//n to include the 0{int i,j,p,*x=wa,*y=wb,*t added at the end;    for (i=0; i<m; i++) wsf[i]=0;    for (i=0; i<n; i++) wsf[x[i]=r[i]]++; for (i= 1; i<m;    i++) wsf[i]+=wsf[i-1];    for (i=n-1; i>=0; i--) sa[--wsf[x[i]]]=i;    P=1;    J=1;        for (; 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<n; i++) wv[i]=x[y[i];        for (i=0; i<m; i++) wsf[i]=0;        for (i=0; i<n; i++) wsf[wv[i]]++;        for (I=1; i<m; i++) wsf[i]+=wsf[i-1];        for (i=n-1; i>=0; i--) sa[--wsf[wv[i]]]=y[i];        T=x;        X=y;        y=t;        x[sa[0]]=0; for (P=1,i=1; i<n; i++) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)?    p-1:p++;    }}void getheight (int *r,int n)//n does not save the last 0{int i,j,k=0;    for (I=1; i<=n; i++) rank[sa[i]]=i;        for (i=0; i<n; i++) {if (k) k--;        else k=0;        J=SA[RANK[I]-1];        while (R[i+k]==r[j+k]) k++;    Height[rank[i]]=k;    }}int Main () {int i,j,k,len,n; W (~scanf ("%s%s", str1,str2)) {len = strlen (str1);       n = 0;        Up (i,0,len-1) s[n++] = str1[i]-' a ' +1;        s[n++] = 30;        Len = strlen (str2);        Up (i,0,len-1) s[n++] = str2[i]-' a ' +1;        S[n] = 0;        Getsa (s,sa,n+1,31);        GetHeight (S,n);        Len = strlen (str1);        int ans = 0; Up (i,2,n-1) {if (Height[i]>ans) {if (sa[i-1]>=0 && sa[i-1]<le                n && sa[i]>=len) ans = max (ans,height[i]);            if (sa[i]>=0 && sa[i]<len && sa[i-1]>=len) 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.