HDU - 1403 Longest Common Substring (后缀数组)

题目链接

题目原文

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

题目大意

给出两个字符串 s1 和 s2
找出它们的最长公共子串

题目思路

后缀数组里有一个叫高度数组的小东西
表示最长公共前缀
我们只要把这两个字符串拼成一个,然后直接瞎j2暴力找最长公共前缀
值得注意的是,需要判断一下我们找到的最长公共前缀 h e i g h t [ i ] height[i] height[i] ,其在 s a [ ] sa[] sa[]中, i i i左右两个后缀,分别在字符串 s 1 s1 s1 s 2 s2 s2

代码

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int mc = 2e5 + 10;
//注意,我们把两个字符串拼接到了一起,所以要开两倍的空间
char s[mc];
int y[mc], x[mc], c[mc], sa[mc], rk[mc], height[mc], wt[30];
int n, m;
#define mt(x, k) memset((x), (k), sizeof(x))

void sa_init() {
    mt(c, 0);
    mt(x, 0);
    mt(y, 0);
    mt(sa, 0);
    mt(rk, 0);
    mt(height, 0);
}
//计算后缀数组
void get_SA() {
    for (int i = 1; i <= n; ++i) ++c[x[i] = s[i]];
    for (int i = 2; i <= m; ++i) c[i] += c[i - 1];
    for (int i = n; i >= 1; --i) sa[c[x[i]]--] = i;
    for (int k = 1; k <= n; k <<= 1) {
        int num = 0;
        for (int i = n - k + 1; i <= n; ++i) y[++num] = i;
        for (int i = 1; i <= n; ++i) if (sa[i] > k) y[++num] = sa[i] - k;
        for (int i = 1; i <= m; ++i) c[i] = 0;
        for (int i = 1; i <= n; ++i) ++c[x[i]];
        for (int i = 2; i <= m; ++i) c[i] += c[i - 1]; //第一关键字排名为1~i的数有多少个
        for (int i = n; i >= 1; --i) sa[c[x[y[i]]]--] = y[i], y[i] = 0;
        swap(x, y);
        x[sa[1]] = 1;
        num = 1;
        for (int i = 2; i <= n; ++i)
            x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
        if (num == n) break;
        m = num;
    }
}
//计算高度数组
void get_height() {
    int k = 0;
    for (int i = 1; i <= n; ++i) rk[sa[i]] = i;
    for (int i = 1; i <= n; ++i) {
        if (rk[i] == 1) continue;//第一名height为0
        if (k) --k;//h[i]>=h[i-1]-1;
        int j = sa[rk[i] - 1];
        while (j + k <= n && i + k <= n && s[i + k] == s[j + k]) ++k;
        height[rk[i]] = k;//h[i]=height[rk[i]];
    }
}

int main() {
    m = 128;
    while (~scanf("%s", s + 1)) {
        sa_init();
        //用一个len1,来记录第一个字符串的长度,从而区分
        int len1 = strlen(s + 1);
        s[len1 + 1] = '$';
        scanf("%s", s + 2 + len1);
        //因为模板是以s[1]作为字符串开头的,所以要处理一下
        n = strlen(s + 1);
        get_SA();
        get_height();
        int ans = 0;
        for (int i = 2; i <= n; ++i) {
            //找到最长的公共前缀,并且其前后两个字符串分别在s1和s2中
            if (ans < height[i] && (min(sa[i - 1], sa[i]) <= len1 && max(sa[i - 1], sa[i]) > len1))
                ans = height[i];
        }
        printf("%d\n", ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值