HDU 1403 Longest Common Substrung [后缀数组] [LCP] [LCS]

Longest Common Substring
Time Limit: 4000MS
Memory Limit: 32768KB
64bit IO Format: %I64d & %I64u

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


注意有多组数据!!!那么就是用后缀数组求LCS的裸题咯。。
中间用个特殊字符隔开,因为任意两个后缀的LCP必定可以由rank相邻的两个得来,那么只需要求height最大值并且分别在 特殊字符 两边即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 200005;
int seq[maxn],cnt[maxn],sa[maxn],t1[maxn],t2[maxn];
inline bool equal(int temp[],int i,int len)
{
    return temp[sa[i-1]]==temp[sa[i]] && temp[sa[i-1]+len]==temp[sa[i]+len];
}
void da(int lens,int lim)
{
    int *x=t1,*y=t2;
    for(int i=0;i<lim;i++) cnt[i]=0;
    for(int i=0;i<lens;i++) cnt[ x[i]=seq[i] ]++;
    for(int i=1;i<lim;i++) cnt[i] += cnt[i-1];
    for(int i=lens-1;i>=0;i--) sa[--cnt[x[i]]] = i;
    for(int k=1;k<=lens;k<<=1)
    {
        int p=-1;
        for(int i=lens-k;i<lens;i++) y[++p]=i;
        for(int i=0;i<lens;i++) if(sa[i]>=k) y[++p]=sa[i]-k;
        for(int i=0;i<lim;i++) cnt[i]=0;
        for(int i=0;i<lens;i++) cnt[x[y[i]]]++;
        for(int i=1;i<lim;i++) cnt[i] += cnt[i-1];
        for(int i=lens-1;i>=0;i--) sa[--cnt[x[y[i]]]] = y[i];
        swap(x,y); p=1;
        x[sa[0]]=0;
        for(int i=1;i<lens;i++)
            x[sa[i]] = equal(y,i,k)? p-1 : p++;
        if(p>=lens) break;
        lim = p;
    }
}
int rank[maxn],height[maxn];
void get_height(int lens)
{
    int k=0;
    for(int i=1;i<=lens;i++) rank[sa[i]]=i;
    for(int i=0;i<lens;i++)
    {
        if(k) k--;
        int j=sa[rank[i]-1];
        while(seq[i+k]==seq[j+k]) k++;
        height[rank[i]]=k;
    }
}
char s[maxn];
int work()
{
    if(!~scanf("%s",s)) return -1;
    memset(seq,0,sizeof(seq));
    int len = strlen(s);
    int lim = 0;
    for(int i=0;i<len;i++) seq[i]=s[i]-'a'+1;
    seq[len]='0'; //special character
    scanf("%s",s+len+1);
    int lens = strlen(s+len+1) + len + 1;
    for(int i=len+1;i<lens;i++) seq[i]=s[i]-'a'+1;
    da(lens+1,150);
    get_height(lens);
    int ans = 0;
    for(int i=2;i<=lens;i++)
        if((sa[i-1]<len&&sa[i]>len) || (sa[i-1]>len&&sa[i]<len))
            smax(ans , height[i]);
    return ans;
}
int main()
{
    freopen("lcs.in","r",stdin);
    freopen("lcs.out","w",stdout);
    while(true)
    {
        int ans = work();
        if(!~ans) break;
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值