Codeforces 1073G. Yet Another LCP Problem SAM+虚树

本文介绍了一种解决字符串匹配问题的高效算法——使用后缀数组构建SAM树,并通过虚树优化求解多个询问的LCALCALCA权值和问题。该方法将后缀的LCPLCPLCP转换为前缀的LCSLCSLCS,通过SAM树将问题转化为树上的统计问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Solution

很套路的一道题。
后缀的LCPLCPLCP可以变为前缀的LCSLCSLCS,那么建出SAM之后就转化为一个树上问题,即统计所有给出点对的LCALCALCA权值和,由于多个询问,建虚树即可。

Code

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
const int Maxn=200010;
const int inf=2147483647;
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<<3)+(x<<1)+(ch^48),ch=getchar();
	return x*f;
}
char s[Maxn];
int n,q,lst=1,tot=1,mx[Maxn<<1],son[Maxn<<1][26],par[Maxn<<1];
int dfn[Maxn<<1],DFN=0,dep[Maxn<<1],fa[Maxn<<1][19],to[Maxn];
void extend(int x)
{
	int p=lst,np=++tot;mx[np]=mx[p]+1;
	while(p&&!son[p][x])son[p][x]=np,p=par[p];
	if(!p)par[np]=1;
	else
	{
		int q=son[p][x];
		if(mx[p]+1==mx[q])par[np]=q;
		else
		{
			int nq=++tot;mx[nq]=mx[p]+1;
			for(int i=0;i<26;i++)son[nq][i]=son[q][i];
			par[nq]=par[q];
			par[q]=par[np]=nq;
			while(son[p][x]==q)son[p][x]=nq,p=par[p];
		}
	}
	lst=np;
}
struct Edge{int y,next;}e[Maxn<<1],E[Maxn<<1];
int last[Maxn<<1],len=0;
void ins(int x,int y)
{
	int t=++len;
	e[t].y=y;e[t].next=last[x];last[x]=t;
}
int Last[Maxn<<1],Len;
void Ins(int x,int y)
{
	int t=++Len;
	E[t].y=y;E[t].next=Last[x];Last[x]=t;
}
void dfs(int x)
{
	dfn[x]=++DFN;
	for(int i=1;(1<<i)<=dep[x];i++)fa[x][i]=fa[fa[x][i-1]][i-1];
	for(int i=last[x];i;i=e[i].next)
	{
		int y=e[i].y;
		fa[y][0]=x;dep[y]=dep[x]+1;
		dfs(y);
	}
}
int LCA(int x,int y)
{
	if(dep[x]<dep[y])swap(x,y);
	for(int i=18;i>=0;i--)
	if((1<<i)<=dep[x]-dep[y])x=fa[x][i];
	if(x==y)return x;
	for(int i=18;i>=0;i--)
	if((1<<i)<=dep[x]&&fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
	return fa[x][0];
}
int k,l,a[Maxn],b[Maxn],cnt[2][Maxn<<1];
struct Node{int x,o;}A[Maxn<<1];
bool cmp(Node a,Node b){return dfn[a.x]<dfn[b.x];}
int sta[Maxn<<1],top;
LL ans;int tmp[Maxn<<1],lt;
void build()
{
	sort(A+1,A+1+k+l,cmp);
	sta[top=1]=1;Len=lt=0;
	for(int i=1;i<=k+l;i++)
	{
		cnt[A[i].o][A[i].x]++;
		if(i>1&&A[i].x==A[i-1].x)continue;
		int t=LCA(A[i].x,sta[top]);
		if(t!=sta[top])
		{
			while(top>1&&dep[sta[top-1]]>=dep[t])Ins(sta[top-1],sta[top]),top--;
			if(sta[top]!=t)Ins(t,sta[top]),sta[top]=t;
		}
		sta[++top]=A[i].x;
	}
	while(top>1)Ins(sta[top-1],sta[top]),top--;
}
void DFS(int x)
{
	tmp[++lt]=x;
	ans+=(LL)mx[x]*cnt[0][x]*cnt[1][x];
	for(int i=Last[x];i;i=E[i].next)
	{
		int y=E[i].y;
		DFS(y);
		ans+=(LL)mx[x]*cnt[0][y]*cnt[1][x];
		ans+=(LL)mx[x]*cnt[0][x]*cnt[1][y];
		cnt[0][x]+=cnt[0][y],cnt[1][x]+=cnt[1][y];
	}
}
int main()
{
	n=read(),q=read();
	scanf("%s",s+1);
	for(int i=n;i;i--)extend(s[i]-'a');
	for(int i=2;i<=tot;i++)ins(par[i],i);
	dep[1]=0;dfs(1);
	int p=1;
	for(int i=n;i;i--)
	{
		int x=s[i]-'a';
		p=son[p][x];to[i]=p;
	}
	while(q--)
	{
		k=read(),l=read();
		for(int i=1;i<=k;i++)A[i].x=to[read()],A[i].o=0;
		for(int i=1;i<=l;i++)A[k+i].x=to[read()],A[k+i].o=1;
		build();
		ans=0;DFS(1);
		for(int i=1;i<=lt;i++)
		{
			int x=tmp[i];
			cnt[0][x]=cnt[1][x]=Last[x]=0;
		}
		printf("%lld\n",ans);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值