刚学最长公共子串求调
查看原帖
刚学最长公共子串求调
749630
wxzzzz楼主2023/10/11 21:40

悬赏八个 A_Passing_Creeper 的小号 关注。

die马:

#include <bits/stdc++.h>
#define ll long long
#define rll register ll
#define cll const ll
#define N 100005
#define M 15
using namespace std;
inline ll read()
{
    rll x=0;bool f=1;register char c=getchar();
    while(c<48||c>57){if(c=='-') f=0;c=getchar();}
    while(c>=48&&c<=57){x=x*10+(c^48);c=getchar();}
    return f?x:-x;
}
inline void write(ll x)
{
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+48);
}
cll n=read();
ll ans=1e18,pos,idxs=1,last=1,minn=1e18,sc[N],len[N],lens[M],t[N][26];
char s[M][N];
inline void insert(ll x)
{
    rll p=++idxs,q,nq;
    len[p]=len[last]+1;
    while(last&&!t[last][x])
        t[last][x]=p,last=sc[last];
    if(!last)
	{
        sc[p]=1,last=p;
        return;
    }
    q=t[last][x];
    if(len[q]==len[last]+1)
	{
        sc[p]=q,last=p;
        return;
    }
    nq=++idxs;
    len[nq]=len[last]+1;
    sc[nq]=sc[q],sc[q]=sc[p]=nq;
    memcpy(t[nq],t[q],sizeof t[nq]);
    while(t[last][x]==q)
        t[last][x]=nq,last=sc[last];
    last=p;
}
inline void mate(ll x)
{
	rll ret=0,tmp=0,now=1;
	for(rll i=1;i<=lens[x];i++)
	{
		cll c=s[x][i]-'a';
		while(!t[now][c]&&sc[now])
			now=sc[now],tmp=len[now];
		if(!t[now][c]) now=1,tmp=0;
		else tmp++,now=t[now][c];
		ret=max(ret,tmp);
	}
	ans=min(ans,ret);
}
int main()
{
	for(rll i=1;i<=n;i++)
	{
		scanf("%s",s[i]+1),lens[i]=strlen(s[i]+1);
		if(lens[i]<minn) minn=lens[i],pos=i;
	}
	for(rll i=1;i<=lens[pos];i++)
		insert(s[pos][i]-'a');
	for(rll i=1;i<=n;i++) mate(i);
	write(ans);
    return 0;
}
2023/10/11 21:40
加载中...