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;
    }
}

  

原文地址:https://www.cnblogs.com/Accpted/p/11272691.html