codeforces #305 E Mike and friends
原问题可以转化为:给定第k个字符串,求它在L-R的字符串里作为子串出现了多少次
定义子串为字符串的某个前缀的某个后缀(废话)
等价于我们把一个字符串插入到trie里,其过程中每个经过的节点和其向上的fail链上的点都是该字符串的子串
又因为对于一条fail链,u向上能访问到v当前仅当u在v的子树内
那么原问题又变成了:
将L-R个字符串按照上述方法插入到trie中并将经过的节点的val值增加
求第k个字符串对应的单词节点在fail树上的子树的权值和
又因为查询的信息满足区间可减性,所以我们可以建出fail树
对fail树用可持久化线段树维护DFS序 完成单点修改和子树查询
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; const int maxn=500010;
int n,m,sum=0;
int L,R,k;
int pos[maxn];
int rt[maxn];
char s[maxn];
queue<int>Q; int h[maxn],cnt=0;
int A[maxn],B[maxn],tot=0;
struct edge{
int to,next;
}G[maxn<<1]; void add(int x,int y){++cnt;G[cnt].to=y;G[cnt].next=h[x];h[x]=cnt;}
void Get_DFS(int u){
A[u]=++tot;
for(int i=h[u];i;i=G[i].next)Get_DFS(G[i].to);
B[u]=tot;
} struct Seg_Tree{
int L,R,v;
}t[11000010]; void build(int &o,int L,int R){
o=++sum;
if(L==R)return;
int mid=(L+R)>>1;
build(t[o].L,L,mid);
build(t[o].R,mid+1,R);
}
void modify(int &o,int L,int R,int p){
t[++sum]=t[o];o=sum;
if(L==R){t[o].v++;return;}
int mid=(L+R)>>1;
if(p<=mid)modify(t[o].L,L,mid,p);
else modify(t[o].R,mid+1,R,p);
t[o].v=t[t[o].L].v+t[t[o].R].v;
}
int ask(int o,int L,int R,int x,int y){
if(L>=x&&R<=y)return t[o].v;
int mid=(L+R)>>1;
if(y<=mid)return ask(t[o].L,L,mid,x,y);
else if(x>mid)return ask(t[o].R,mid+1,R,x,y);
else return ask(t[o].L,L,mid,x,y)+ask(t[o].R,mid+1,R,x,y);
} struct Trie{
int cnt;
int t[maxn][26];
int fail[maxn],fa[maxn];
void init(){
cnt=1;fail[0]=1;
for(int i=0;i<26;++i)t[0][i]=1;
}
int insert(){
int len=strlen(s+1);
int now=1;
for(int i=1;i<=len;++i){
int id=s[i]-'a';
if(!t[now][id])t[now][id]=++cnt,fa[t[now][id]]=now;
now=t[now][id];
}return now;
}
void build_fail(){
Q.push(1);fail[1]=0;
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int i=0;i<26;++i){
if(t[u][i]){
int k=fail[u];
while(!t[k][i])k=fail[k];
fail[t[u][i]]=t[k][i];
add(t[k][i],t[u][i]);
Q.push(t[u][i]);
}
}
}return;
}
void UPD(){
build(rt[0],1,cnt);
for(int i=1;i<=n;++i){
rt[i]=rt[i-1];
for(int j=pos[i];j!=1;j=fa[j]){
modify(rt[i],1,cnt,A[j]);
}
}return;
}
}AC; int main(){
scanf("%d%d",&n,&m);
AC.init();
for(int i=1;i<=n;++i){
scanf("%s",s+1);
pos[i]=AC.insert();
}AC.build_fail();Get_DFS(1);AC.UPD();
for(int i=1;i<=m;++i){
scanf("%d%d%d",&L,&R,&k);
printf("%d\n",ask(rt[R],1,AC.cnt,A[pos[k]],B[pos[k]])-ask(rt[L-1],1,AC.cnt,A[pos[k]],B[pos[k]]));
}return 0;
}
即可
codeforces #305 E Mike and friends的更多相关文章
- codeforces #305 A Mike and Frog
挺简单的题目,但是有一堆恶心的边界 在刨去恶心的边界之后: 假定我们知道两边的循环节为b1,b2 其中h第一次到达目标的时间为a1,a2 又知道对于答案t t=a1+b1*t1=a2+b2*t2 不妨 ...
- codeforces #305 B Mike and Feet
跟之前做过的51Nod的移数博弈是一样的QAQ 我们考虑每个数的贡献 定义其左边第一个比他小的数的位置为L 定义其右边第一个比他小的数的位置为R 这个可以用排序+链表 或者 单调队列 搞定 那么对于区 ...
- codeforces #305 D Mike and Fish
正解貌似是大暴搜? 首先我们考虑这是一个二分图,建立网络流模型后很容易得出一个算法 S->行 容量为Num[X]/2; 行->列 容量为1 且要求(x,y)这个点存在 列->T 容量 ...
- codeforces #305 C Mike and Foam
首先我们注意到ai<=50w 因为2*3*5*7*11*13*17=510510 所以其最多含有6个质因子 我们将每个数的贡献分离, 添加就等于加上了跟这个数相关的互素对 删除就等于减去了跟这个 ...
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
- (CodeForces 548B 暴力) Mike and Fun
http://codeforces.com/problemset/problem/548/B Mike and some bears are playing a game just for fun. ...
- codeforces 361 E - Mike and Geometry Problem
原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...
- codeforces 361 A - Mike and Cellphone
原题: Description While swimming at the beach, Mike has accidentally dropped his cellphone into the wa ...
- codeforces 361 B - Mike and Shortcuts
原题: Description Recently, Mike was very busy with studying for exams and contests. Now he is going t ...
随机推荐
- k8s(5)-拓展服务
在之前我们创建了一个部署,然后通过服务公开它.部署只创建了一个Pod来运行我们的应用程序.当流量增加时,我们需要扩展应用程序以满足用户需求. 通过更改部署中的副本数来完成扩展. 1. 拓展部署 这里将 ...
- 《转载》RPC入门总结(一)RPC定义和原理
转载:深入浅出 RPC - 浅出篇 转载:RPC框架与Dubbo完整使用 转载:深入浅出 RPC - 深入篇 转载:远程调用服务(RPC)和消息队列(Message Queue)对比及其适用/不适用场 ...
- 关于SpringBoot 2.0,Pageable 无法注入,提示缺少默认构造方法的解决办法
在SpringBoot 2.0 以前,我们会配置以下类 * @date 2018/06/03 */ @Configuration public class WebMvcConfig extends W ...
- python 笔记 week1-- if while for
1.添加环境变量 windows要加环境变量.linux若升级版本不一致, #!/usr/bin/env python 调用环境变量中的python #!/usr/bin/python 调用系统中默认 ...
- 10.17小结:table.copy() 和 distinct 查询
1. 当datatable 已存在于一个dataset中时,可以使用 ds.tables.add(dt.copy()) 来向dataset 中添加datatable; 2. 当datarow已存在于一 ...
- ThinkPHP 缓存技术详解 使用大S方法
如果没有缓存的网站是百万级或者千万级的访问量,会给数据库或者服务器造成很大的压力,通过缓存,大幅减少服务器和数据库的负荷,假如我们把读取数据的过程分为三个层,第一个是访问层,第一个是缓存层,第三个是数 ...
- Echarts . 在柱状图中添加自定义值 (键值对)
x ["需求"] {"0":"使用Echarts根据数据加载一个饼状图"} {"1":"点击哪个饼状图,弹出此 ...
- arcpy加载mxd文件时,无效的MXD路径,提示assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
无效的MXD路径,将路径前加‘u’,改为这种: mxdPath = u"C:\\1331\\DB\\Original Files\\dd.mxd" 参考: https://gis. ...
- 表驱动方法(Table-Driven Methods)
表驱动方法(Table-Driven Methods) - winner_0715 - 博客园 https://www.cnblogs.com/winner-0715/p/9382048.html W ...
- http 你造吗?
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...