SPOJ - LCS2 Longest Common Substring II 后缀自动机

本文介绍了一种处理多个字符串的最长公共子序列(LCS)问题的算法实现。通过使用后缀自动机(SAM)结构,文章详细展示了如何构建SAM,并采用拓扑排序更新每个节点的最长匹配串长度,最终求得所有输入串间的最长公共子序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多个串求lcs,

每次匹配,每个节点记录该节点匹配的最长串。然后每个节点需要根据拓扑序更新到pre[x]上。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 2*2.5e5 + 8;
int sum[maxn],temp[maxn];
string s;
struct SAM{
     int ch[maxn][26], pre[maxn], val[maxn];
    int last, tot, minl[maxn], maxl[maxn];
    void init(){
        last = tot = 0;
        memset(ch[0], -1, sizeof ch[0]);
        pre[0] = -1; val[0] = 0;
        minl[0] = maxl[0] = 0;
    }
  void extend(int c){
        int p = last, np = ++tot;
        val[np] = val[p] + 1;
        memset(ch[np], -1, sizeof ch[np]);
        minl[np] = maxl[np] = val[np];
        while(~p && ch[p][c] == -1) ch[p][c] = np, p = pre[p];
        if(p == -1) pre[np] = 0;
        else{
            int q = ch[p][c];
            if(val[q] != val[p] + 1){
                int nq = ++tot;
                memcpy(ch[nq], ch[q], sizeof ch[q]);
                val[nq] = val[p] + 1;
                minl[nq] = maxl[nq] = val[nq];
                pre[nq] = pre[q];
                pre[q] = pre[np] = nq;
                while(~p && ch[p][c] == q) ch[p][c] = nq, p = pre[p];
            }
            else pre[np] = q;
        }
        last = np;
    }
    void build(const string &s)
    {
        init();
        for(int i=0;i<s.size();i++) extend(s[i]-'a');

    }
      void Tsort(const string &s){
        for (int i=0;i<=tot;i++) sum[val[i]]++ ;
        for (int i=1;i<=s.size();i++) sum[i]+=sum[i-1];
        for (int i=0;i<=tot;i++) temp[sum[val[i]]--]=i;
  //     for (int i=1;i<=tot+1;i++) printf("%d %d\n",temp[i],val[temp[i]]);
    }
  void _find(const string &s){
        int sz = s.size(), u = 0, tmp = 0, c, i;
        for(i = 0; i <= tot; i++) maxl[i] = 0;
        for(i = 0; i < sz; i++){
            c = s[i] - 'a';
            if(~ch[u][c]) tmp++, u = ch[u][c];
            else{
                while(~u && ch[u][c] == -1) u = pre[u];
                if(~u) tmp = val[u] + 1, u = ch[u][c];
                else tmp = 0, u = 0;
            }
            maxl[u] = max(maxl[u], tmp);
        }
      //     for(int i=0;i<=tot;i++) printf("%d ",maxl[i]);printf("\n");
      //  printf("%d\n",val[pre[temp[tot+1]]]);
        for(i = tot+1; i >= 2; i--)if(maxl[temp[i]]) maxl[pre[temp[i]]]=val[pre[temp[i]]];
    //    for(int i=0;i<=tot;i++) printf("%d ",maxl[i]);
        for(i = 0; i <= tot; i++) minl[i] = min(minl[i], maxl[i]);
    }
    int calc(){
        int res = 0;
        for(int i = 0; i <= tot; i++) res = max(res, minl[i]);
        return res;
    }
} sam;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    int n, i;
    cin >> s;
    sam.build(s);
    sam.Tsort(s);
     while(cin >> s) sam._find(s);
    cout << sam.calc() << endl;;

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值