hdu1403Longest Common Substring

Longest Common Substring

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5144    Accepted Submission(s): 1811



Problem Description
Given two strings, you have to tell the length of the Longest Common Substring of them.

For example:
str1 = banana
str2 = cianaic

So the Longest Common Substring is "ana", and the length is 3.
 

Input
The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

Process to the end of file.
 

Output
For each test case, you have to tell the length of the Longest Common Substring of them.
 

Sample Input
   
   
   
   
banana cianaic
 

Sample Output
   
   
   
   
3
 

Author
Ignatius.L

这是一道后缀数组题,根据height数组只要加上(sa[i]-strlen(s))*(sa[i-1]-strlen(s))<0进行判断即可
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=200000+100;
char s[maxn],s1[maxn];
int sa[maxn],t[maxn],t2[maxn],c[maxn],n;
//构造字符串s的后缀数组,每个字符值必须为0~m-1
void build_sa(int m){
    int *x=t,*y=t2;
    //基数排序
    for(int i=0;i<m;i++)    c[i]=0;
    for(int i=0;i<n;i++)    c[x[i]=s[i]]++;
    for(int i=1;i<m;i++)    c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
    for(int k=1;k<=n;k<<=1){
        int p=0;
        //直接利用sa数组排序第二关键字
        for(int i=n-k;i<n;i++)  y[p++]=i;
        for(int i=0;i<n;i++)    if(sa[i]>=k)    y[p++]=sa[i]-k;
        //基数排序第一关键字
        for(int i=0;i<m;i++)    c[i]=0;
        for(int i=0;i<n;i++)    c[x[y[i]]]++;
        for(int i=1;i<m;i++)    c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
        //根据sa和y计算新的x数组
        swap(x,y);
        p=1;
        x[sa[0]]=0;
        for(int i=1;i<n;i++)
            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
        if(p>=n)
            break;
        m=p;                //下次基数排序的最大值
    }
}
int rank1[maxn],height[maxn];

void getHeight(){
    int i,j,k=0;
    n--;
    for(i=0;i<=n;i++)    rank1[sa[i]]=i;
    for(i=0;i<n;i++){
        if(k)
            k--;
        int j=sa[rank1[i]-1];
        while(s[i+k]==s[j+k])   k++;
        height[rank1[i]]=k;
    }
}

int main(){
    while(scanf("%s%s",s,s1)!=EOF){
        int z=strlen(s);
        s[z]='*';
        s[z+1]='\0';
        strcat(s,s1);
        n=strlen(s);
        s[n]='0';
        n++;
        s[n]='\0';
        build_sa(144);
        getHeight();
        int ans=0;
        for(int i=1;i<=n;i++){
            if(height[i]>ans&&(ll)(sa[i]-z)*(sa[i-1]-z)<0)  //此处乘法会溢出
                ans=height[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(hdu1403Longest Common Substring)