BZOJ4212: 神牛的养成计划
题解: 可持久化trie树神题 我们考虑把字符串插入到trie树中 对于trie 树每个节点记录[l,r]表示叶子节点在dfs序中对应的区间 然后按照dfs序建可持久化trie 然后查询即可
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <stack> #include <queue> #include <cmath> #include <set> #include <map> #define mp make_pair #define pb push_back #define pii pair<int,int> #define link(x) for(edge *j=h[x];j;j=j->next) #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,r,l) for(int i=r;i>=l;i--) const int MAXN=2e6+10; const int NM=2e3+10; const double eps=1e-8; #define ll long long using namespace std; struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e; void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;} ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; } typedef struct node{ int a[26],id; }node; node d[MAXN],p[MAXN]; string str[NM],s1,s2; int rt1,cnt1,cnt2,cnt,rt[MAXN],n,m,res; int L[MAXN],R[MAXN],st[MAXN]; void newnode(int &x){ x=++cnt1;d[x].id=0; for(int i=0;i<26;i++)d[x].a[i]=0; } void zh(){ int len=s1.size(); for(int i=0;i<len;i++)s1[i]=(s1[i]-'a'+res)%26+'a'; } void zh2(){ int len=s2.size(); for(int i=0;i<len;i++)s2[i]=(s2[i]-'a'+res)%26+'a'; } void ins(string s,int id){ int len=s.size();int temp=rt1; for(int i=0;i<len;i++){ int t=s[i]-'a'; if(!d[temp].a[t])newnode(d[temp].a[t]); temp=d[temp].a[t]; //cout<<temp<<" "<<id<<" "<<s<<endl; } d[temp].id=id; } int querty(string s){ int len=s.size();int temp=rt1; for(int i=0;i<len;i++){ int t=s[i]-'a'; if(!d[temp].a[t])return -1; temp=d[temp].a[t]; //cout<<temp<<"===="<<s<<endl; } return temp; } void dfs(int x){ L[x]=cnt+1; if(d[x].id)st[++cnt]=d[x].id; for(int i=0;i<26;i++){ if(!d[x].a[i])continue; dfs(d[x].a[i]); } R[x]=cnt; } void insert(int x,string s){ int len=s.size(); for(int i=0;i<len;i++){ int t=s[i]-'a'; int y=++cnt2;p[y]=p[p[x].a[t]];p[y].id++;p[x].a[t]=y; x=p[x].a[t]; } } int querty1(string s,int x,int y){ int len=s.size(); for(int i=0;i<len;i++){ int t=s[i]-'a'; if(p[p[y].a[t]].id-p[p[x].a[t]].id==0)return -1; y=p[y].a[t];x=p[x].a[t]; } return p[y].id-p[x].id; } int main(){ ios::sync_with_stdio(false); cin>>n;newnode(rt1); inc(i,1,n)cin>>str[i],ins(str[i],i); dfs(rt1); inc(i,1,n)reverse(str[i].begin(),str[i].end()); // inc(i,1,cnt)cout<<st[i]<<" "; // cout<<endl; inc(i,1,cnt)rt[i]=++cnt2,p[rt[i]]=p[rt[i-1]],insert(rt[i],str[st[i]]); cin>>m; while(m--){ cin>>s1>>s2; // cout<<s1<<" "<<s2<<endl; zh();zh2(); int t1=querty(s1); if(t1==-1){res=0;printf("%d\n",res);continue;} reverse(s2.begin(),s2.end()); int t2=querty1(s2,rt[L[t1]-1],rt[R[t1]]); if(t2==-1){res=0;printf("%d\n",res);continue;} res=t2;printf("%d\n",res); } return 0; }
4212: 神牛的养成计划
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 309 Solved: 97
[Submit][Status][Discuss]
Description
Hzwer成功培育出神牛细胞,可最终培育出的生物体却让他大失所望......
后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变,原先决定神牛特征的基因序列都被破坏了,神牛hzwer很生气,但他知道基因突变的低频性,说不定还有以下优秀基因没有突变,那么他就可以用限制性核酸内切酶把它们切出来,然后再构建基因表达载体什么的,后面你懂的......
黄学长现在知道了N个细胞的DNA序列,它们是若干个由小写字母组成的字符串。一个优秀的基因是两个字符串s1和s2,当且仅当s1是某序列的前缀的同时,s2是这个序列的后缀时,hzwer认为这个序列拥有这个优秀基因。
现在黄学长知道了M个优秀基因s1和s2,它们想知道对于给定的优秀基因,有多少个细胞的DNA序列拥有它。
Input
第一行:N,表示序列数
接下来N行,每行一个字符串,代表N个DNA序列,它们的总长为L1
接下来一个M,表示询问数
接下来M行,每行两个字符串s1和s2,由一个空格隔开,hzwer希望你能在线回答询问,所以s1等于“s1”的所有字符按字母表的顺序向后移动ans位(字母表是一个环),ans为上一个询问的答案,s2同理。例如ans=2 “s1”=qz
则s1=sb。对于第一个询问,ans=0
s1和s2的总长度为L2
Output
输出M行,每行一个数,第i行的数表示有多少个序列拥有第i个优秀基因。
Sample Input
10
emikuqihgokuhsywlmqemihhpgijkxdukjfmlqlwrpzgwrwozkmlixyxniutssasrriafu
emikuqihgokuookbqaaoyiorpfdetaeduogebnolonaoehthfaypbeiutssasrriafu
emikuqihgokuorocifwwymkcyqevdtglszfzgycbgnpomvlzppwrigowekufjwiiaxniutssasrriafu
emikuqihgokuorociysgfkzpgnotajcfjctjqgjeeiheqrepbpakmlixyxniutssasrriafu
emikuqihgokuorociysgfrhulymdxsqirjrfbngwszuyibuixyxniutssasrriafu
emikuqihgokuorguowwiozcgjetmyokqdrqxzigohiutssasrriafu
emikuqihgokuorociysgsczejjmlbwhandxqwknutzgdmxtiutssasrriafu
emikuqihgokuorociysgvzfcdxdiwdztolopdnboxfvqzfzxtpecxcbrklvtyxniutssasrriafu
emikuqihgokuorocsbtlyuosppxuzkjafbhsayenxsdmkmlixyxniutssasrriafu
emikuqihgokuorociysgfjvaikktsixmhaasbvnsvmkntgmoygfxypktjxjdkliixyxniutssasrriafu
10
emikuqihgokuorociysg yxniutssasrriafu
aiegqmedckgqknky eqpoowonnewbq
xfbdnjbazhdnhkhvb qrqgbnmlltlkkbtyn
bjfhrnfedlhrlolzfv qppxpoofxcr
zhdfpldcbjf stsidponnvnmmdvap
zhdfpldcbjfpjmjxdt gdstsidponnvnmmdvap
dlhjtphgfnjtnqnbhxr wxwmhtsrrzrqqhzet
bjfhrnfedlhrlolzfv frqppxpoofxcr
zhdfpldcbjf dponnvnmmdvap
ucyakgyxweakehes nondykjiiqihhyqvk
emikuqihgokuhsywlmqemihhpgijkxdukjfmlqlwrpzgwrwozkmlixyxniutssasrriafu
emikuqihgokuookbqaaoyiorpfdetaeduogebnolonaoehthfaypbeiutssasrriafu
emikuqihgokuorocifwwymkcyqevdtglszfzgycbgnpomvlzppwrigowekufjwiiaxniutssasrriafu
emikuqihgokuorociysgfkzpgnotajcfjctjqgjeeiheqrepbpakmlixyxniutssasrriafu
emikuqihgokuorociysgfrhulymdxsqirjrfbngwszuyibuixyxniutssasrriafu
emikuqihgokuorguowwiozcgjetmyokqdrqxzigohiutssasrriafu
emikuqihgokuorociysgsczejjmlbwhandxqwknutzgdmxtiutssasrriafu
emikuqihgokuorociysgvzfcdxdiwdztolopdnboxfvqzfzxtpecxcbrklvtyxniutssasrriafu
emikuqihgokuorocsbtlyuosppxuzkjafbhsayenxsdmkmlixyxniutssasrriafu
emikuqihgokuorociysgfjvaikktsixmhaasbvnsvmkntgmoygfxypktjxjdkliixyxniutssasrriafu
10
emikuqihgokuorociysg yxniutssasrriafu
aiegqmedckgqknky eqpoowonnewbq
xfbdnjbazhdnhkhvb qrqgbnmlltlkkbtyn
bjfhrnfedlhrlolzfv qppxpoofxcr
zhdfpldcbjf stsidponnvnmmdvap
zhdfpldcbjfpjmjxdt gdstsidponnvnmmdvap
dlhjtphgfnjtnqnbhxr wxwmhtsrrzrqqhzet
bjfhrnfedlhrlolzfv frqppxpoofxcr
zhdfpldcbjf dponnvnmmdvap
ucyakgyxweakehes nondykjiiqihhyqvk
Sample Output
4
7
3
5
5
1
3
5
10
4
7
3
5
5
1
3
5
10
4
HINT
N<=2000
L1<=2000000
M<=100000
L2<=2000000