4477: [Jsoi2015]字符串树

本文介绍了一种利用差分、可持久化Trie树解决树上字符串查询问题的方法。通过将问题转化为点到根路径上的字符串操作,实现高效查询特定前缀出现次数的功能。

题目链接

题目大意:给出一棵树,每条边上都有一个长度不超过10的字符串。给出m个询问x y ch,求x到y的路径有多少个字符串的前缀是ch

题解:首先差分一下,把(x,y)变成 (1,x)+(1,y)2(1,lca)
用可持久化Trie树维护每个点到根的所有串,这样树上节点就不会超过所有串总长

记一个size表示在其子树中有多少个串

查询的时候把询问串跑一遍输出size即可

我的收获:套路++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int N=100005;
const int L=20;

int n,m;
int t,head[N];
int root[N];
int dep[N],fa[N][L+2];
char s[N][13],T[N];

struct edge{int to,nex,id;}e[N<<1];

void add(int u,int v,int i){e[t].to=v,e[t].nex=head[u],e[t].id=i,head[u]=t++;}

struct Trie{
    #define idx(i) i-'a'

    int cnt,sum[N*10],c[N*10][26];

    void insert(int x,int &y,int id){
        int len=strlen(s[id]),w=y=++cnt;
        for(int i=0;i<len;i++){
            memcpy(c[w],c[x],sizeof(c[x]));
            x=c[x][idx(s[id][i])];
            w=c[w][idx(s[id][i])]=++cnt;
            sum[w]=sum[x]+1;
        }
    }

    int query(int x){
        int len=strlen(T);
        for(int i=0;i<len;i++) x=c[x][idx(T[i])];
        return sum[x];
    }

}Tree;

void dfs(int x)
{
    for(int i=1;i<=L;i++) fa[x][i]=fa[fa[x][i-1]][i-1];
    for(int i=head[x];i!=-1;i=e[i].nex){
        int v=e[i].to;
        if(v==fa[x][0]) continue;
        fa[v][0]=x,dep[v]=dep[x]+1;
        root[v]=root[x];Tree.insert(root[x],root[v],e[i].id);
        dfs(v);
    }
}

int lca(int x,int y)
{
    if(dep[x]<dep[y]) swap(x,y);
    for(int i=0;i<=L;i++) if((dep[x]-dep[y])&(1<<i)) x=fa[x][i];
    if(x==y) return x;
    for(int i=L;i>=0;i--) if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}

void work()
{
    scanf("%d",&m);
    for(int u,v,i=1;i<=m;i++){
        scanf("%d%d%s",&u,&v,T);
        int pr=lca(u,v);
        printf("%d\n",Tree.query(root[u])+Tree.query(root[v])-2*Tree.query(root[pr]));
    }
}

void init()
{
    scanf("%d",&n);memset(head,-1,sizeof(head));
    for(int u,v,i=1;i<n;i++) scanf("%d%d%s",&u,&v,s[i]),add(u,v,i),add(v,u,i);
    dfs(1);
}

int main()
{
    init();
    work();
    return 0;
}
帮我调试一下下面代码: #include<bits/stdc++.h> using namespace std; /* link: http://goj.wiki/d/junior/p/P1700 https://www.luogu.com.cn/problem/P6088 -------------------------------------- 可持久化trie 建图 -> 将每条边的字符串丢到trie中,并且利用可持久化建出不同路径的版本。 然后计算答案的时候用到lca去减掉算多的 */ const int MAXN = 1e5 + 5; int N, Q; struct star{ int nxt, to; string str; }edge[MAXN * 2]; int head[MAXN], ccnt; void add( int u, int v, string & s ){ edge[++ ccnt] = { head[u], v, s }; head[u] = ccnt; } //---------------------------------------------- const int MAXM = 1300000; int tr[MAXM][30];//点p的字母c子节点 int cnt[MAXM], tot;//cnt[p]表示经过p的字符串数量 void insert( int pre, int now, string & s ){ int pree = pre, noww = now; for( int i = 0; i < s.size(); i ++ ){ int k = s[i] - 'a'; for( int j = 0; j < 26; j ++ ){ if( j != k ) tr[noww][j] = ( pree ? tr[pree][j] : 0 ); } tr[noww][k] = ++ tot; noww = tr[noww][k]; pree = ( pree ? tr[pree][k] : 0 ); cnt[noww] = ( pree ? cnt[pree] : 0 ) + 1; } } int query( int root, string s ){//以 s 为前缀的字符串数量 int now = root; for( int i = 0; i < s.size(); i ++ ){ int k = s[i] - 'a'; if( !tr[now][k] ) return 0;//不存在当前前缀 now = tr[now][k]; } return cnt[now]; } //---------------------------------------------------------------- int dep[MAXN], f[21][MAXN], root[MAXN]; //(from Qwen-Max) root[u]:表示从树的根节点(1)到节点 u 的路径上,所有边字符串插入后形成的 Trie 的根节点编号。 void dfs( int u, int fa ){ dep[u] = dep[fa] + 1; f[0][u] = fa; for( int i = 1; i <= 20; i ++ ){ f[i][u] = f[i - 1][f[i - 1][u]];//预处理倍增 } for( int i = head[u]; i; i = edge[i].nxt ){ int to = edge[i].to; if( to == fa ) continue; root[to] = ++ tot; insert( root[u], root[to], edge[i].str ); dfs( to, u ); } // cout << "ok" << endl; } int lca( int u, int v ){ if( dep[u] < dep[v] ) swap( u, v ); for( int i = 20; i >= 0; i -- ){ if( dep[f[u][i]] >= dep[v] ) u = f[u][i]; } if( u == v ) return u; for( int i = 20; i >= 0; i -- ){ if( f[u][i] != f[v][i] ){ u = f[u][i]; v = f[v][i]; } } return f[0][u]; // cout << "ok" << endl; } int main(){ // freopen( "trie.in", "r", stdin ); // freopen( "trie.out", "w", stdout ); cin >> N; for( int i = 1; i < N; i ++ ){ int u, v; string str; cin >> u >> v >> str; add( u, v, str ); add( v, u, str ); } dep[0] = -1; dfs( 1, 0 );//预处理 + 建trie cin >> Q; while( Q -- ){ int u, v; string qb; cin >> u >> v >> qb; int p = lca( u, v ); cout << query( root[u], qb ) + query( root[v], qb ) - 2 * query( root[p], qb ) << endl; } return 0; } 题面: # P6088 [JSOI2015] 字符串树 ## 题目背景 萌萌买了一颗字符串树的种子,春天种下去以后夏天就能长出一棵很大的字符串树字符串树很奇特,树枝上都密密麻麻写满了字符串,看上去很复杂的样 子。 ## 题目描述 字符串树本质上还是一棵树,即 $N$ 个节点 $N-1$ 条边的连通无向无环图,节点从 $1$ 到 $N$ 编号。与普通的树不同的是,树上的每条边都对应了一个字符串。萌萌和 JYY 在树下玩的时候,萌萌决定考一考 JYY。每次萌萌都写出一个字符串 $S$ 和两个节点 $U,V$,JYY 需要立即回答 $U$ 和 $V$ 之间的最短路径(即 $U,V$ 之间边数最少的路径,由于给定的是一棵树,这样的路径是唯一的)上有多少个字符串以 $S$ 为前缀。 JYY 虽然精通编程,但对字符串处理却不在行。所以他请你帮他解决萌萌的难题。 ## 输入格式 输入第一行包含一个整数 $N$,代表字符串树的节点数量。 接下来 $N-1$ 行,每行先是两个数 $U,V$,然后是一个字符串 $S$,表示节点 $U$ 和节点 $V$ 之间有一条直接相连的边,这条边上的字符串是 $S$。输入数据保证给出的是一棵合法的树。 接下来一行包含一个整数 $Q$,表示萌萌的问题数。 接下来 $Q$ 行,每行先是两个数 $U,V$,然后是一个字符串 $S$,表示萌萌的一个问题是节点 $U$ 和节点 $V$ 之间的最短路径上有多少字符串以 $S$ 为前缀。 ## 输出格式 输出 $Q$ 行,每行对应萌萌的一个问题的答案。 ## 输入输出样例 #1 ### 输入 #1 ``` 4 1 2 ab 2 4 ac 1 3 bc 3 1 4 a 3 4 b 3 2 ab ``` ### 输出 #1 ``` 2 1 1 ``` ## 说明/提示 对于 $100\%$ 的数据,$1\leq N,Q\leq 10^5$,输入所有字符串长度不超过 $10$ 且只包含 `a~z` 的小写字母。
最新发布
10-14
``` #include<bits/stdc++.h> #define ll long long using namespace std; struct Node{ int v,p,sz; unsigned int he; Node *cl,*cr; Node(int x):v(x),p(rand()),sz(1),he(x),cl(nullptr),cr(nullptr){} ~Node(){ delete cl; delete cr; } friend int siz(Node *x){ if(x==nullptr)return 0; return x->sz; } void push_up(){ sz=1; he=v*(1u<<siz(cl)); if(cl!=nullptr){ sz+=cl->sz; he+=cl->he; } if(cr!=nullptr){ sz+=cr->sz; he+=(1u<<(siz(cl)+1))*cr->he; } } friend Node* merge(Node *x,Node *y){ if(x==nullptr)return y; if(y==nullptr)return x; if(x->p<y->p){ x->cr=merge(x->cr,y); x->push_up(); return x; }else{ y->cl=merge(x,y->cl); y->push_up(); return y; } } friend Node* split(Node *&x,int r){ if(x==nullptr)return nullptr; if(siz(x->cl)>=r){ Node *t=split(x->cl,r); swap(t,x->cl); x->push_up(); swap(t,x); return t; }else{ Node *t=split(x->cr,r-siz(x->cl)-1); x->push_up(); return t; } } friend void change(Node *&h,int x,Node w){ Node *wr=split(h,x),*dq=split(h,x-1); delete dq; h=merge(h,merge(new Node(w),wr)); } friend void add(Node *&h,int x,Node w){ Node *wr=split(h,x); h=merge(h,merge(new Node(w),wr)); } }; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin>>s; Node *tr1=nullptr,*tr2=nullptr; for(int i=0;i<s.size();++i){ tr1=merge(tr1,new Node(s[i])); tr2=merge(tr2,new Node(s[i])); } int T; cin>>T; while(T--){ char op; cin>>op; if(op=='Q'){ int x,y; cin>>x>>y; Node *r1=split(tr1,x),*r2=split(tr2,y); int ans=0; for(int i=20;i>=0;--i){ if(ans+(1<<i)>min(r1->sz,r2->sz))continue; Node *rr1=split(r1,ans+(1<<i)),*rr2=split(r2,ans+(1<<i)); if(r1->he==r2->he)ans+=1<<i; merge(r1,rr1); merge(r2,rr2); } cout<<ans; tr1=merge(tr1,r1); tr2=merge(tr2,r2); }else if(op=='R'){ int x; char c; cin>>x>>c; change(tr1,x,Node(c)); change(tr2,x,Node(c)); }else{ int x; char c; cin>>x>>c; add(tr1,x,Node(c)); add(tr2,x,Node(c)); } } delete tr1; delete tr2; return 0; }```# P4036 [JSOI2008] 火星人 ## 题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号: ``` 序号 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m ``` 现在,火星人定义了一个函数 $LCQ(x, y)$,表示:该字符串中第 $x$ 个字符开始的字串,与该字符串中第 $y$ 个字符开始的字串,两个字串的公共前缀的长度。比方说,$LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0$ 在研究 $LCQ$ 函数的过程中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出 $LCQ$ 函数的值;同样,如果求出了 $LCQ$ 函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取 $LCQ$ 函数的快速算法,但不甘心认输的地球人又给火星人出了个难题:在求取 $LCQ$ 函数的同时,还可以改变字符串本身。具体地说,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此复杂的问题中,火星人是否还能够做到很快地求取 $LCQ$ 函数的值。 ## 输入格式 第一行给出初始的字符串。第二行是一个非负整数 $M$ ,表示操作的个数。接下来的M行,每行描述一个操作。操作有 $3$ 种,如下所示 1. 询问。语法:$Q$ $x$ $y$ ,$x$ ,$y$ 均为正整数。功能:计算 $LCQ(x,y)$ 限制:$1$ $\leq$ $x$ , $y$ $\leq$ 当前字符串长度 。 2. 修改。语法:$R$ $x$ $d$,$x$ 是正整数,$d$ 是字符。功能:将字符串中第 $x$ 个数修改为字符 $d$ 。限制:$x$ 不超过当前字符串长度。 3. 插入:语法:$I$ $x$ $d$ ,$x$ 是非负整数,$d$ 是字符。功能:在字符串第 $x$ 个字符之后插入字符 $d$ ,如果 $x=0$,则在字符串开头插入。限制:$x$ 不超过当前字符串长度 ## 输出格式 对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。 ## 输入输出样例 #1 ### 输入 #1 ``` madamimadam 7 Q 1 7 Q 4 8 Q 10 11 R 3 a Q 1 7 I 10 a Q 2 11 ``` ### 输出 #1 ``` 5 1 0 2 1 ``` ## 说明/提示 1. 所有字符串自始至终都只有小写字母构成。 2. $M\leq150,000$ 3. 字符串长度L自始至终都满足$L\leq100,000$ 4. 询问操作的个数不超过 $10,000$ 个。 对于第 $1$,$2$ 个数据,字符串长度自始至终都不超过 $1,000$ 对于第 $3$,$4$,$5$ 个数据,没有插入操作。 2024/07/40 更新一组 hack。 debug RE
04-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值