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 ...
随机推荐
- jenkins之 Throttle Concurrent Builds使用
Jenkins控制并发插件 Throttle Concurrent Builds介绍,管网见:https://github.com/jenkinsci/throttle-concurrent-buil ...
- ConfuserEx壳
前言: 这几天用Rolan的时候出现了点问题,然后发现了这个非常好用的工具居然只有几百k,打算逆向一下,然后发现了ConfuserEx壳 探索: Rolan是用C#写的,刚开始用EXEinfoPE打开 ...
- hdu 4544——消灭兔子
游戏规则很简单,用箭杀死免子即可. 箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di(1 <= i <= M),每种箭需要一定的QQ币 ...
- poj2109 【贪心】
Current work in cryptography involves (among other things) large prime numbers and computing powers ...
- python全栈开发 * 24 知识点汇总 * 180705
24 模块-------序列化一.什么是模块 模块:py文件就是一个模块.二.模块的分类:(1)内置模块 (登录模块,时间模块,sys模块,os模块)(2)扩展模块 (itchat 微信有关,爬虫,b ...
- LINUX常用命令 --- 权限篇
linux常用命令 linux用户权限相关 root 用户 相当于群主 超级用户 sudo命令 相当于群管理员 普通用户 群成员 查看用户id信息 使用linux ...
- java.lang.NoClassDefFoundError 错误
练习jfianl,,,配置数据库插件的时候遇到: java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource 解 ...
- ML.NET 0.10特性简介
IDataView被单独作为一个类库包 IDataView组件为表格式数据提供了非常高效的处理方式,尤其是用于机器学习和高级分析应用.它被设计为可以高效地处理高维数据和大型数据集.并且也适合处理属于更 ...
- DATEADD日期函数的使用
在当前日期加上几天:https://www.cnblogs.com/shitaotao/p/7648198.html 计算本月的第一天:https://www.cnblogs.com/lcyuhe/p ...
- 【立体几何】Journey to Jupiter Gym - 101991J 立体几何模板
https://cn.vjudge.net/problem/Gym-101991J 题目很长,其实就是给你一个正三角形,并且告诉你它的中点在Z轴上以及法向量,边长和顶点A的坐标(自由度已定),让你求A ...