HDU1403 Longest Common Substring —— 后缀数组

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1403


Longest Common Substring

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


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




题解:

1.将两个字符串接在一起,并用特殊分隔符分开。然后跑一下后缀数组,得出height[]数组。

2.枚举height[]数组,如果排位相邻的两个后缀字符串的开头分别在两边(原先的两个字符串),则更新答案。



后缀数组学习推荐:

http://blog.csdn.net/yxuanwkeith/article/details/50636898

http://blog.csdn.net/j_sure/article/details/41777097




代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int MAXN = 2e5+10;

bool cmp(int *r, int a, int b, int l)
{
    return r[a]==r[b] && r[a+l]==r[b+l];
}

int t1[MAXN], t2[MAXN], c[MAXN];
void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
{
    int i, j, p, *x = t1, *y = t2;
    for(i = 0; i<m; i++) c[i] = 0;
    for(i = 0; i<n; i++) c[x[i] = str[i]]++;
    for(i = 1; i<m; i++) c[i] += c[i-1];
    for(i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
    for(j = 1; j<=n; j <<= 1)
    {
        p = 0;
        for(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++) c[i] = 0;
        for(i = 0; i<n; i++) c[x[y[i]]]++;
        for(i = 1; i<m; i++) c[i] += c[i-1];
        for(i = n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i];

        swap(x, y);
        p = 1; x[sa[0]] = 0;
        for(i = 1; i<n; i++)
            x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++;

        if(p>=n) break;
        m = p;
    }

    int k = 0;
    for(i = 0; i<=n; i++) Rank[sa[i]] = i;
    for(i = 0; i<n; i++)
    {
        if(k) k--;
        j = sa[Rank[i]-1];
        while(str[i+k]==str[j+k]) k++;
        height[Rank[i]] = k;
    }
}

char a[MAXN], b[MAXN];
int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
// sa[]下标的有效值:0~n-1;  height[]下标的有效值:1~n-1
int main()
{
    while(scanf("%s%s", a, b)!=EOF)
    {
        int lena, lenb, len;
        lena = strlen(a);
        lenb = strlen(b);

        len = 0;
        for(int i = 0; i<lena; i++) r[len++] = a[i];
        r[len++] = 1;
        for(int i = 0; i<lenb; i++) r[len++] = b[i];
        r[len++] = 0;
        DA(r, sa, Rank, height, len, 128);

        int ans = 0;
        for(int i = 1; i<len; i++)
            if( (sa[i]<lena && sa[i-1]>lena) || (sa[i]>lena && sa[i-1]<lena) )
                ans = max(ans, height[i]);
        printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值