题解:CF1801G A task for substrings
前言
复建来了。
思路
首先我们可以有一个粗略的思路就是用前缀
细节讲一下这两个步骤如何实现。
- 首先我们对于求一个前缀的答案只需要建一个关于
s_i 的 ACAM 然后我们去走每一个前缀假设走到的点为p 然后去看他在 fail 树上的父亲中有多少个为s_j 的末尾节点即可,这个用树状数组的子树加不难做到。 - 然后就只剩下如何求出每一次询问给出的
l,r 的多的答案了,我们先简单刻画一下这个串要满足什么,设这个串为t 中的L\sim R 然后满足1\leq L<l,l\leq R\leq r 那么我们在 ACAM 的体现就是存在一种把s_i 分开的方法让1\sim p 为1\sim l-1 的一个后缀并且p+1\sim len_i 为l\sim r 的一个前缀,对于前一个条件我们只需要暴力遍历每一个分界点那么要满足的就是1\sim l-1 走到的点在 fail 树上为1\sim p 的点的子树中,但是我们发现前缀并不好搞,所以我们考虑建出反串的 ACAM,至于实现在下面细讲。 - 建出了反串的 ACAM 之后,我们还是一样在上面走
p+1\sim len_i (假设走到的点为k )然后我们需要满足的就是n\to l 的点在k 的子树内,但是我们发现会存在一个串的结尾超出了r 的情况所以我们需要从n\to 1 走到的点开始网上跳一段 fail 树直到跳到一个点的dep\leq r_i-l_i+1 就停止,那么查这个点就能保证不会超出r 了,至于如何实现这个过程相信大家都会二维数点,直接数就行了。
实在不会的话只需要把充要条件写出来就行了假设
只需要注意是在两棵树上的 dfs 序就行了。
代码
写得有点史。
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define rep1(i,x,y) for(register int i=x;i>=y;--i)
#define fire signed
#define ll long long
#define il inline
template<class T> il void print(T x) {
if(x<0) printf("-"),x=-x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
int T=1;
const int N=1e6+10,M=5e5+10;
struct nn{
struct node{
int s[30];
int lst;
}tr[N];
int idx;
int len[N];
void modify(string s) {
int p=0;
for(auto to:s) {
if(!tr[p].s[to-'a']) {
tr[p].s[to-'a']=++idx;
len[idx]=len[p]+1;
}
p=tr[p].s[to-'a'];
}
}
void build() {
queue<int>q;
rep(i,0,25) if(tr[0].s[i]) q.push(tr[0].s[i]);
while(q.size()) {
int x=q.front();
q.pop();
rep(i,0,25) {
int &to=tr[x].s[i];
if(to) {
tr[to].lst=tr[tr[x].lst].s[i];
q.push(to);
}else {
tr[x].s[i]=tr[tr[x].lst].s[i];
}
}
}
}
vector<int>v[N];
int dfn[N],tot,siz[N];
int f[N][20];
void dfs(int x,int fa) {
dfn[x]=++tot;
siz[x]=1;
f[x][0]=fa;
for(auto to:v[x]) {
if(to==fa) continue;
dfs(to,x);
siz[x]+=siz[to];
}
}
void ii() {
rep(j,1,19) {
rep(i,1,tot) f[i][j]=f[f[i][j-1]][j-1];
}
}
void init() {
rep(i,0,idx) v[tr[i].lst].pb(i);
}
}t1,t2;
struct nnn{
int a,b,c;
};
vector<nnn>pl[N];
ll tr[N*5];
int lowbit(int x) {
return x&-x;
}
void add(int x,int k,int mx) {
for(;x<=mx;x+=lowbit(x)) {
tr[x]+=k;
}
}
ll Ans(int x) {
ll res=0;
for(;x;x-=lowbit(x)) res+=tr[x];
return res;
}
ll ans[M];
int n,m;
vector<int>qy[N];
string t,s[M];
ll sum[N*5];
int ll1[M],rr[M];
vector<int>idd[N*5];
int ip[M];
vector<int>id1[N*5];
void solve() {
cin>>n>>m;
cin>>t;
rep(i,1,n) cin>>s[i],t1.modify(s[i]);
rep(i,1,n) {
reverse(s[i].begin(),s[i].end());
t2.modify(s[i]);
reverse(s[i].begin(),s[i].end());
}
t1.build();
t2.build();
t1.init();
t2.init();
t1.dfs(0,0);
t2.dfs(0,0);
t2.ii();
int lt=t.size()-1;
int now=0;
rep(i,1,n) {
int p=0;
for(auto to:s[i]) {
p=t1.tr[p].s[to-'a'];
}
add(t1.dfn[p],1,t1.tot);
add(t1.dfn[p]+t1.siz[p],-1,t1.tot);
}
rep(i,0,lt) {
now=t1.tr[now].s[t[i]-'a'];
sum[i+1]=Ans(t1.dfn[now]);
}
rep(i,1,lt+1) sum[i]+=sum[i-1];
rep(i,1,m) {
int l,r;
cin>>l>>r;
ll1[i]=l,rr[i]=r;
idd[l-1].pb(i);
id1[l].pb(i);
ans[i]=sum[r]-sum[l-1];
}
now=0;
rep(i,1,lt+1) {
now=t1.tr[now].s[t[i-1]-'a'];
for(auto to:idd[i]) {
qy[t1.dfn[now]].pb(to);
}
}
rep(i,1,n) {
vector<int>v,v1;
v.clear(),v1.clear();
int p=0;
for(auto to:s[i]) {
p=t1.tr[p].s[to-'a'];
v.pb(p);
}
p=0;
reverse(s[i].begin(),s[i].end());
for(auto to:s[i]) {
p=t2.tr[p].s[to-'a'];
v1.pb(p);
}
int ll11=v.size()-1;
rep(j,0,ll11-1) {
int a=t1.dfn[v[j]],b=t1.dfn[v[j]]+t1.siz[v[j]]-1;
int c=t2.dfn[v1[ll11-1-j]],d=t2.dfn[v1[ll11-1-j]]+t2.siz[v1[ll11-1-j]]-1;
pl[a].pb({c,d,1});
pl[b+1].pb({c,d,-1});
}
}
now=0;
rep1(i,lt+1,1) {
now=t2.tr[now].s[t[i-1]-'a'];
for(auto to:id1[i]) {
ip[to]=now;
}
}
memset(tr,0,sizeof tr);
rep(i,1,t1.tot) {
for(auto to:pl[i]) {
add(to.a,to.c,t2.tot);
add(to.b+1,-to.c,t2.tot);
}
for(auto id:qy[i]) {
int now=ip[id];
int len=rr[id]-ll1[id]+1;
rep1(j,19,0) {
if(t2.f[now][j]&&t2.len[t2.f[now][j]]>len) {
now=t2.f[now][j];
}
}
if(len<t2.len[now]) now=t2.f[now][0];
ans[id]-=Ans(t2.dfn[now]);
}
}
rep(i,1,m) printf("%lld ",ans[i]);
}
fire main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(T--) {
solve();
}
return false;
}