zoukankan      html  css  js  c++  java
  • trie

    struct trie
    {
        int tree[maxn][30],sum[maxn],flag[maxn],tot;
        void insert_(char *str)
        {
            int len = strlen(str);
            int root = 0;
            for (int i = 0; i < len; i++)
            {
                int id = str[i] - 'a';
                if (!tree[root][id])
                {
                    tree[root][id] = ++tot;
                }
                sum[tree[root][id]]++;
                root = tree[root][id];
            }
            flag[root]=1;
        }
        bool search_(char *str)
        {
            int len = strlen(str);
            int root = 0;
            for (int i = 0; i < len; i++)
            {
                int id = str[i] - 'a';
                if (!tree[root][id])
                {
                    return 0;
                }
                root = tree[root][id];
            }
            if (flag[root])
            {
                return 1;
            }
            return 0;
        }
        void init()
        {
            for (int i=0; i<=tot; i++)
            {
                flag[i]=sum[i]=0;
                for (int j=0; j<30; j++)
                {
                    tree[i][j]=0;
                }
            }
            tot=0;
        }
    } T;

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=2e6+5;
    int sum[maxn],flag[maxn];
    
    char s[maxn];
    struct trie {
        int tree[maxn][30],tot;
    
        void insert_(char *str) {
            int len = strlen(str);
            int root = 0;
            for (int i = 0; i < len; i++) {
                int id = str[i] - 'a';
                if (!tree[root][id]) {
                    tree[root][id] = ++tot;
                }
                sum[tree[root][id]]++;
                root = tree[root][id];
            }
            flag[root]=1;
        }
        int search_(char *str) {
            int len = strlen(str);
            int root = 0;
            for (int i = 0; i < len; i++) {
                int id = str[i] - 'a';
                if (!tree[root][id]) {
                    return 0;
                }
                root = tree[root][id];
            }
            return sum[root];
        }
    }T;
    
    int main(){
        while (gets(s)){
            if (s[0]==''){
                break;
            }
            T.insert_(s);
        }
        while (~scanf("%s",s)){
            printf("%d
    ",T.search_(s));
        }
    }
    

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=2e6+5;
    int sum[maxn],flag[maxn];
    
    char s[maxn];
    struct trie
    {
        int tree[maxn][30],tot;
    
        void insert_(string str)
        {
            int len = str.length();
            int root = 0;
            for (int i = 0; i < len; i++)
            {
                int id = str[i] - 'a';
                if (!tree[root][id])
                {
                    tree[root][id] = ++tot;
                }
                sum[tree[root][id]]++;
                root = tree[root][id];
            }
            flag[root]=1;
        }
        int search_(string str)
        {
            int len = str.length();
            int root = 0;
            for (int i = 0; i < len; i++)
            {
                int id = str[i] - 'a';
                if (!tree[root][id])
                {
                    return 1;
                }
                root = tree[root][id];
            }
            if (flag[root])
            {
                return 0;
            }
            return 1;
        }
        void init()
        {
            for (int i=0; i<tot; i++)
            {
                flag[i]=0;
                for (int j=0; j<30; j++)
                {
                    tree[i][j]=0;
                }
            }
            tot=0;
        }
    } T;
    string str1,str2;
    int main()
    {
        while (getline(cin,str1))
        {
            int ans=0;
            T.init();
            if (str1=="#")
            {
                break;
            }
            stringstream ss(str1);
            while (ss>>str2)
            {
                if (T.search_(str2))
                {
                    ans++;
                    T.insert_(str2);
                }
            }
            printf("%d
    ",ans);
        }
    
    }

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int maxn=2e6+5;
    char s[maxn][30];
    int n;
    struct trie
    {
        int tree[maxn][30],sum[maxn],flag[maxn],tot;
        void insert_(char *str)
        {
            int len = strlen(str);
            int root = 0;
            for (int i = 0; i < len; i++)
            {
                int id = str[i] - 'a';
                if (!tree[root][id])
                {
                    tree[root][id] = ++tot;
                }
                sum[tree[root][id]]++;
                root = tree[root][id];
            }
            flag[root]=1;
        }
        string search_(char *str)
        {
            int len = strlen(str);
            int root = 0;
            string ans="";
            for (int i = 0; i < len; i++)
            {
                int id = str[i] - 'a';
                root = tree[root][id];
                ans+=str[i];
                if (sum[root]==1){
                    return ans;
                }
            }
        }
        void init()
        {
            for (int i=0; i<tot; i++)
            {
                flag[i]=sum[i]=0;
                for (int j=0; j<30; j++)
                {
                    tree[i][j]=0;
                }
            }
            tot=0;
        }
    } T;
    int main()
    {
        while (~scanf("%s",&s[++n])){
            T.insert_(s[n]);
        }
        for (int i=1;i<=n;i++){
            printf("%s ",s[i]);
            cout<<T.search_(s[i])<<endl;
        }
    }
    

      

  • 相关阅读:
    Linux命令-read
    pytest8-skip与xfail
    python模块-contextlib
    python中eval的使用
    python学习-面向对象(六)
    java学习-IDEA运行java程序报错
    nginx 使用SSL证书 代理https
    BC30560: “ExtensionAttribute”在命名空间“System.Runtime.CompilerServices”中不明确。
    nginx 代理相对路径使用不起
    nginx 解决不同web服务器 在同一服务器使用80端口问题
  • 原文地址:https://www.cnblogs.com/Accpted/p/11272691.html
Copyright © 2011-2022 走看看