P5212-SubString【LCT,SAM】

1|0正题

题目链接:https://www.luogu.com.cn/problem/P5212


1|1题目大意

开始一个字符串S,有n次操作

  1. S末尾加入一个字符串
  2. 询问一个串在S中出现了多少次

强制在线


1|2解题思路

强制在线的话,只有SAM能够支持动态插字符了,但是我们平时统计答案的时候要先做一次拓扑排序然后上传信息。

这里要动态维护parents树的话用LCT就好了,就是链修改加单点查询,然后因为根是指定的可以少写很多操作。

时间复杂度O( (n+S)log|S|)


1|3code

#include<cstdio> #include<cstring> #include<algorithm> #include<stack> using namespace std; const int N=6e5*2+10; int n,cnt,len[N],fa[N],ch[N][26]; char st[N]; struct LCT{ int t[N][2],lazy[N],w[N],fa[N]; stack<int> s; bool Nroot(int x) {return fa[x]&&((t[fa[x]][0]==x)||(t[fa[x]][1]==x));} bool Direct(int x) {return t[fa[x]][1]==x;} void Add(int x,int val) {if(x)w[x]+=val,lazy[x]+=val;return;} void PushDown(int x){ if(!lazy[x])return; if(t[x][0])Add(t[x][0],lazy[x]); if(t[x][1])Add(t[x][1],lazy[x]); lazy[x]=0;return; } void Rotate(int x){ int y=fa[x],z=fa[y]; int xs=Direct(x),ys=Direct(y); int w=t[x][xs^1]; if(Nroot(y))t[z][ys]=x; t[y][xs]=w;t[x][xs^1]=y; if(w)fa[w]=y;fa[y]=x;fa[x]=z; return; } void Splay(int x){ int y=x;s.push(x); while(Nroot(y))y=fa[y],s.push(y); while(!s.empty())PushDown(s.top()),s.pop(); while(Nroot(x)){ y=fa[x]; if(!Nroot(y))Rotate(x); else if(Direct(y)==Direct(x)) Rotate(y),Rotate(x); else Rotate(x),Rotate(x); } return; } void Access(int x){ for(int y=0;x;y=x,x=fa[x]) Splay(x),t[x][1]=y; return; } void Link(int x,int y)//x为单点,y为树 {fa[x]=y;Access(y);Splay(y);Add(y,w[x]);return;} void Cut(int x)//将x切断为单点 {Access(x);Splay(x);Add(t[x][0],-w[x]);fa[t[x][0]]=0;t[x][0]=0;return;} }T; void decode(char *s,int l,int mask) { for (int j=0;j<l;j++) { mask=(mask*131+j)%l; swap(s[j],s[mask]); } return; } int insert(int c,int p){ int np=++cnt;len[np]=len[p]+1;T.w[np]++; for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np; if(!p)fa[np]=1,T.Link(np,1); else{ int q=ch[p][c]; if(len[p]+1==len[q])fa[np]=q,T.Link(np,q); else{ int nq=++cnt;len[nq]=len[p]+1; memcpy(ch[nq],ch[q],sizeof(ch[nq])); fa[nq]=fa[q];fa[np]=fa[q]=nq; T.Cut(q);T.Link(nq,fa[nq]); T.Link(np,nq);T.Link(q,nq); for(;p&&ch[p][c]==q;p=fa[p])ch[p][c]=nq; } } return np; } int main() { scanf("%d",&n); scanf("%s",st);int l=strlen(st); int p=cnt=1;int mask=0; for(int i=0;i<l;i++) p=insert(st[i]-'A',p); while(n--){ char op[5]; scanf("%s %s",op,st);l=strlen(st); decode(st,l,mask); if(op[0]=='Q'){ int x=1; for(int i=0;i<l;i++) if(!ch[x][st[i]-'A']) {x=0;break;} else x=ch[x][st[i]-'A']; if(!x)puts("0"); else{ T.Splay(x); printf("%d\n",T.w[x]); mask^=T.w[x]; } } else{ for(int i=0;i<l;i++) p=insert(st[i]-'A',p); } } return 0; }

__EOF__

本文作者QuantAsk
本文链接https://www.cnblogs.com/QuantAsk/p/14278978.html
关于博主:退役OIer,GD划水选手
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   QuantAsk  阅读(97)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示