标签:def flag 复杂 cos reg -- print vector map
简单的想法就是把这些串的广义\(\mathrm{SAM}\)建出来,然后对每个节点求出它代表的串出现在了多少个原串中。假设这个已经求出,接下来我们对每个节点求出它及其祖先节点的贡献(因为它们对应了最长串的一连串后缀),在求每个串的答案时在\(\mathrm{SAM}\)匹配就好了。
那么怎么求每个节点的串出现在了多少个原串中呢?暴力的想法是在\(\mathrm{SAM}\)上匹配,然后对其祖先打上标记看起来复杂度是\(O(|s|^2)\)的,但是由于\(\mathrm{SAM}\)的节点数是\(O(\sum |s|)\)的,在配合上一些不等式技巧可证得打标记的总时间复杂度是\(O(L\sqrt L)\)的(\(L=\sum |s|\))
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bitset>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
const int N=100000+100;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u];i;i=sq[i].nxt)
#define fir first
#define sec second
#define mp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
int n,m,lst,tot=1,siz[N<<1],ch[N<<1][26],len[N<<1],sum[N<<1],ord[N<<1],fa[N<<1],tax[N<<1],cnt[N<<1],col[N<<1];
char s[N];
vector<char> str[N];
void insert(int x)
{
if ((ch[lst][x]) && (len[ch[lst][x]]==len[lst]+1))
{
lst=ch[lst][x];
return;
}
int np=(++tot),p=lst,flag=0;len[np]=len[p]+1;
while ((p) && (!ch[p][x])) {ch[p][x]=np;p=fa[p];}
if (!p) fa[np]=1;
else
{
int q=ch[p][x];
if (len[q]==len[p]+1) fa[np]=q;
else
{
if (len[np]==len[p]+1) flag=1;
int nq=(++tot);len[nq]=len[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
fa[nq]=fa[q];fa[np]=fa[q]=nq;
while ((p) && (ch[p][x]==q)) {ch[p][x]=nq;p=fa[p];}
if (flag) np=nq;
}
}
siz[np]=1;lst=np;
}
int main()
{
n=read();m=read();
rep(i,1,n)
{
scanf("%s",s+1);
int len=strlen(s+1);lst=1;
rep(j,1,len)
{
insert(s[j]-'a');
str[i].pb(s[j]);
}
}
rep(i,1,tot) tax[len[i]]++;
rep(i,1,tot) tax[i]+=tax[i-1];
per(i,tot,1) ord[tax[len[i]]--]=i;
rep(i,1,n)
{
int now=1,len=str[i].size();
rep(j,0,len-1)
{
int x=str[i][j]-'a';
now=ch[now][x];
int tmp=now;
while ((tmp) && (col[tmp]!=i))
{
col[tmp]=i;cnt[tmp]++;
tmp=fa[tmp];
}
}
}
cnt[1]=0;
rep(i,1,tot)
{
int u=ord[i],f=fa[u];
sum[u]=sum[f];
if (cnt[u]>=m) sum[u]+=len[u]-len[f];
}
rep(i,1,n)
{
ll ans=0;int now=1,len=str[i].size();
rep(j,0,len-1)
{
int x=str[i][j]-'a';
now=ch[now][x];
ans+=sum[now];
}
printf("%lld ",ans);
}
return 0;
}
标签:def flag 复杂 cos reg -- print vector map
原文地址:https://www.cnblogs.com/encodetalker/p/12528474.html