又入lcm,O(n)写法,(SAM)

当两个字符串的长度大于 1 e 5 1e5 1e5

显然之前的dp思路会超时,这里引入 S A M SAM SAM的解法

题目描述

在这里插入图片描述

思路:

  • 先考虑,只有两个串的情况
    在这里插入图片描述

对于两个串,直观的想法是:

  1. 先对 A A A串求一遍 S A M SAM SAM,之后就相当于有了 A A A的所有子串。
  • 暴力解法:
    对于B串每次枚举一个起点,在 S A M SAM SAM里跑(A建立的 S A M SAM SAM中跑),当匹配失败时,又要重新枚举起点
    o ( n 2 ) o(n^2) o(n2)显然会超时。
  • 利用 S A M SAM SAM的性质:
    当匹配失败时,重新枚举起点时,由于当前失配的点(状态)的所有后缀都会失配,所以可以从这个状态跳到father,来发生质变(类比于kmp)
  • dfs统计max
    由于每个状态p是一个等价类(儿子),根结点的max由儿子共同决定
    叶 子 结 点 的 状 态 p 1 , p 2 , . . . ⊆ p r 根 结 点 的 状 态 叶子结点的状态p1,p2,...\subseteq p_r根结点的状态 p1,p2,...pr,即根节点所表示的串,是叶子结点的后缀),所以要跑一遍dfs

AC

/*
皮卡丘冲鸭!
へ     /|
  /\7    ∠_/
  / │   / /
 │ Z _,< /   /`ヽ
 │     ヽ   /  〉
  Y     `  /  /
 イ● 、 ●  ⊂⊃〈  /
 ()  へ    | \〈
  >ー 、_  ィ  │ //
  / へ   / ノ<| \\
  ヽ_ノ  (_/  │//
  7       |/
  >―r ̄ ̄`ー―_
*/
#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define ALL(x) x.begin(),x.end()
#define mp make_pair
#define fi first
#define se second
#define db double
//#define endl '\n'
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
typedef pair<db,db> pdd;

const db eps = 1e-8;
const db pi = acos(-1.0);

template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
  int x = 0, f = 0; char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
template<typename T> void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
  print(x), putchar(let);
}

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int N = 2e4+10;
struct Node {
    int len, fa;
    int ch[26];
} node[N];
int last = 1, tot = 1;
char str[N];
int head[N], ne[N], e[N], idx;

void extend(int c){
    int p = last, np = last = ++tot;
    node[np].len = node[p].len + 1;
    while(p && !node[p].ch[c])node[p].ch[c] = np, p = node[p].fa;
    if(!p) node[np].fa = 1;
    else {
        int q = node[p].ch[c];
        if(node[q].len == node[p].len + 1) node[np].fa = q;
        else {
            int nq = ++tot;
            node[nq] = node[q], node[nq].len = node[p].len + 1;
            node[q].fa = node[np].fa = nq;
            while(p && node[p].ch[c] == q)node[p].ch[c] = nq, p = node[p].fa;
        }
    }
}

void add(int a, int b){
    e[idx] = b, ne[idx] = head[a],head[a] = idx++;  
}

int now[N], ans[N];

void dfs(int u){
    for(int i = head[u]; ~i; i = ne[i]){
        int v = e[i];
        dfs(v);
        ckmax(now[u],now[v]);
    }
}

void sol(){
    int n = read();
    scanf("%s", str);
    int len1 = strlen(str);
    fori(i, 0, len1) extend(str[i] - 'a');
    For(i,1,tot)ans[i] = node[i].len;
    mst(head,-1);
    For(i,1,tot)add(node[i].fa, i);
    For(i,2,n){
        scanf("%s", str);
        mst(now,0);///
        int p = 1, len = 0;
        for(int j = 0; str[j]; j ++ ){
            int c = str[j] - 'a';
            while(p > 1 && !node[p].ch[c]) p = node[p].fa, len = node[p].len;
            if(node[p].ch[c]) p = node[p].ch[c], len++;
            ckmax(now[p],len);
        }
        dfs(1);
        For(i,1,tot)ckmin(ans[i],now[i]);
    }
    int res = 0;
    For(i,1,tot)ckmax(res,ans[i]);
    print(res,'\n');
}

//#define LOCAL
int main()
{
   // ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
    sol();
    return 0;
}

作者:HenriHGS
链接:https://www.acwing.com/activity/content/code/content/1033991/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值