自從WC之後,我好像就一直鴿着博客……
原因嗎,就是寒假作業太多,沒有時間寫博客了。
題意:開始給定一個字符串,有次操作,每次詢問一個詢問串在字符串中出現的次數,或者在字符串結尾加上一段,強制在線。
數據範圍:字符串任意時刻長度,,詢問總長$\leq 3\times 10^6,時限
題解:可能全世界只有我一個人受到了數據範圍的誤導……這個很小,但是並沒有什麼用。思路很簡單,只需要一個sam,詢問的時候進行匹配,沿着邊走就行了,最後答案就是parent樹上這個點的子樹的葉節點個數。而修改的時候就會發現我們可能需要改變parent樹的形態,這個時候我們又看到了,所以就是lct了,每次把新建的節點到原點的路徑上的點加一即可,開心的碼吧!
代碼:
#include<iostream>
#include<cassert>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cstdio>
#include<time.h>
#include<vector>
#include<algorithm>
using namespace std;
#define REP(i,x,y) for(int i=x;i<=y;i++)
#define rep(i,n) REP(i,1,n)
#define rep0(i,n) REP(i,0,n-1)
#define repG(i,x) for(int i=pos[x];~i;i=e[i].next)
#define ll long long
#define db double
const int N=1200007;
const int INF=1e9+7;
struct spl{
int son[2];
int p,lz,to,sm;
bool tn;
}t[N];
void lzd(int x){
if(t[x].tn){
swap(t[x].son[0],t[x].son[1]);
t[t[x].son[0]].tn^=1;\\懶標記下傳要記得
t[t[x].son[1]].tn^=1;
t[x].tn=0;
}
if(t[x].lz){
if(t[x].son[0])t[t[x].son[0]].lz+=t[x].lz;
if(t[x].son[1])t[t[x].son[1]].lz+=t[x].lz;
t[x].sm+=t[x].lz;
t[x].lz=0;
}
}
void rot(int x){
if(t[t[x].p].p)lzd(t[t[x].p].p);
lzd(t[x].p);
lzd(x);
bool u=(x==t[t[x].p].son[1]);//這裏先傳標記再算是哪邊的兒子
int x1=x,x2=t[x].p,x3=t[t[x].p].p,x4=t[x].son[u^1];
if(x3)t[x3].son[(x2==t[x3].son[1])]=x1;
else t[x1].to=t[x2].to,t[x2].to=0;
t[x2].p=x1;
t[x2].son[u]=x4;
t[x1].p=x3;
t[x1].son[u^1]=x2;
if(x4)t[x4].p=x2;
}
void goup(int x){while(t[x].p)rot(x); lzd(x);}
void acc(int x){
goup(x);
if(t[x].son[1]){
t[t[x].son[1]].p=0;
t[t[x].son[1]].to=x;
t[x].son[1]=0;
}
int tp=x;
while(t[tp].to){
int g=t[tp].to;
goup(g);
if(t[g].son[1]){
t[t[g].son[1]].p=0;
t[t[g].son[1]].to=g;
}
t[g].son[1]=tp;
t[tp].p=g;
t[tp].to=0;
tp=g;
}
}
void crt(int x){acc(x); goup(x); t[x].tn^=1;}//這裏比較亂,要想清楚
void lk(int x,int y){crt(x); goup(x); t[x].to=y;}
void cut(int x,int y){crt(x); acc(x); goup(y); if(t[y].to==x)t[y].to=0;}
void adw(int x,int y,int v){crt(y); acc(x); goup(x); t[x].lz+=v;}
int check(int x){goup(x); return t[x].sm;}
int cnt=1,ls=1,q,mask=0;
struct sam{
int p,l;
int to[26];
}s[N];
int ins(int x){
s[++cnt].l=s[ls].l+1;
int i=ls;
for(;i;i=s[i].p){
if(s[i].to[x])break;
s[i].to[x]=cnt;
}
if(!i){
ls=cnt;
s[ls].p=1;
lk(cnt,1);
return ls;
}
int q=s[i].to[x];
if(s[q].l==s[i].l+1){
ls=cnt;
s[ls].p=q;
lk(cnt,q);
return ls;
}
int h=cnt;
s[++cnt]=s[q];
t[cnt].sm=check(q);//這裏新建的節點也有權值
cut(q,s[q].p);
lk(cnt,s[q].p);
lk(h,cnt);
lk(q,cnt);
s[cnt].l=s[i].l+1;
s[q].p=cnt;
s[h].p=cnt;
for(;i;i=s[i].p){
if(s[i].to[x]!=q)break;
s[i].to[x]=cnt;
}
ls=h;
return ls;
}
char w[3*N];
int read(int ma){//加密的時候每次要單開一個變量
scanf("%s",w);
int n=strlen(w),fl=0;
rep(i,n-1)fl|=(w[i]!=w[i-1]);
rep0(i,n){
ma=(ma*131+i)%n;
swap(w[i],w[ma]);
}
return n;
}
int main(){
scanf("%d",&q);
scanf("%s",w+1);
int n=strlen(w+1);
rep(i,n)adw(1,ins(w[i]-'A'),1);
while(q--){
char op[10]; scanf("%s",op);
int l=read(mask);
if(op[0]=='A')rep0(i,l)adw(1,ins(w[i]-'A'),1);
else {
int nw=1;
bool fl=1;
rep0(i,l){
if(!s[nw].to[w[i]-'A']){fl=0; break;}
nw=s[nw].to[w[i]-'A'];
}
if(fl){
int v=check(nw);
printf("%d\n",v);
mask^=v;
}
else puts("0");
}
}
return 0;
}
坑點就是這個強制在線的方式。
這是新學期的第一篇博客,也是2019的第一篇博客,希望這道題能在新的學期/一年裏激勵我不怕毒瘤,繼續保齡。