【SPOJ】Longest Common Substring

【SPOJ】Longest Common Substring

求两个字符串的最长公共子串
对一个串建好后缀自动机然后暴力跑一下 废话
讲一下怎么跑吧
从第一个字符开始遍历,遍历不到了再沿着\(parents\)走看能否找到出路,走到某个点时,统计一下走过了多少点然后更新答案
来说说这样做的正确性:
遍历是肯定的, PAM 从根节点出发的任意路径都表示一个子串
沿着\(parents\)边往后走,保证贪心情况下维护最长公共子串寻找出路
注意这里是统计走过了多少点更新答案,不能直接通过\(len\)更新答案

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const LL maxn=600000;
LL nod,last,n,T;
LL len[maxn],fail[maxn],son[maxn][26];
char s[maxn];
inline void Insert(LL c){
    LL np=++nod,p=last;
    len[np]=len[p]+1;
    last=np;
    while(p&&!son[p][c]){
        son[p][c]=np,
        p=fail[p];
    }
    if(!p)
        fail[np]=1;
    else{
        LL q=son[p][c];
        if(len[q]==len[p]+1)
            fail[np]=q;
        else{
            LL nq=++nod;
            len[nq]=len[p]+1;
            fail[nq]=fail[q];
            memcpy(son[nq],son[q],sizeof(son[q]));
            fail[np]=fail[q]=nq;
            while(p&&son[p][c]==q){
                son[p][c]=nq,
                p=fail[p];
            }
        }
    }
}
int main(){
    nod=last=1;
    scanf(" %s",s);
    LL Len=strlen(s);
    for(LL i=0;i<Len;++i)
        Insert(s[i]-'a');
    scanf(" %s",s);
    LL ans=0,now=1,cnt=0;
    Len=strlen(s);
    for(LL i=0;i<Len;++i){
        LL c=s[i]-'a';
        if(son[now][c])
            ++cnt,
            now=son[now][c];
        else{
            while(now&&!son[now][c])
                now=fail[now];
            if(!now)
                cnt=0,
                now=1;
            else
                cnt=len[now]+1,
                now=son[now][c];
        }
        ans=max(ans,cnt);
    }
    printf("%lld\n",ans);
    return 0;
}/*
fjewiofejhiofjmwopejeugfzjkjnfoakweldnfmoierhguiewkjfkowejrfoiwejsfd
jwierhdwuiek,dedjfkz[pjeowrfhuqigrfwerljfiuekdfkcdfheosf
*/

原文地址:https://www.cnblogs.com/y2823774827y/p/10200539.html

时间: 2024-08-28 14:01:07

【SPOJ】Longest Common Substring的相关文章

【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\) 否则,如果不存在可行的

【SPOJ】Longest Common Substring II

[SPOJ]Longest Common Substring II 多个字符串求最长公共子串 还是将一个子串建SAM,其他字符串全部跑一边,记录每个点的最大贡献 由于是所有串,要对每个点每个字符串跑完后去最小值才是每个点的最终贡献 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm

SPOJ 1811LCS Longest Common Substring

后缀自动机裸题.... Longest Common Substring Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is

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

LCS - Longest Common Substring no tags 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 sequence of characters occurrences at

Spoj LCS2 - Longest Common Substring II

题目描述 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 sequence of characters occurrences at least once in a string. Now your t

【LeetCode】Longest Palindromic Substring 解题报告

DP.KMP什么的都太高大上了,自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. [思路](应该算是O(n)吧) 从中间向两端搜索,分别找到以每个字母为中心的最长

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

[题目链接] http://www.spoj.com/problems/LCS2/ [题目大意] 求n个串的最长公共子串 [题解] 对一个串建立后缀自动机,剩余的串在上面跑,保存匹配每个状态的最小值, 取最小值中的最大值即可.由于跑的地方只记录了匹配结尾的状态, 所以还需要更新parent树上的状态,既然匹配到了子节点, 那么parent树链上的值就都能够取到l, 一开始给每个不同状态按照l从小到大分配储存地址, 这样,我们就可以从匹配长度最长的开始更新parent树的情况. [代码] #inc

【Leetcode】Longest Common Prefix

题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目: Write a function to find the longest common prefix string amongst an array of strings. 算法: [java] view plain copy public String longestCommonPrefix(String[] strs) { if (strs.length == 0) {