当前位置:网站首页>[jsoi2015] string tree
[jsoi2015] string tree
2022-06-26 13:58:00 【__ LazyCat__】
Be persistent trie
link :[P6088 JSOI2015] String tree - Luogu | New ecology of computer science education (luogu.com.cn)
The question : Given a tree , Edge weight is string , Ask the path many times u To v How many prefixes do all strings of all edges that pass have w.
Answer key : Persistent dictionary tree maintenance , Every time u , v u,v u,v Of l c a lca lca, Then you can add the query in two sections .
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
typedef pair<int,string> P;
const int maxn=1e5+5;
string w;
int n,m,u,v;
int dep[maxn],lg[maxn],fa[maxn][18];
int p[maxn*10][26],rt[maxn],ct[maxn*10],now;
vector<P>ed[maxn];
void insert(int u,int v,string s)
{
rt[v]=++now;
int l=rt[u],r=rt[v]; ct[r]=ct[l]+1;
for(int i=0;i<s.length();i++)
{
int q=s[i]-'a';
for(int j=0;j<=25;j++)p[r][j]=p[l][j];
p[r][q]=++now,l=p[l][q],r=p[r][q],ct[r]=ct[l]+1;
}
return;
}
int query(int u,int v,string s)
{
int l=rt[u],r=rt[v];
for(int i=0;i<s.length();i++)
{
l=p[l][s[i]-'a'],r=p[r][s[i]-'a'];
}
return ct[r]-ct[l];
}
int LCA(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
while(dep[x]>dep[y])
{
x=fa[x][lg[dep[x]-dep[y]]-1];
}
if(x==y)return x;
for(int i=17;i>=0;i--)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i],y=fa[y][i];
}
}
return fa[x][0];
}
void dfs(int x,int f,string s)
{
dep[x]=dep[f]+1,fa[x][0]=f;
if(s.length())insert(f,x,s);
for(int i=1;i<=17;i++)
{
fa[x][i]=fa[fa[x][i-1]][i-1];
}
for(auto y:ed[x])
{
if(y.first!=f)dfs(y.first,x,y.second);
}
return;
}
int Query(int x,int y,string s)
{
int lca=LCA(x,y);
return query(lca,x,s)+query(lca,y,s);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)lg[i]=lg[i-1]+(!(i&i-1));
for(int i=1;i<n;i++)
{
cin>>u>>v>>w;
ed[u].push_back(P(v,w));
ed[v].push_back(P(u,w));
}
dfs(1,0,""),cin>>m;
for(int i=1;i<=m;i++)
{
cin>>u>>v>>w;
cout<<Query(u,v,w)<<"\n";
}
return 0;
}
边栏推荐
猜你喜欢

ICML 2022 | limo: a new method for rapid generation of targeted molecules

MongoDB系列之Window环境部署配置

Es sauvegarde et restauration des données par instantané

7.consul service registration and discovery

Ring queue PHP

Generation and rendering of VTK cylinder

Pointer

Zero basics of C language lesson 7: break & continue

AGCO AI frontier promotion (6.26)

8.Ribbon负载均衡服务调用
随机推荐
Gee - Global Human Settlements grid data 1975-1990-2000-2014
虫子 STL string 下 练习题
Basic type of typescript
Go language - pipeline channel
Wechat applet magic bug - choose to replace the token instead of clearing the token, wx Getstoragesync will take the old token value instead of the new token value
Jenkins build prompt error: eacces: permission denied
es常用语法一
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
团队管理的最关键因素
Solutions to the failure of last child and first child styles of wechat applet
GEE——全球人类居住区网格数据 1975-1990-2000-2014
Formal parameters vs actual parameters
shell脚本详细介绍(四)
A collection of common tools for making we media videos
Input text to automatically generate images. It's fun!
7-2 picking peanuts
Cloudcompare - Poisson reconstruction
HW蓝队溯源流程详细整理
Zero basics of C language lesson 7: break & continue
Firewall introduction