luogu3181 [HAOI2016]找相同字符

建广义\(\mathrm{SAM}\),同时对每个串在每个节点处分别维护其\(\mathrm{endpos}\)集合大小,记广义\(\mathrm{SAM}\)上的节点\(u\)的两个串的\(\mathrm{endpos}\)集合大小分别为\(siz_{u,0},siz_{u,1}\),则

\[\mathrm{Answer}=\sum_{i=1}^{tot} siz_{i,0}\times siz_{i,1} \times (\mathrm{Len}(u)-Len(fa_u)) \]

#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;
}

namespace My_Math{
	#define N 100000

	int fac[N+100],invfac[N+100];

	int add(int x,int y) {return x+y>=maxd?x+y-maxd:x+y;}
	int dec(int x,int y) {return x<y?x-y+maxd:x-y;}
	int mul(int x,int y) {return 1ll*x*y%maxd;}
	ll qpow(ll x,int y)
	{
		ll ans=1;
		while (y)
		{
			if (y&1) ans=mul(ans,x);
			x=mul(x,x);y>>=1;
		}
		return ans;
	}
	int inv(int x) {return qpow(x,maxd-2);}

	int C(int n,int m)
	{
		if ((n<m) || (n<0) || (m<0)) return 0;
		return mul(mul(fac[n],invfac[m]),invfac[n-m]);
	}

	int math_init()
	{
		fac[0]=invfac[0]=1;
		rep(i,1,N) fac[i]=mul(fac[i-1],i);
		invfac[N]=inv(fac[N]);
		per(i,N-1,1) invfac[i]=mul(invfac[i+1],i+1);
	}
	#undef N
}
using namespace My_Math;

int n,tot=1,lst,ch[N<<1][26],len[N<<1],fa[N<<1],col[N<<1];
ll ans[N];
char s[N];
int ord[N<<1],tax[N<<1];

void insert(int x,int id)
{
	//cout << x << endl;
	if ((ch[lst][x]) && (len[ch[lst][x]]==len[lst]+1)) 
	{
		if (col[ch[lst][x]]) col[ch[lst][x]]=-1;
		else if (!col[ch[lst][x]]) col[ch[lst][x]]=id;
		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[q]=fa[np]=nq;
			while ((p) && (ch[p][x]==q)) {ch[p][x]=nq;p=fa[p];}
			if (flag) np=nq;
		}
	}
	if (col[np]) col[np]=-1;
	else if (!col[np]) col[np]=id;
	lst=np;
}

int main()
{
	n=read();
	rep(i,1,n)
	{
		lst=1;
		scanf("%s",s+1);
		int m=strlen(s+1);
		rep(j,1,m) insert(s[j]-'a',i);
	}
	rep(i,1,tot) tax[len[i]]++;
	rep(i,1,tot) tax[i]+=tax[i-1];
	rep(i,1,tot) ord[tax[len[i]]--]=i;
	per(i,tot,1)
	{
		int u=ord[i],f=fa[u];
		if (col[u]!=-1) ans[col[u]]+=len[u]-len[f];
		if (!col[f]) col[f]=col[u];
		else if (col[f]!=col[u]) col[f]=-1;
	}
	rep(i,1,n) printf("%lld\n",ans[i]);
	return 0;
}

posted @ 2020-03-19 23:54  EncodeTalker  阅读(149)  评论(0编辑  收藏  举报