字典树及其应用

字典树及其应用练习题

前导知识

字典树(Trie)

1. 于是他开始错误的点名了

传送门

#include <cstdio>
const int MAX_N = 500010;
char s[60];
int n,m,next[MAX_N][26],exist[MAX_N],cnt;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%s", s);
        int p=0;
        for(int i=0;s[i];i++){
            int c=s[i]-'a';
            if(!next[p][c]) next[p][c]=++cnt;
            p=next[p][c];
        }
        exist[p]=1;
    }
    scanf("%d",&m);
    while(m--){
        scanf("%s",s);
        int p=0;
        for(int i=0;s[i];i++){
            int c=s[i]-'a';
            p=next[p][c];
            if(!p) break;
        }
        if(exist[p]==1){
            exist[p]=2;
            printf("OK\n");
        }else if(exist[p]==2){
            printf("REPEAT\n");
        }
        else printf("WRONG\n");
    }
    return 0;
}

2. 最长异或路径

传送门

#include <algorithm>
#include <cstdio>
const int MAX_N = 100010;
int head[MAX_N],cnt;
int n,dis[MAX_N],ch[MAX_N<<5][2],tot,ans;
struct node{
    int next,v,w;
}e[MAX_N<<1];
void add_edge(int u,int v,int w){
    e[++cnt].v=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void insert(int x){
    int u=0;
    for(int i=30;i>=0;i--){
        int c=((x>>i)&1);
        if(!ch[u][c]) ch[u][c]=++tot;
        u=ch[u][c];
    }
}
void get(int x){
    int res=0,u=0;
    for(int i=30;i>=0;i--){
        int c=((x>>i)&1);
        if(ch[u][c^1]){
            u=ch[u][c^1];
            res|=(1<<i);
        }else
            u=ch[u][c];
    }
    ans=std::max(ans,res);
}
void dfs(int u,int fa){
    insert(dis[u]);
    get(dis[u]);
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].v;
        if(v==fa) continue;
        dis[v]=dis[u]^e[i].w;
        dfs(v,u);
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        add_edge(u,v,w);
        add_edge(v,u,w);
    }
    dfs(1,0);
    printf("%d",ans);
    return 0;
}

3. UVA11362 Phone List

传送门

#include <iostream>
#include <string.h>
using namespace std;
const int N = 100010;
int t;
long long n;
char s[N];
long long nex[N][15];
bool exist[N];
long long cnt,ans;
inline void insert(){
    int p=0;
    long long l=strlen(s);
    bool flag=false;
    for(int i=0;i<l;i++){
        long long c=(s[i]^48);
        if(!nex[p][c]) nex[p][c]=++cnt;
        else if(i==l-1)
            flag=true;
        ans+=exist[p];
        p=nex[p][c];
    }
    exist[p]=1;
    ans+=flag;
    return;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        memset(nex,0,sizeof nex);
        memset(exist,0,sizeof exist);
        cnt=0;
        ans=0;
        /* 更新 */
        cin>>n;
        while(n--){
            cin>>s;
            insert();
        }
        printf("%s\n",ans?"NO":"YES");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DeeGLMath

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值