spoj Longest Common Substring (多串求最大公共子序列)

题目链接:

https://vjudge.net/problem/SPOJ-LCS

题意:

最多10行字符串

求最大公共子序列

数据范围:

$1\leq |S| \leq100000$

分析:

让他们都和第一个字符串匹配,算出每个字符串与第一个字符串的,以$i$位置(i指的是在s1中的位置)结尾匹配的最大长度

与其它字符串的匹配取最小值

最后对所有位置取最大值

超时代码:(题限是236ms,这个代码跑2000ms没问题)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=500000*2+7;
char S[maxn];
char S1[maxn/2];
int ans[maxn/2];
int n,a,b,len1,len2;
struct suffixautomation
{
    int mp[maxn*2][30],fa[maxn*2],ed,ct,len[maxn*2],minpos[maxn*2],maxpos[maxn*2],deg[maxn*2];
    int res[maxn/2];
    suffixautomation(){
        for(int i=0;i<maxn*2;i++)minpos[i]=1e9,maxpos[i]=-1;
        ed=ct=1;}
    inline void ins(int c,int pos)
    {
        int p=ed;ed=++ct;minpos[ed]=maxpos[ed]=pos;len[ed]=pos;//先初始化size和len
        for(;p&&mp[p][c]==0;p=fa[p]){mp[p][c]=ed;}//然后顺着parent树的路径向上找
        if(p==0){fa[ed]=1;return;}int q=mp[p][c];//case1
        if(len[p]+1==len[q]){fa[ed]=q;return;}//case2
        len[++ct]=len[p]+1;//case 3
        for(int i=1;i<=26;i++){mp[ct][i]=mp[q][i];}
        fa[ct]=fa[q];fa[q]=ct;fa[ed]=ct;
        for(int i=p;mp[i][c]==q;i=fa[i]){mp[i][c]=ct;}
    }
    void solve(){

        for(int i=1;i<=ct;i++)deg[fa[i]]++;
        queue<int>que;
        for(int i=1;i<=ct;i++)if(deg[i]==0)que.push(i);
        while(que.size()){
            int x=que.front();
            int y=fa[x];
            que.pop();
            minpos[y]=min(minpos[x],minpos[y]);
            maxpos[y]=max(maxpos[x],maxpos[y]);
            deg[y]--;
            if(deg[y]==0)que.push(y);
        }
        for(int i=1;i<=ct;i++)
            if(minpos[i]<=len1&&maxpos[i]>=len1+2)
                res[minpos[i]]=max(res[minpos[i]],len[i]);
    }
}sam,sam2;
int main()
{
//    cout<<"acfvd"
    for(int i=0;i<maxn/2;i++)ans[i]=1e9;
    scanf("%s",S+1);
    len1=strlen(S+1);
    S[len1+1]=‘z‘+1;
    while(scanf("%s",S1+1)==1){
         len2=strlen(S1+1);
         sam=sam2;
         for(int i=len1+2;i<=len1+len2+1;i++)
            S[i]=S1[i-len1-1];
        for(int i=1;i<=len1+len2+1;i++)sam.ins(S[i]-‘a‘+1,i);
        sam.solve();
        for(int i=1;i<=len1;i++)
            ans[i]=min(ans[i],sam.res[i]);
    }
    int res=0;
    for(int i=1;i<=len1;i++)
        if(ans[i]!=1e9)res=max(res,ans[i]);
    printf("%d\n",res);
    return 0;
}

  

原文地址:https://www.cnblogs.com/carcar/p/11631745.html

时间: 2024-07-29 17:54:36

spoj Longest Common Substring (多串求最大公共子序列)的相关文章

SPOJ Longest Common Substring II

Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: LCS264-bit integer IO format: %lld      Java class name: Main A string is finite sequence of characters over a non-empty finite se

后缀自动机(SAM):SPOJ Longest Common Substring II

Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the set of lowercase letters. Substring, also called factor, is a consecutive sequenc

spoj 1811 LCS - Longest Common Substring (后缀自动机)

spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2) 铁定超时 后缀数组 O(nlog(n)) 在spoj上没试过,感觉也会被卡掉 后缀自动机 O(n) 我们考虑用SAM读入字符串B; 令当前状态为s,同时最大匹配长度为len; 我们读入字符x.如果s有标号为x的边,那么s=trans(s,x),len = len+1; 否则我们找到s的第一个祖先a,它

spoj 1812 LCS2 - Longest Common Substring II (后缀自动机)

spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 <= n <= 10 |A[i]| <= 1e5 思路: 和spoj 1811 LCS差不多的做法 把其中一个A建后缀自动机 考虑一个状态s, 如果A之外的其他串对它的匹配长度分别是a[1], a[2], ..., a[n - 1], 那么min(a[1], a[2], ..., a[n - 1]

【SPOJ】1812. Longest Common Substring II(后缀自动机)

http://www.spoj.com/problems/LCS2/ 发现了我原来对sam的理解的一个坑233 本题容易看出就是将所有匹配长度记录在状态上然后取min后再对所有状态取max. 但是不要忘记了一点:更新parent树的祖先. 为什么呢?首先如果子树被匹配过了,那么长度一定大于任意祖先匹配的长度(甚至有些祖先匹配长度为0!为什么呢,因为我们在匹配的过程中,只是找到一个子串,可能还遗漏了祖先没有匹配到,这样导致了祖先的记录值为0,那么在对对应状态去min的时候会取到0,这样就wa了.而

HDU 1403 Longest Common Substring(后缀数组啊 求最长公共子串 模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1403 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", a

spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

spoj 1812 LCS2 - Longest Common Substring II 题意: 给出最多n个字符串A[1], ..., A[n], 求这n个字符串的最长公共子串. 限制: 1 <= n <= 10 |A[i]| <= 1e5 思路: 和spoj 1811 LCS几乎相同的做法 把当中一个A建后缀自己主动机 考虑一个状态s, 假设A之外的其它串对它的匹配长度各自是a[1], a[2], ..., a[n - 1], 那么min(a[1], a[2], ..., a[n -

【SPOJ】Longest Common Substring II (后缀自动机)

[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记录\(f[i]\)表示走到了\(i\)节点 能够匹配上的最长公共子串的长度 当然,每个串的\(f[i]\)可以更新\(f[i.parent]\) 所以需要拓扑排序 对于每个串求出每个节点的最长匹配 然后对他们取\(min\),表示某个节点大家都能匹配的最长长度 最后对于所有点的值都取个\(max\)

【SPOJ】Longest Common Substring(后缀自动机)

[SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另外一个串在\(SAM\)上不断匹配 最后计算答案就好了 匹配方法: 如果\(trans(s,c)\)存在 直接沿着\(trans\)走就行,同时\(cnt++\) 否则沿着\(parent\)往上跳 如果存在\(trans(now,c),cnt=now.longest+1\) 否则,如果不存在可行的