bzoj 4598: [Sdoi2016]模式字符串 点分治+hash
2018-01-19 21:12
417 查看
题意
给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m的模式串s,其中每一位仍然是A到z的大写字母。Alice希望知道,有多少对结点<u,v>满足T上从u到V的最短路径形成的字符串可以由模式串S重复若干次得到?这里结点对<u,v>是有序的,也就是说<u,v>和<v,u>需要被区分.所谓模式串的重复,是将若干个模式串S依次相接(不能重叠).例如当S=PLUS的时候,重复两次会得到PLUSPLUS,重复三次会得到PLUSPLUSPLUS,同时要注恿,重复必须是整数次的。例如当S=XYXY时,因为必须重复整数次,所以XYXYXY不能看作是S重复若干次得到的。T<=10,n,M<=1000000
分析
刚看完题就往sam上想,想了半天发现搞不懂。瞄了一眼题解发现是点分治+hash,然后就会做了。脑残细节题,调的我妈都不认得了。还好过了样例就一次A了,不然真的不知道要怎么调。。。
直接点分治,维护前缀后缀哈希,然后每次记录每种长度的前缀后缀数量,最后统计答案即可。
代码
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<vector> using namespace std; typedef long long LL; typedef unsigned long long ull; const int N=1000005; int n,len,cnt,last ,a ,ch ,size ,mx_size ,pre ,suf ,tot_pre,tot_suf,stack ; int cnt_pre ,cnt_suf ,top,L1 ,L2 ,sum,root; ull hash_pre ,hash_suf ,po ,hash ; LL ans; struct edge{int to,next;}e[N*2]; bool vis ; vector<int> vec; char str ; int read() { int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } void addedge(int u,int v) { e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt; e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt; } void get_root(int x,int fa) { size[x]=1;mx_size[x]=0; for (int i=last[x];i;i=e[i].next) { if (e[i].to==fa||vis[e[i].to]) continue; get_root(e[i].to,x); size[x]+=size[e[i].to]; mx_size[x]=max(mx_size[x],size[e[i].to]); } mx_size[x]=max(mx_size[x],sum-size[x]); if (!root||mx_size[x]<mx_size[root]) root=x; } bool check(int op,int top,int L) { ull w=hash[stack[top]]-hash[stack[top-L]]*po[L]; if (!op) return w==hash_pre[L]; else return w==hash_suf[len-L+1]; } void get(int x,int fa) { hash[x]=hash[fa]*27+a[x]; stack[++top]=x; if (top<len) L1[x]=check(0,top,top)?top:-1; else L1[x]=check(0,top,len)?L1[stack[top-len]]:-1; if (top<len) L2[x]=check(1,top,top)?top:-1; else L2[x]=check(1,top,len)?L2[stack[top-len]]:-1; if (L1[x]>-1) pre[++tot_pre]=L1[x]; if (L2[x]>-1) suf[++tot_suf]=L2[x]; size[x]=1; for (int i=last[x];i;i=e[i].next) if (e[i].to!=fa&&!vis[e[i].to]) get(e[i].to,x),size[x]+=size[e[i].to]; top--; } void solve(int x) { vis[x]=1;L1[x]=L2[x]=-1; if (a[x]==ch[1]) cnt_pre[1]++,vec.push_back(1),L1[x]=1; if (a[x]==ch[len]) cnt_suf[1]++,vec.push_back(1),L2[x]=1; for (int i=last[x];i;i=e[i].next) { if (vis[e[i].to]) continue; tot_pre=tot_suf=0;stack[top=1]=x; hash[x]=a[x]; get(e[i].to,x); for (int j=1;j<=tot_pre;j++) ans+=cnt_suf[len-pre[j]+1]; for (int j=1;j<=tot_suf;j++) ans+=cnt_pre[len-suf[j]+1]; for (int j=1;j<=tot_pre;j++) cnt_pre[pre[j]]++,vec.push_back(pre[j]); for (int j=1;j<=tot_suf;j++) cnt_suf[suf[j]]++,vec.push_back(suf[j]); } for (int i=0;i<vec.size();i++) cnt_pre[vec[i]]=cnt_suf[vec[i]]=0; vec.clear(); for (int i=last[x];i;i=e[i].next) { if (vis[e[i].to]) continue; sum=size[e[i].to];root=0; get_root(e[i].to,x); solve(root); } } void clear() { cnt=ans=0; memset(last,0,sizeof(last)); memset(vis,0,sizeof(vis)); } int main() { po[0]=1; for (int i=1;i<=1000000;i++) po[i]=po[i-1]*27; int T=read(); while (T--) { n=read();len=read(); clear(); scanf("%s",str+1); for (int i=1;i<=n;i++) a[i]=str[i]-'A'+1; for (int i=1;i<n;i++) { int x=read(),y=read(); addedge(x,y); } scanf("%s",str+1); for (int i=1;i<=len;i++) ch[i]=str[i]-'A'+1; hash_pre[0]=hash_suf[len+1]=0; for (int i=1;i<=len;i++) hash_pre[i]=hash_pre[i-1]+(ull)ch[i]*po[i-1]; for (int i=len;i>=1;i--) hash_suf[i]=hash_suf[i+1]+(ull)ch[i]*po[len-i]; root=0;sum=n;L1[0]=L2[0]=len; get_root(1,0); solve(root); printf("%lld\n",ans); } return 0; }
相关文章推荐
- bzoj 4598: [Sdoi2016]模式字符串 (hash+点分治)
- [BZOJ4598][Sdoi2016]模式字符串(点分治+Hash)
- bzoj4598 [Sdoi2016]模式字符串(hash+点分治)
- 【BZOJ4598】[Sdoi2016]模式字符串 树分治+hash
- [SDOI2016] 模式字符串 (BZOJ4598 & VIJOS1995)
- bzoj4598 [Sdoi2016]模式字符串 hash+点分
- BZOJ 4598: [Sdoi2016]模式字符串
- BZOJ4598: [Sdoi2016]模式字符串
- bzoj 4598: [Sdoi2016]模式字符串
- 【BZOJ 4598】【SDOI 2016 Round2 Day1 T3】模式字符串
- BZOJ4598 [Sdoi2016]模式字符串 【点分治 + hash】
- 4598: [Sdoi2016]模式字符串
- [BZOJ2795] [Poi2012] [字符串hash] A Horrible Poem
- 【字符串哈希】bzoj3098 Hash Killer II
- 【LOJ】#2065. 「SDOI2016」模式字符串
- bzoj 3916 friends | 字符串hash
- [CQOI2014][bzoj3507] 通配符匹配 [字符串hash+dp]
- bzoj4598: [Sdoi2016]模式字符串
- [BZOJ1014] [JSOI2008]火星人prefix && splay+字符串hash 重写版
- BZOJ 4435 [Cerc2015]Juice Junctions 分治最小割+hash