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 ...
随机推荐
- SQL查询无限层级结构的所有下级,所有上级
无限层级结构的table1表,Id(主键),ParentId(父级id)查询某个Id的所有下级或所有上级,使用WITH AS,UNION ALL 查询 1.查询Id为1所有的下级 WITH T AS( ...
- 使用RecyclerView设置自定义分割线
在安卓开发中,RecyclerView控件来做一些列表是非常方便的,如何使用在网上很多文章,这次着重来说一下怎么设置自定义分割线: 首先,我们来看一下怎么设置默认的分割线 RecyclerView m ...
- 【CF429E】Points and Segments 欧拉回路
[CF429E]Points and Segments 题意:给你数轴上的n条线段$[l_i,r_i]$,你要给每条线段确定一个权值+1/-1,使得:对于数轴上的任一个点,所有包含它的线段的权值和只能 ...
- css 控制文字超出部分显示省略号
该文章转自:http://www.daqianduan.com/6179.html 如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽 ...
- centos 安装 python36
centos6 安装 python36 临时方法: https://www.softwarecollections.org/en/scls/rhscl/rh-python36/ 方法二: http:/ ...
- 唉,可爱的小朋友---(DFS)
唉,小朋友是比较麻烦的.在一个幼儿园里,老师要上一节游戏课,有N个小朋友要玩游戏,做游戏时要用小皮球,但是幼儿园里只有M个小皮球,而且有些小朋友不喜欢和一些小朋友在一起玩,而只喜欢和另一些小朋友一起玩 ...
- yum安装openjdk
含有的命令:yum,java -version,javac,source,vim [root@ycj ~]# yum -y install java-1.8.0-openjdk-devel //安装j ...
- Gym 101194L / UVALive 7908 - World Cup - [三进制状压暴力枚举][2016 EC-Final Problem L]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- php 延迟静态绑定: static关键字
abstract class DomainObject { public static function create() { return new self(); } } class User ex ...
- MSMQ 跨服务器读写队列的“消息队列系统的访问被拒绝”的解决方案
转自https://www.cnblogs.com/jyz/articles/4612333.html 最近项目中需要跨服务器对消息队列进行读写,开始在单独开发机器上进行Queue的读写没问题.但是部 ...