BZOJ4477: [Jsoi2015] string tree

Source: Internet
Author: User

"Portal: BZOJ4477" Brief test Instructions:

Given a tree of n points, the Bendu on the tree represents a string, gives the Q query, each query input x, Y, and the string s, and the number of strings prefixed with s on the path to the Y is obtained.

The following:

I yy a wave of persistent dictionary tree

Place a string of each edge into a deeper node to save

For X-to-y paths, Rt[x]+rt[y]-2*rt[lca] is OK, because we don't need the LCA to its father's string

Native RE, submit AC ... I'm afraid it's wrong.

Reference Code:
#include <cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<cstring>using namespacestd;structtrie{intson[ -],c; Trie () {c=0; memset (son,0,sizeof(son)); }}tr[4100000];intTrlen;intSt:110000];structnode{intX,y,next;} a[110000];intlen,last[110000];voidInsintXinty) {Len++; a[len].x=x;a[len].y=y; A[len].next=last[x];last[x]=Len;}Charst[ +];intSlen;voidLink (int&AMP;U1,intd) {    if(u1==0) u1=++Trlen; TR[U1].C=1; if(D==slen)return ; D++; inty=st[d]-'a'+1; Link (tr[u1].son[y],d);}voidMerge (int&AMP;U1,intU2) {    if(u1==0) {u1=u2;return ;} if(u2==0)return ; TR[U1].C+=tr[u2].c;  for(intI=1; i<= -; i++) Merge (Tr[u1].son[i],tr[u2].son[i]);}intf[110000][ +],bin[ +],dep[110000];voidDfsintx) {     for(intI=1; bin[i]<=dep[x];i++) f[x][i]=f[f[x][i-1]][i-1];  for(intk=last[x];k;k=A[k].next) {        inty=a[k].y; if(y!=f[x][0]) {f[y][0]=x; Dep[y]=dep[x]+1;            Merge (Rt[y],rt[x]);        DFS (y); }    }}intLCA (intXinty) {    if(dep[x]<Dep[y]) swap (x, y);  for(intI= -; i>=0; i--)    {        if(dep[x]-dep[y]>=Bin[i]) {x=F[x][i]; }    }    if(x==y)returnx;  for(intI= -; i>=0; i--)    {        if(dep[x]>=bin[i]&&f[x][i]!=F[y][i]) {x=f[x][i];y=F[y][i]; }    }    returnf[x][0];}Chars[ +];intClen;intFINDC (intU1,intU2,intU3,intd) {    intc=tr[u1].c+tr[u2].c-2*tr[u3].c; if(c==0)return 0; if(D==clen)returnC; D++; inty=s[d]-'a'+1; returnFINDC (tr[u1].son[y],tr[u2].son[y],tr[u3].son[y],d);}intMain () {bin[0]=1; for(intI=1; i<= -; i++) bin[i]=bin[i-1]<<1; intN; scanf ("%d",&N); Len=0; Memset (Last,0,sizeof(last)); Trlen=0; Memset (RT,0,sizeof(RT));  for(intI=1; i<n;i++)    {        intx, y; scanf ("%d%d%s", &x,&y,st+1); Slen=strlen (st+1);        Ins (x, y); Link (Rt[y],0); } f[1][0]=0;d ep[1]=0;d FS (1); intQ; scanf ("%d",&Q);  for(intI=1; i<=q;i++)    {        intx, y; scanf ("%d%d%s", &x,&y,s+1); Clen=strlen (s+1); intLca=LCA (x, y); printf ("%d\n", FINDC (Rt[x],rt[y],rt[lca],0)); }    return 0;}

BZOJ4477: [Jsoi2015] string tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.