将每个串正着插入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(询问在线版)的更多相关文章

  1. BZOJ 3483 SGU505 Prefixes and suffixes(字典树+可持久化线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3483 [题目大意] 给出一些串,同时给出m对前缀后缀,询问有多少串满足给出的前缀后缀模 ...

  2. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  3. Codeforces 432D Prefixes and Suffixes(KMP+dp)

    题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...

  4. 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 ...

  5. Codeforces 432 D. Prefixes and Suffixes

    用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...

  6. Codeforces 1092C Prefixes and Suffixes(思维)

    题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...

  7. 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 ...

  8. Codeforces 432D Prefixes and Suffixes kmp

    手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...

  9. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes

                                                        D. Prefixes and Suffixes You have a string s = s ...

随机推荐

  1. rails获取json内容

    文章是从我的个人博客上粘贴过来的, 大家也可以访问 www.iwangzheng.com url点开后的json是这样的 { e:   { provider: ”searches.soku.top”, ...

  2. angular 强制刷新路由,重新加载路由

    angular js ui-route 在使用时默认不是不会刷新路由的,所有有些时候我们需要主动刷新路由. 主动刷新方法是: <a ui-sref="profitManage" ...

  3. &&队友最近一周水水

    100130 练习5 5 hr ago 15.2 days Private qwerqqq 100093 DP2 16 hr ago 50.2 days Private qwerqqq 100092 ...

  4. Controller之间传递数据:协议传值

    http://itjoy.org/?p=416 前边介绍过从第一个页面传递数据到第二个页面,那么反过来呢我们该如何操作?还是同一个例子,将第二个页面的字符串传递到第一个页面显示出来,这中形式就可以使用 ...

  5. VelocityTracker简介

    android.view.VelocityTracker主要用跟踪触摸屏事件(flinging事件和其他gestures手势事件)的速率.用addMovement(MotionEvent)函数将Mot ...

  6. register

    register:这个关键字请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率.注意是尽可能,不是绝对.你想想,一个CPU 的寄存器也就那么几个或几十个,你要是定义了很 ...

  7. 自定义viewgroup实现ArcMenu

    最终效果如下 实现思路 通过效果图,会有几个问题: a.动画效果如何实现 可以看出动画是从顶点外外发射的,可能有人说,那还不简单,默认元素都在定点位置,然后TraslateAnimation就好了:这 ...

  8. Liz Murray成功故事的偶然与必然(转)

    偶尔,我看到了一部电影:Homeless to Harvard (风雨哈佛路) 说句老实话,她( Liz Murray 莉丝·默里 )的经历确实让人钦佩和学习! 下面我看到他人写的有这本电影的评论,觉 ...

  9. Linux命令--文件管理

    1.ls ls -a 列出目录下是所有文件 ls -l 列出文件的详细信息   2.cd cd /root 进入更目录下的root文件夹 cd file 进入当前目录的file文件夹 cd .. 进入 ...

  10. PW试验-----verilog

    PWM,脉冲宽度调制.顾名思义,是通过调制脉冲的宽度,即占空比,来实现的.可是使占空比逐渐由最小增加到最大,也可以使占空比由最大减少到最小来实现不同的现象.若用LED灯来显示现象,则可以称作:LED呼 ...