• 【POJ3630】Phone List(字典树)


    problem

    • 给定n个长度不超过10的数字串(n<10^4)
    • 问其中是否存在两个数组串a,b,满足a是b的前缀。存在输出NO,不存在输出YES

    solution

    • 将所有数字串构建成字典树
    • 在插入过程中,如果没有新建任何节点(当前串是之前串的前缀))或者插入过程中经过某个带结尾标记的串(之前串是当前串的前缀),则存在前缀情况。

    codes

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn = 1e5+10;
    
    int tire[maxn][26], val[maxn], tot;
    void build(){
        tot = 1;
        memset(tire,0,sizeof(tire));
        memset(val,0,sizeof(val));
    }
    bool insert(char *s){
        bool jingguo = false, xinjian = false;
        int len = strlen(s), u = 1;
        for(int i = 0; i < len; i++){
            int c = s[i]-'0';
            if(tire[u][c]==0){
                tire[u][c] = ++tot;
                xinjian = true;
            }
            u = tire[u][c];
            if(val[u])jingguo = true;
        }
        val[u] = 1;
        if(!xinjian || jingguo)return true;
        return false;
    }
    
    char s[20];
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            int n;
            scanf("%d",&n);
            build();
            bool ans = false;
            for(int i = 1; i <= n; i++){
                scanf("%s",s);
                if(insert(s))ans = true;//存在前缀
            }
            if(ans)cout<<"NO
    ";
            else cout<<"YES
    ";
        }
        return 0;
    }
  • 相关阅读:
    HRBUST--2317 Game(完全背包)
    k8s的回滚应用
    python练习-2
    k8s HA 补充-(keepalived+haproxy配置)
    Etcd故障恢复记录
    kubernetes 1.14安装部署helm插件
    k8s Prometheus+CAdvisor+node_export+grafana
    k8s ingress部署
    k8s pvc
    k8s pv
  • 原文地址:https://www.cnblogs.com/gwj1314/p/10200115.html
Copyright © 2020-2023  润新知