跳到正文

个人记录

题解 P5284 [十二省联考2019]字符串问题

GuanYunchang_约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月30日
更新
2025年12月30日
篇幅
约 11 分钟

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

题意

实在无法描述,自己看题面吧……

数据范围

1≤∣S∣≤2×1051\leq \vert S \vert \leq2×10^5 ,na,nb≤2×105na,nb\leq2×10^5,m≤1×105m \leq 1×10^5

题目分析

考虑用 Sam⁡\operatorname{Sam} 解决这个问题。

首先,Sam⁡\operatorname{Sam} 不好处理前缀,所以翻转 SS,再用 Sam⁡\operatorname{Sam} 建出 Parent⁡\operatorname{Parent} 树。

将 AA 编号为 1∼na1\sim na,BB 编号为 na+1∼na+nbna+1 \sim na+nb,并在 Parent⁡\operatorname{Parent} 树用倍增找出每个A,BA,B 在树上的对应的点,设 xx 对应的点为 dxd_x。注意到一个 BjB_j 为一个 AiA_i 的后缀就是 dBjd_{B_j} 为 dAid_{A_i} 在 Parent⁡\operatorname{Parent} 树上的祖先,那么一个AxA_x 向 ByB_y 连边就是向 dByd_{B_y} 子树内的 AA 连边,所以考虑在原 Parent⁡\operatorname{Parent} 树的基础上拆点进行连边。

具体地,设 Parent⁡\operatorname{Parent} 树上一个节点 xx 被 A,BA,B 对应的点的集合为 ponxpon_x,即 ponxpon_x 中 A,BA,B 的 dd 为 xx,然后将 ponxpon_x 中的 A,BA,B 按长度为第一关键字,是否为 BB 为第二关键字排序(这样可以保证前面的 BB 为后面的 AA 的后缀),并将 xx 拆为两个点 x1,x2x_1,x_2,x1x_1 存出边,x2x_2 存入边。

对于 xx,ponxpon_x 中的每个BB 向后面的 AA 连边,直到碰到下一个 BB 为止(这个 BB 要向下一个 BB 连边);每个BB 向 x1x_1 连边,x2x_2 向每个 AA 连边;x2x_2 向 x1x_1 连边;faxfa_x 的1节点向 x2x_2 连边,x1x_1 向子节点的2节点连边;最后连题目中给的 Ax⇒ByA_x\Rightarrow B_y。

像这样:

Picture

应容易发现这样满足每个 BB 可以向 posBpos_B 子树中所有 AA 和 ponposBpon_{pos_B} 中满足 BB 为 AA 的后缀的 AA 连边,满足点数边数为O(n)O(n)。

之后就是缩点,DAG 上 DP 的 Tarjan⁡\operatorname{Tarjan} 模板了。

最后考虑怎么判-1,如果一个联通分量里有2个及以上的 AA 或 BB(必然有1个 AA),就可以输出-1了。

代码

C++
#include<bits/stdc++.h>
#define INF 1000000000007
#define _gc getchar()
#define ll long long
#define FOR(i,a,b) for(register int i=a;i<=b;i++)
#define ROF(i,b,a) for(register int i=b;i>=a;i--)
#define REP(i,x) for(int i=adj[x];i;i=e[i].next)
#define VEP(i,x) for(int i=0;i<int(x.size());i++)
using namespace std;
inline int read(){
	int x=0;char s=_gc;
	while('0'>s||s>'9')s=_gc;
	while('0'<=s&&s<='9'){x=x*10+s-48;s=_gc;}
	return x;
}

const int N=400011;
int T,na,nb,Len,m,u,v,L[N],R[N],pos[N];
ll ans=-INF;
char s[N];

namespace Graph{
	struct edge{int to,next;};
	int n,cnt,tot,ntot,indexx,deg[4*N],bel[4*N],num[4*N],dfn[4*N],low[4*N],adj[4*N],adv[4*N];
	int top,stk[4*N];
	ll f[4*N],w[4*N];
	bool book[4*N];
	edge e[8*N],ne[8*N];
	vector<int>ele[4*N];
	void creat(int x,int y){e[++tot].to=y,e[tot].next=adj[x];adj[x]=tot;}
	void ncreat(int x,int y){ne[++ntot].to=y,ne[ntot].next=adv[x];adv[x]=ntot;}
	void Tarjan(int x){
		dfn[x]=low[x]=++indexx;
		stk[++top]=x;
		REP(i,x){
			int t=e[i].to;
			if(!dfn[t]){Tarjan(t);low[x]=min(low[x],low[t]);}
			else if(!bel[t])low[x]=min(dfn[t],low[x]);
		}
		if(dfn[x]==low[x]){
			++cnt;
			int p=0;
			while(p!=x){
				p=stk[top--],bel[p]=cnt,ele[cnt].push_back(p);
				if(p<=na+nb)num[cnt]++;
				if(p<=na)w[cnt]+=R[p]-L[p]+1;
			}
		}
	}
	void DAG(){
		queue<int> q;
		FOR(i,1,cnt)if(!deg[i])q.push(i);
		while(!q.empty()){
			int tp=q.front();q.pop();
			f[tp]+=w[tp],ans=max(f[tp],ans);
			for(int i=adv[tp];i;i=ne[i].next){
				int t=ne[i].to;
				f[t]=max(f[t],f[tp]);
				deg[t]--;
				if(!deg[t])q.push(t);
			}
		}
	}
	void work(){
		FOR(i,1,n)if(!dfn[i])Tarjan(i);
		FOR(i,1,cnt)if(num[i]>1)return ;
		top=0;
		FOR(i,1,cnt){
			VEP(j,ele[i]){
				int p=ele[i][j];
				REP(k,p){
					int t=e[k].to;
					if(bel[t]!=i&&!book[bel[t]])
						book[bel[t]]=1,stk[++top]=bel[t],deg[bel[t]]++,ncreat(i,bel[t]);
				}
			}
			while(top)book[stk[top--]]=0;
		}
		DAG();
	}
	void Clear(){
		FOR(i,1,n)bel[i]=dfn[i]=low[i]=adj[i]=0;
		FOR(i,1,cnt)f[i]=w[i]=deg[i]=num[i]=adv[i]=0,book[i]=0;
		FOR(i,1,cnt){vector<int>emp;swap(emp,ele[i]);}
		n=ntot=tot=cnt=indexx=top=0;
	}
}

namespace Sam{
	int cnt=1,las=1,fa[N],anc[N][19],len[N],ch[N][26];
	vector<int>pon[N];
	int ins(int x){
		int p=las,np=(las=++cnt);
		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 {
				int nq=++cnt;
				len[nq]=len[p]+1,fa[nq]=fa[q],fa[q]=fa[np]=nq;
				FOR(i,0,25)ch[nq][i]=ch[q][i];
				while(p&&ch[p][x]==q)ch[p][x]=nq,p=fa[p];
			}
		}
		return np;
	}
	bool cmp(int x,int y){return R[x]-L[x]!=R[y]-L[y]?R[x]-L[x]<R[y]-L[y]:x>y;}
	void init1(){
		FOR(i,1,cnt)anc[i][0]=fa[i];
		FOR(j,1,18)FOR(i,1,cnt)anc[i][j]=anc[anc[i][j-1]][j-1];
	}
	void init2(){
		FOR(i,1,cnt)sort(pon[i].begin(),pon[i].end(),cmp);
		FOR(i,1,cnt){
			int p=0;
			VEP(j,pon[i]){
				int t=pon[i][j];
				if(t>na){
					if(p)Graph::creat(p,t);
					p=t,Graph::creat(p,i+cnt+na+nb);
				}
				else {
					if(p)Graph::creat(p,t);
					Graph::creat(i+na+nb,t);
				}
			}
		}
		FOR(i,2,cnt)Graph::creat(i+na+nb,i+cnt+na+nb),Graph::creat(fa[i]+cnt+na+nb,i+na+nb);
		Graph::n=2*cnt+na+nb;
		m=read();
		FOR(i,1,m)u=read(),v=read(),Graph::creat(u,v+na);
	}
	void Loc(int x){
		int p=pos[R[x]];
		ROF(i,18,0)if(R[x]-len[anc[p][i]]+1<=L[x])p=anc[p][i];
		pon[p].push_back(x);
	}
	void Clear(){
		FOR(i,1,cnt){
			fa[i]=len[i]=0;
			vector<int>emp;swap(emp,pon[i]);
			FOR(j,0,18)anc[i][j]=0;
			FOR(j,0,25)ch[i][j]=0;
		}
		cnt=las=1;
	}
}

int main(){
	T=read();
	FOR(tt,1,T){
		cin>>s+1;
		Len=strlen(s+1);
		reverse(s+1,s+Len+1);
		FOR(i,1,Len)pos[i]=Sam::ins(s[i]-'a');
		Sam::init1();
		na=read();
		FOR(i,1,na){
			L[i]=read(),R[i]=read();
			swap(L[i],R[i]);
			L[i]=Len-L[i]+1,R[i]=Len-R[i]+1;
		}
		nb=read();
		FOR(i,na+1,na+nb){
			L[i]=read(),R[i]=read();
			swap(L[i],R[i]);
			L[i]=Len-L[i]+1,R[i]=Len-R[i]+1;
		}
		FOR(i,1,na+nb)Sam::Loc(i);
		Sam::init2();
		Graph::work();
		if(ans<0)printf("-1\n");
		else printf("%lld\n",ans);
		Sam::Clear(),Graph::Clear();
		ans=-INF;
	}
	return 0;
}
原文