zoukankan      html  css  js  c++  java
  • hihoCoder 1014trie树(字典树)

    hihoCoder 1014

    题目提示已经很清楚了~

    贴代码……

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    const int MAXN = 100000 + 10;
    const int alNum = 26;
    struct Node{
        int cnt;
        int next[alNum];
        void init(){
            memset(next,-1,sizeof(next));
            cnt = 0;
            return;
        }
    };
    Node trie[MAXN];
    int tt;
    void build_trie(char str[]){
        int len = strlen(str);
        int p = 0;
        for(int i = 0;i < len;i++){
            int ch = str[i] - 'a';
            if(trie[p].next[ch] == -1){
                trie[tt].init();
                trie[p].next[ch] = tt++;
            }
            p = trie[p].next[ch];
            trie[p].cnt++;
        }
    }
    int quercy(char str[]){
        int len = strlen(str);
        int p = 0;
        for(int i = 0;i < len;i++){
            int ch = str[i] - 'a';
            if(trie[p].next[ch] == -1){
                return 0;
            }
            p = trie[p].next[ch];
        }
        return trie[p].cnt;
    }
    int main(){
    //    freopen("input.txt","r",stdin);
        int n,m;
        while(~scanf("%d",&n)){
            char str[20];
            tt = 0;
            trie[tt++].init();
            for(int i = 0;i < n;i++){
                scanf("%s",str);
                build_trie(str);
            }
            scanf("%d",&m);
            for(int i = 0;i < m;i++){
                scanf("%s",str);
                int q = quercy(str);
                printf("%d
    ",q);
            }
        }
        return 0;
    }

    hdu1671  Phone List

    字典树 水题

    判断一个是否为另一个的前缀。

    注意 9113

      911 的情况……

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    const int MAXN = 10000000 + 10;
    const int NextNum = 10;
    
    int tt;
    struct Node{
        int next[NextNum];
        bool flag;
        void init(){
            memset(next,-1,sizeof(next));
            flag = 0;
        }
    };
    Node trie[MAXN];
    bool build_trie(char str[]){
        int p = 0;
        int len = strlen(str);
        for(int i = 0;i < len;i++){
            int x = str[i] - '0';
            if(trie[p].next[x] == -1){
                trie[tt].init();
                trie[p].next[x] = tt++;
            }
            p = trie[p].next[x];
            if(trie[p].flag){
                return 0;
            }
        }
        for(int i = 0;i < NextNum;i++){
            if(trie[p].next[i] != -1){
                return 0;
            }
        }
        trie[p].flag = 1;
        return 1;
    }
    
    int main(){
    //    freopen("input.txt","r",stdin);
        int t;
        scanf("%d",&t);
        while(t--){
            int n;
            scanf("%d",&n);
            tt = 0;
            trie[tt++].init();
            bool ok = 1;
            char str[40+10];
            for(int i = 0;i < n;i++){
                scanf("%s",str);
                if(!ok){
                    continue;
                }
                ok = build_trie(str);
            }
            printf("%s
    ",ok?"YES":"NO");
        }
        return 0;
    }
  • 相关阅读:
    51nod 1494 选举拉票 | 线段树
    51nod 1295 XOR key | 可持久化Trie树
    Codeforces 438D (今日gg模拟第二题) | 线段树 考察时间复杂度的计算 -_-|||
    51nod 1563 坐标轴上的最大团(今日gg模拟第一题) | 线段覆盖 贪心 思维题
    良心的可持久化线段树教程
    51nod 1593 公园晨跑 | ST表(线段树?)思维题
    51nod 1595 回文度 | 马拉车Manacher DP
    51nod 1522 上下序列
    胡小兔的OI日志3 完结版
    51nod 1510 最小化序列 | DP 贪心
  • 原文地址:https://www.cnblogs.com/hanbinggan/p/4681313.html
Copyright © 2011-2022 走看看