• POJ 2001


     #include<iostream>
     using namespace  std;
     const int kind=26;
     struct trienode
     {
        trienode * next[kind];    
        int branch;
        trienode()
        {
            branch=0;
            for(int i=0;i<kind;i++)
                next[i]=NULL;    
        }
    };
    class trie
    {
    
        trienode * root;
    public:
        trie()
        {
            root=NULL;
        }
        void insert(char s[])
        {
            trienode * location = root;
            if(location == NULL)
                location = root = new trienode();
            int i = 0,k;
            while(s[i])
            {
                k=s[i]-'a';
                if(location->next[k])
                    location->next[k]->branch++;
                else
                {
                    location->next[k]=new trienode();
                    location->next[k]->branch++;    
                }
                i++;
                location = location->next[k];    
            }
        }
        int search(char s[]){
            trienode * location = root;
            if(!location) 
                return 0;
            int k,i = 0,ans;
            while(s[i])
            {
                k=s[i] - 'a';
                if(!location->next[k])
                return 0;
                cout<<s[i];
                ans = location->next[k]->branch;
                if(ans == 1)
                    return 1;
                location = location->next[k];
                i++;
            }  
            return ans;
        }
        
    };
    int main()
    {
        //freopen("acm.acm","r",stdin);
        char a[1000][26];
        char b[26];
        int i = 0;
        int j;
        int num;
        trie mytrie;
        while(cin>>a[i])
        {
            mytrie.insert(a[i]);
            ++ i;
        }
        for(j = 0; j < i; ++ j)
        {
            cout<<a[j]<<" ";
            mytrie.search(a[j]);
            cout<<endl;
        }
        return 0;
    }

    关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。 

    技术网站地址: vmfor.com

  • 相关阅读:
    JavaScript——闭包(转自别人)
    JavaScript ——内部函数和匿名函数
    socks5代理服务器ss5配置
    TCP部首长度定义
    九个Console命令,让js调试更简单
    一位前端新手入住博客园
    动画库 Tweenmax 使用示例1
    Dijkstra算法实例
    N皇后问题
    华为精益研发外包 PDF
  • 原文地址:https://www.cnblogs.com/gavinsp/p/4566606.html
Copyright © 2020-2023  润新知