CodeForces 547E Mike and Friends [后缀树组+主席树]
来源:互联网 发布:911色主站新域名 编辑:程序博客网 时间:2024/05/16 05:52
Description
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!
In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n.
Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).
Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF.
The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number
题意:给出N个串,M个询问,对于每个询问L,R,K,给出串L到串R的所有串中串K作为子串出现了几次。
解法:正解是AC自动机建Fail树然后根据DFS序乱搞,但是脑洞太小,只能想出比较朴素的后缀数组+主席树的解法。
可知,将所有串并成一个串做完SA后,是可以二分求出每个串作为子串的区间(Lx , Rx)的 (SA中的下标进行二分),二分过程只要比较一下LCP与原串的长度即可。然后只要询问这个区间中所有的后缀所属于的串的编号,在询问的区间(L,R)中的个数。这个过程可以用主席树实现。比较卡常数,不过稍微改下就能过。
代码: (这里后缀数组使用了DC3实现)
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<iostream>#include<stdlib.h>#include<set>#include<map>#include<queue>#include<vector>#include<bitset>#pragma comment(linker, "/STACK:1024000000,1024000000")template <class T>bool scanff(T &ret){ //Faster Input char c; int sgn; T bit=0.1; if(c=getchar(),c==EOF) return 0; while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); if(c==' '||c=='\n'){ ret*=sgn; return 1; } while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10; ret*=sgn; return 1;}#define inf 1073741823#define llinf 4611686018427387903LL#define PI acos(-1.0)#define lth (th<<1)#define rth (th<<1|1)#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)#define mem(x,val) memset(x,val,sizeof(x))#define mkp(a,b) make_pair(a,b)#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define pb(x) push_back(x)using namespace std;typedef long long ll;typedef pair<int,int> pii;#define lowbit(x) (x&(-x))#define NN 800040#define SIZZ 256#define F(x) ((x)/3+((x)%3==1?0:tb))#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)int aa[3*NN],bb[NN],cc[NN],xx[NN],yy[NN];int sa[3*NN],height[NN],rank[NN];int hf[NN][25];char s[NN];int c0(int *r,int a,int b){return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];}int c12(int k,int *r,int a,int b){if(k==2)return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);else return r[a]<r[b]||r[a]==r[b]&&cc[a+1]<cc[b+1];}void sort(int *r,int *a,int *b,int n,int m){int i;for(i=0;i<n;i++)cc[i]=r[a[i]];for(i=0;i<m;i++)bb[i]=0;for(i=0;i<n;i++)bb[cc[i]]++;for(i=1;i<m;i++)bb[i]+=bb[i-1];for(i=n-1;i>=0;i--)b[--bb[cc[i]]]=a[i];}void dc3(int *r,int *sa,int n,int m){int i,j,ta=0,tb=(n+1)/3,tc=0,p;int *rn=r+n,*sn=sa+n;r[n]=r[n+1]=0;for(i=0;i<n;i++)if(i%3!=0)xx[tc++]=i;sort(r+2,xx,yy,tc,m);sort(r+1,yy,xx,tc,m);sort(r,xx,yy,tc,m);for(p=1,rn[F(yy[0])]=0,i=1;i<tc;i++)rn[F(yy[i])]=c0(r,yy[i-1],yy[i])?p-1:p++;if(p<tc)dc3(rn,sn,tc,p);else for(i=0;i<tc;i++)sn[rn[i]]=i;for(i=0;i<tc;i++)if(sn[i]<tb)yy[ta++]=sn[i]*3;if(n%3==1)yy[ta++]=n-1;sort(r,yy,xx,ta,m);for(i=0;i<tc;i++)cc[yy[i]=G(sn[i])]=i;for(i=0,j=0,p=0;i<ta&&j<tc;p++)sa[p]=c12(yy[j]%3,r,xx[i],yy[j])?xx[i++]:yy[j++];for(;i<ta;p++)sa[p]=xx[i++];for(;j<tc;p++)sa[p]=yy[j++];}void calh(int n){int i,j,k=0;for(i=0;i<n;i++)rank[sa[i]]=i;for(i=0;i<n;i++){if(k)k--;j=sa[rank[i]-1];if(rank[i]-1<0)continue;while(i+k<n-1&&j+k<n-1&&aa[i+k]==aa[j+k])k++;height[rank[i]]=k;}for(i=0;i<n;i++)hf[i][0]=height[i];for(j=1;j<=log(n*1.0)/log(2.0);j++){for(i=0;i<n;i++){if(i+(1<<j)<n){hf[i][j]=min(hf[i][j-1],hf[i+(1<<(j-1))][j-1]);}}}}int lcp(int x,int y){int i=rank[x];int j=rank[y];if(j<i)swap(i,j);int k=log(j-i)*1.0/log(2.0);return min(hf[i+1][k],hf[j-(1<<k)+1][k]);}int n,m,qn,ql,qr;int len[NN];int idx[NN];int belong[NN];#define MM 10000000int tot=0;int t[NN];int c[MM],lson[MM],rson[MM];int build(int l,int r){ int x=++tot; if(l!=r){ int m=(l+r)>>1; lson[x]=build(l,m); rson[x]=build(m+1,r); } return x;}void update(int l,int r,int pos,int pre,int cur,int val){ lson[cur]=lson[pre]; rson[cur]=rson[pre]; c[cur]=c[pre]+val; if(l==r)return; int m=(l+r)>>1; if(pos<=m)update(l,m,pos,lson[pre],lson[cur]=++tot,val); else update(m+1,r,pos,rson[pre],rson[cur]=++tot,val);}int gettop(int idx,int len){ int l=0,r=idx; while(l+1<r){ int m=(l+r)>>1; if(lcp(sa[m],sa[idx])>=len)r=m; else l=m; } if(lcp(sa[l],sa[idx])>=len)return l; return r;}int getbutton(int idx,int len){ int l=idx,r=n-1; while(l+1<r){ int m=(l+r)>>1; if(lcp(sa[m],sa[idx])>=len)l=m; else r=m; } if(lcp(sa[r],sa[idx])>=len)return r; return l;}int query(int l,int r,int x){ if(l>=ql&&r<=qr)return c[x]; int sum=0; int m=(l+r)>>1; if(m>=ql)sum+=query(l,m,lson[x]); if(m<qr)sum+=query(m+1,r,rson[x]); return sum;}int lx[NN];int rx[NN];main(){ n=0; scanff(m); scanff(qn);rep(i,1,m){scanf("%s",s);len[i]=strlen(s);idx[i]=n;rep(j,0,len[i]-1){ aa[n++]=s[j]; belong[n-1]=i;}aa[n++]=SIZZ+i;belong[n-1]=i;}dc3(aa,sa,n,SIZZ+m+10);calh(n);build(1,m);rep(i,0,n)t[i]=1; rep(i,1,n){ if(aa[sa[i-1]]>=SIZZ){ n=i; break; } t[i]=++tot; update(1,m,belong[sa[i-1]],t[i-1],t[i],1); } rep(i,1,qn){ int k; scanff(ql); scanff(qr); scanff(k); int l,r; if(lx[k])l=lx[k]; else l=gettop(rank[idx[k]],len[k])+1,lx[k]=l; if(rx[k])r=rx[k]; else r=getbutton(rank[idx[k]],len[k])+1,rx[k]=r; printf("%d\n",query(1,m,t[r])-query(1,m,t[l-1])); }}
- CodeForces 547E Mike and Friends [后缀树组+主席树]
- CodeForces 547E Mike and Friends [Fail树+树状数组]
- 【codeforces】Codeforces Round #305 (Div. 1)E. Mike and Friends【后缀数组+线段树】
- Codeforces Round #305 (Div. 1)E. Mike and Friends 后缀数组+RMQ+线段树
- Codeforces Round #305 (Div. 1)E. Mike and Friends【后缀数组+线段树】
- 字符串k在第li到第ri个字符串中一共出现了几次 后缀数组+线段树 Codeforces Div. 1E. Mike and Friends
- 【后缀自动机】Codeforces Round #305 (Div. 1) E. Mike and Friends
- Codeforces 689E Mike and Geometry Problem【离散化+线段树+组合数】
- 【codeforces 548E】Mike and Foam
- CodeForces 547B. Mike and Feet 线段树
- Codeforces Gym-101161E【LCA+主席树】
- Codeforces 813E Army Creation [主席树]
- Codeforces 453E Little Pony and Lord Tirek (Splay + 主席树)
- CodeForces 669E Little Artem and Time Machine(主席树+树状数组)
- Codeforces Round #410 (Div. 2) E. Mike and code of a permutation(拓扑序+线段树)
- Codeforces 548E Mike and Foam(容斥)
- Codeforces #361 E. Mike and Geometry Problem 数学
- Codeforces Round #361 (Div. 2)E. Mike and Geometry Problem
- CodeForces 610 A. Pasha and Stick(水~)
- 机器学习简史brief history of machine learning
- opencv学习笔记(二十四)——相机标定板制作
- 王亟亟的Python学习之路(10)-函数对象的作用域,函数作为返回值,闭包
- MacTalk 跨越边界
- CodeForces 547E Mike and Friends [后缀树组+主席树]
- COGS896圈奶牛
- Win7+VS2013+OpenCV3.0.0详细配置
- LeetCode题解:Odd Even Linked List
- Ubuntu里面的安装命令总结
- 编写demo,验证swith语句的表达式可以是enum类型
- 二叉线索树
- koala 编译scss不支持中文(包括中文注释),解决方案如下
- 枚举的详细举例解释