BZOJ3483 : SGU505 Prefixes and suffixes(询问在线版)
将每个串正着插入Trie A中,倒着插入Trie B中。
并求出每个串在A,B中的dfs序。
每次查询等价于查询在A中dfs序在[la,ra]之间,在B中dfs序在[lb,rb]之间的串的个数,用主席树维护即可。
#include<cstdio>
const int S=2000010,N=2010,M=N*22;
char s[S],ch;
int n,m,i,j,k,posa[N],posb[N],g[S],v[N],nxt[N],ed,la,lb,ra,rb,ans;
inline void read(){
while(!(((ch=getchar())>='a')&&(ch<='z')));
s[k=1]=(ch-'a'+ans)%26;
while(((ch=getchar())>='a')&&(ch<='z'))s[++k]=(ch-'a'+ans)%26;
}
inline void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
struct Trie{
int tot,son[S][26],st[S],en[S],dfn;
inline int ins0(){
int x=0;
for(int i=1;i<=k;i++){
if(!son[x][s[i]])son[x][s[i]]=++tot;
x=son[x][s[i]];
}
return x;
}
inline int ins1(){
int x=0;
for(int i=k;i;i--){
if(!son[x][s[i]])son[x][s[i]]=++tot;
x=son[x][s[i]];
}
return x;
}
void dfs(int x){
st[x]=++dfn;
for(int i=0;i<26;i++)if(son[x][i])dfs(son[x][i]);
en[x]=dfn;
}
inline void ask0(){
int x=0;
for(int i=1;i<=k;i++){
if(!son[x][s[i]]){la=ra=0;return;}
x=son[x][s[i]];
}
la=st[x],ra=en[x];
}
inline void ask1(){
int x=0;
for(int i=k;i;i--){
if(!son[x][s[i]]){lb=rb=0;return;}
x=son[x][s[i]];
}
lb=st[x],rb=en[x];
}
}A,B;
int l[M],r[M],val[M],tot,T[S];
int ins(int x,int a,int b,int c){
int y=++tot;val[y]=val[x]+1;
if(a==b)return y;
int mid=(a+b)>>1;
if(c<=mid)l[y]=ins(l[x],a,mid,c),r[y]=r[x];else l[y]=l[x],r[y]=ins(r[x],mid+1,b,c);
return y;
}
int ask(int x,int a,int b){
if(!x)return 0;
if(lb<=a&&b<=rb)return val[x];
int mid=(a+b)>>1,t=0;
if(lb<=mid)t=ask(l[x],a,mid);
if(rb>mid)t+=ask(r[x],mid+1,b);
return t;
}
int main(){
scanf("%d",&n);
for(i=1;i<=n;i++){
read();
posa[i]=A.ins0();
posb[i]=B.ins1();
}
A.dfs(0),B.dfs(0);
for(i=1;i<=n;i++)add(A.st[posa[i]],B.st[posb[i]]);
for(i=1;i<=A.dfn;i++)for(T[i]=T[i-1],j=g[i];j;j=nxt[j])T[i]=ins(T[i],1,B.dfn,v[j]);
scanf("%d",&m);
while(m--){
read(),A.ask0();
read(),B.ask1();
if(la&&lb)ans=ask(T[ra],1,B.dfn)-ask(T[la-1],1,B.dfn);else ans=0;
printf("%d\n",ans);
}
return 0;
}
BZOJ3483 : SGU505 Prefixes and suffixes(询问在线版)的更多相关文章
- BZOJ 3483 SGU505 Prefixes and suffixes(字典树+可持久化线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3483 [题目大意] 给出一些串,同时给出m对前缀后缀,询问有多少串满足给出的前缀后缀模 ...
- codeforces432D Prefixes and Suffixes(kmp+dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Prefixes and Suffixes You have a strin ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)
D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 432 D. Prefixes and Suffixes
用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- CodeForces Round #527 (Div3) C. Prefixes and Suffixes
http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
D. Prefixes and Suffixes You have a string s = s ...
随机推荐
- Android使用OkHttp实现带进度的上传下载
先贴上MainActivity.java package cn.edu.zafu.sample; import android.os.Bundle; import android.support.v7 ...
- gmm
参考大神的博文:http://www.cnblogs.com/tornadomeet/archive/2012/06/02/2531565.html http://www.cnblogs.com/to ...
- HDU 1176免费馅饼 DP数塔问题转化
L - 免费馅饼 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- Windows命令行提取日期时间
参考: http://elicecn.blog.163.com/blog/static/174017473200931910320556/ SET str="%date:~0,4%%date ...
- FileOutputStream与FileInputStream互相转换
List<InstorageNoticeDto> noticeList = null; FileOutputStream fos = null; FileInputStream is = ...
- 明白python文件如何组织,理解建立源文件
在Python 中引用是非常简单的事情,这里需要清楚三个概念就可以了包.模块.类.类这个就不用说了. 模块对应的是一个.py 文件,那么module_name 就是这个文件去掉.py 之后的文件名,p ...
- Java for LeetCode 076 Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- Ubuntu 12.10 安装 jdk-7u10-linux-x64.tar.gz(转载)
在Ubuntu 12.10下安装 jdk-7u10-linux-x64.tar.gz 总的原则:将jdk-7u10-linux-x64.tar.gz压缩包解压至/usr/lib/jdk,设置jdk环境 ...
- php 正则表达式
<?php //正则表达式 //定界符:斜杠:/正则/ //匹配开始:^ //匹配结束:$ /*\d代表一个数字 \w代表一个单词 */ $zz = "/(13[0-9]|14[5|7 ...
- 在Android4.4上新增加keycode
keycode是android定义好的,但是有时候无法满足需要,进行定制化难免会涉及到新增加keycode.分成两部分,驱动和framework,这里主要讲解framework部分: 一.驱动部分: ...