1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #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=head[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 double eps=1e-8; const int inf=1e9+9; #define ll long long using namespace std; struct edge{int t;edge*next;}e[MAXN<<1],*head[MAXN],*o=e; void add(int x,int y){o->t=y;o->next=head[x];head[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; } int n; char str[MAXN]; int fa[MAXN],dep[MAXN],son[MAXN],num[MAXN]; void dfs(int x,int pre,int deep){ fa[x]=pre;dep[x]=deep+1;num[x]=1; link(x){ if(j->t==pre)continue; dfs(j->t,x,deep+1); num[x]+=num[j->t]; if(son[x]==-1||num[son[x]]<num[j->t])son[x]=j->t; } } int p[MAXN],fp[MAXN],tp[MAXN],cnt; void _dfs(int x,int td){ p[x]=++cnt;fp[p[x]]=x;tp[x]=td; if(son[x]!=-1)_dfs(son[x],td); link(x)if(j->t!=fa[x]&&j->t!=son[x])_dfs(j->t,j->t); } char s[MAXN],s1[MAXN]; int len,tot; int End[MAXN],Begin[MAXN]; void work(){ len=0; inc(i,1,n){ if(tp[i]!=i)continue; int t=i;tot=0;Begin[i]=len; while(t!=-1){ s1[tot++]=str[t]; t=son[t]; } inc(i,0,tot-1)s[len++]=s1[i]; s[len++]='$'; dec(i,tot-1,0)s[len++]=s1[i]; End[i]=len-1; s[len++]='$'; } s[len]='\0'; } int sa[MAXN],txt[MAXN],t1[MAXN],t2[MAXN],rank1[MAXN],rank2[MAXN],Td[MAXN]; bool cmp(int f[],int t,int w,int k){return f[t]==f[w]&&f[t+k]==f[w+k];} void Sa(){ int m=256; int *rank1=t1;int *Td=t2; for(int i=0;i<m;i++)txt[i]=0; for(int i=0;i<len;i++)rank1[i]=s[i],txt[s[i]]++; for(int i=1;i<=m;i++)txt[i]+=txt[i-1]; for(int i=len-1;i>=0;i--)sa[--txt[s[i]]]=i; for(int k=1;k<=len;k<<=1){ int num=0; for(int i=len-k;i<len;i++)Td[num++]=i; for(int i=0;i<len;i++)if(sa[i]>=k)Td[num++]=sa[i]-k; for(int i=0;i<m;i++)txt[i]=0; for(int i=0;i<len;i++)txt[rank1[i]]++; for(int i=1;i<m;i++)txt[i]+=txt[i-1]; for(int i=len-1;i>=0;i--)sa[--txt[rank1[Td[i]]]]=Td[i]; swap(rank1,Td);rank1[sa[0]]=0; num=1; for(int i=1;i<len;i++)rank1[sa[i]]=cmp(Td,sa[i],sa[i-1],k)?num-1:num++; if(num>=len)break; m=num; } for(int i=0;i<len;i++)rank2[sa[i]]=i; } int h[MAXN],H[MAXN]; void hh(){ for(int i=0;i<len;i++){ if(rank2[i]==0)continue; int t=sa[rank2[i]-1];int w=i;int k=0; if(i==0||H[i-1]<=1)k=0;else k=H[i-1]-1,t+=k,w+=k; while(t<len&&w<len){ if(s[t]==s[w])k++; else break; t++;w++; } H[i]=k;h[rank2[i]]=k; } } int dp[MAXN][21],ma[MAXN]; void St(){ for(int i=2;i<=len;i++)ma[i]=ma[i/2]+1; for(int i=1;i<len;i++)dp[i][0]=h[i]; for(int j=1;j<=20;j++){ for(int i=1;i+(1<<j)<=len;i++){ dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } } } int rmq(int l,int r){ if(l==r)return inf; if(l>r)swap(l,r); l++; int k=ma[r-l+1]; return min(dp[l][k],dp[r-(1<<k)+1][k]); } vector<pii>v1,v2,p1,p2; void solve(int u,int v){ v1.clear();v2.clear(); int uu=tp[u];int vv=tp[v]; while(uu!=vv){ if(dep[uu]>dep[vv]){ v1.pb(mp(End[uu]-dep[u]+dep[uu],End[uu])); u=fa[uu];uu=tp[u]; } else{ v2.pb(mp(Begin[vv],Begin[vv]+dep[v]-dep[vv])); v=fa[vv];vv=tp[v]; } } if(dep[u]>dep[v]){v1.pb(mp(End[tp[u]]-dep[u]+dep[tp[u]],End[tp[v]]-dep[v]+dep[tp[v]]));} else v1.pb(mp(Begin[tp[u]]+dep[u]-dep[tp[u]],Begin[tp[v]]+dep[v]-dep[tp[v]])); reverse(v2.begin(),v2.end()); for(int i=0;i<v2.size();i++)v1.pb(v2[i]); } int main(){ n=read(); inc(i,1,n)son[i]=-1; inc(i,1,n)scanf(" %c",&str[i]); int u,v; inc(i,2,n)u=read(),v=read(),add(u,v),add(v,u); dfs(1,0,0);_dfs(1,1);work(); Sa();hh();St(); int q=read(); int x,y; while(q--){ p1.clear();p2.clear(); u=read();v=read();x=read();y=read(); solve(u,v); for(int i=0;i<v1.size();i++)p1.pb(v1[i]); solve(x,y); for(int i=0;i<v1.size();i++)p2.pb(v1[i]); int l=0,r=p1[0].first,lx=0,rx=p2[0].first; int t1=p1.size();int t2=p2.size();int ans=0; while(l<t1&&lx<t2){ int k=rmq(rank2[r],rank2[rx]); k=min(k,min(p1[l].second-r+1,p2[lx].second-rx+1)); if(!k)break; ans+=k; if(r+k-1>=p1[l].second){ l++; if(l==t1)break; r=p1[l].first; } else r+=k; if(rx+k-1>=p2[lx].second){ lx++; if(lx==t2)break; rx=p2[lx].first; } else rx+=k; } printf("%d\n",ans); } return 0; }
|