Hihocoder #1415: suffix array three • Repeat Melody 3

Source: Internet
Author: User
Tags first string

#1415: suffix array three • Repeat Melody 3Time limit:5000mscase time limit:1000msmemory limit:256mb description

Small hi usually a big hobby is playing the piano. We know that a musical melody is represented as a series of numbers consisting of N in length. Little hi has practiced a lot of music and found that many of the melodies in the works have a common part.

Melody is a continuous series, if the same melody in works A and b in the same time, this melody is a and b common parts, such as in Abab in Bababab and Cabacababc have appeared. Little hi want to know what is the longest common melody of the two works?

Tips on how to solve problems

Input

A total of two lines. A single line of string that contains only lowercase letters. The string length does not exceed 100000.

Output

An integer line that represents the answer.

Sample Input
Abcdefgabacabca
Sample Output
3
Analysis:

Connecting two strings together becomes the longest common prefix for suffixes, but since the prefix cannot span two strings, we add a character after the first string that does not appear, then the second string, and then the maximum height of all the non-identical string suffixes ...

Code:
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio>//by neighthornusing namespace Std;//zhao kan fei niao mu fei hui,ying chuang hua luo lian chui diconst int maxn=200000+5;int n , T,s[maxn],gs[maxn],sa[maxn],wb[maxn],wv[maxn],ran[maxn],height[maxn];char s1[maxn],s2[maxn];inline BOOL cmp (INT * X,int a,int b,int L) {return x[a]==x[b]&&x[a+l]==x[b+l];} inline void da (int *sa,int *x,int n,int m) {int i,j,p,*y=wb;for (i=0;i<m;i++) gs[i]=0;for (i=0;i<n;i++) gs[x[i]]++; for (i=1;i<m;i++) gs[i]+=gs[i-1];for (i=n-1;~i;i--) sa[--gs[x[i]]]=i;for (j=1,p=1;p<n;j<<=1,m=p) {for (i= n-j,p=0;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++) gs[i]=0;for (i=0;i<n;i++) gs[wv[i]]++;for (i=1;i<m;i++) gs[i]+=gs[i-1];for (i=n-1;~i;i--) Sa[--gs[wv[i]]]=y[i];p =1;swap (x, y), x[sa[0]]=0;for (i=1;i<n;i++) x[sa[i]]=cmp (y,sa[i],sa[i-1],j) p-1:p++;}} inline void calheight (int n) {int i,j, K=0;for (i=0;i<=n;i++) ran[sa[i]]=i;for (i=0;i<n;height[ran[i++]]=k) for (k?k--:233,j=sa[ran[i]-1];s[i+k]==s [j+k];k++);} inline int solve (int n) {int ans=0;for (int i=1;i<=n;i++) if (sa[i]<t&&sa[i-1]>t) | | (sa[i]>t&&sa[i-1]<t)) Ans=max (Ans,height[i]); return ans;} Signed main (void) {scanf ("%s%s", S1,s2), T=n=strlen (S1); for (int i=0;i<n;i++) s[i]=s1[i]-' a ' +1;s[n]=100;int lala= strlen (S2); for (int i=1;i<=lala;i++) s[i+n]=s2[i-1]-' a ' +1;n+=lala+1;for (int i=0;i<n;i++) ran[i]=s[i];d a (SA, RAN,N+1,101); Calheight (n);p rintf ("%d\n", Solve (n)); return 0;} The Cap ou pas cap. Pas Cap.

  

by Neighthorn

Hihocoder #1415: suffix array three • Repeat Melody 3

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.