博客
关于我
bzoj3879: SvT 后缀自动机
阅读量:302 次
发布时间:2019-03-03

本文共 2379 字,大约阅读时间需要 7 分钟。

这一题看了一下,首先反应SAM,肯定是最近SAM做的太多了。。

那么lcp自然就是倒串求LCA啊
嗯,感觉很对
接着再一看,数据有点大
好像不太好。。
那我们有虚树啊!
于是这题就成功地水过去了
感觉这题好题啊,把我最近学的东西都用上了,当然没有线性基。。
但这也说明我最近学得都很扎实,大概可能可以熟练运用了

#include
#include
#include
#include
#include
typedef long long LL;using namespace std;const int N=500005*2;struct qq{int son[26],step,pre;}s[N];int tot,Last;char ss[N];int n,m;void ins (int x){ int p=Last,np=++tot; s[np].step=s[p].step+1; while (p!=0&&s[p].son[x]==0) s[p].son[x]=np,p=s[p].pre; if (p==0) s[np].pre=1; else { int q=s[p].son[x]; if (s[p].step+1==s[q].step) s[np].pre=q; else { int nq=++tot; s[nq]=s[q]; s[nq].step=s[p].step+1; s[q].pre=s[np].pre=nq; while (p!=0&&s[p].son[x]==q) s[p].son[x]=nq,p=s[p].pre; } } Last=np;}int pos[N];struct qr{int x,y,last;}e[N];int num,last[N];void init (int x,int y){ num++; e[num].x=x;e[num].y=y; e[num].last=last[x]; last[x]=num;}int lalal[N],cnt;int f[N][25],dep[N];int dfs (int x,int fa){ lalal[x]=++cnt; dep[x]=dep[fa]+1; f[x][0]=fa; for (int u=1;(1<
<=dep[x];u++) f[x][u]=f[f[x][u-1]][u-1]; for (int u=last[x];u!=-1;u=e[u].last) { int y=e[u].y; dfs(y,x); }}int a[N];bool cmp (int x,int y){ return lalal[x]
=0;u--) if (dep[f[x][u]]>=dep[y]) x=f[x][u]; if (x==y) return x; for (int u=21;u>=0;u--) if (f[x][u]!=f[y][u]) x=f[x][u],y=f[y][u]; return f[x][0]; }int t;void bt ()//建立虚树 { cnt=1;sta[cnt]=1; for (int u=1;u<=t;u++) { int lca=get(a[u],sta[cnt]); if (lca==sta[cnt]) sta[++cnt]=a[u]; else { while (true) { int x=sta[cnt],xx=sta[cnt-1];cnt--; if (xx==lca) { init(xx,x); break; } if (dep[xx]
'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){ x=x*10+ch-'0';ch=getchar();} return x*f;}int main(){ tot=Last=1; n=read();m=read(); scanf("%s",ss+1); for (int u=n;u>=1;u--) { ins(ss[u]-'a'); pos[u]=Last; /* printf("%d %d\n",u,pos[u]); system("pause");*/ } num=0;memset(last,-1,sizeof(last)); for (int u=2;u<=tot;u++) init(s[u].pre,u); dep[0]=0;cnt=0;dfs(1,0); memset(last,-1,sizeof(last)); for (int u=1;u<=m;u++) { num=0; t=read(); for (int u=1;u<=t;u++) { int x=read(); a[u]=pos[x]; tot1[a[u]]=1; } sort(a+1,a+1+t,cmp); int ttt=1; for (int u=2;u<=t;u++) if (a[u]!=a[ttt]) a[++ttt]=a[u]; t=ttt; bt(); ans=0; solve(1);tot1[1]=0; printf("%lld\n",ans); } return 0;}

转载地址:http://lwcq.baihongyu.com/

你可能感兴趣的文章
Web API - File - Selecting files using drag and drop
查看>>
Spring Boot - axios upload file(带请求头上传文件,非前后端分离)
查看>>
排序算法总结——c++实现
查看>>
c++ 逗号运算符
查看>>
vector的几种初始化及赋值方式
查看>>
STL基础梳理 2019.1.19(仿函数,谓词,内建函数对象,适配器,算法)
查看>>
IDEA效率快捷键收集
查看>>
并发情况下三种线程/并发安全
查看>>
希尔排序
查看>>
TreeView 中 SelectedNodeStyle 无效,selectedClass 无效的参考方案
查看>>
C#,asp.net,ashx处理session
查看>>
501 5.1.7 Invalid address
查看>>
foxmail 登录 exchange 2013 exchange 2016
查看>>
Netty高性能原理和框架架构解析
查看>>
C/C++ * & : :: -> .等特殊符号
查看>>
(00)剑指 Offer 13. 机器人的运动范围
查看>>
剑指 Offer 25. 合并两个排序的链表
查看>>
C# WinForm 监视文件变化程序
查看>>
Redis主从复制原理
查看>>
将本地已有的maven工程导入工作空间
查看>>