• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C小加

            厚德 博學 求真 至善 The bright moon and breeze
            posts - 145, comments - 195, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
            以前學數據結構的時候吧字典樹忽視了,導致到現在才學到。
            字典樹是一種樹形的數據結構,插入的復雜度為單詞的平均長度。本題只需要寫一個插入函數就可以了。
            由于過幾天就要省賽了,等到省賽后總結一下字典樹。

            // trietree.cpp : 定義控制臺應用程序的入口點。
            //
            #include<cstdio>
            #include<iostream>
            #include<string.h>

            using namespace std;
            const int num_chars = 26;            //關鍵碼最大位

            int _max;
            char ans[12];

            class Trie
            {
            public:
                Trie();
                Trie(Trie& tr);
                virtual ~Trie();
                //int trie_search(const char* word, char*entry) const;
                bool insert(const char* word);
            protected:
                struct Trie_node   //關鍵碼類型
                {
                    bool isfin;
                    int cnt;                     //關鍵碼當前位數
                    Trie_node* branch[num_chars];   //關鍵碼存放數組
                    Trie_node();
                };
                Trie_node* root;
            };

            Trie::Trie_node::Trie_node()   //結點定義
            {
                isfin = false;
                cnt = 0;
                for(int i = 0; i < num_chars;++i)
                    branch[i] = NULL;
            }

            Trie::Trie():root(NULL)
            {
            }

            Trie::~Trie()
            {
            }



            bool Trie::insert(const char*word)
            {
                int result = 1,position = 0;
                if(root == NULL)
                    root = new Trie_node;
                char char_code;
                Trie_node *location = root;
                while(location != NULL && *word != 0)
                {
                    if(*word >= 'a'&& *word <= 'z')
                        char_code = *word - 'a';
                    if(location->branch[char_code] == NULL)
                        location->branch[char_code] = new Trie_node;
                    location = location->branch[char_code];
                    position++;
                    word++;
                }

                ++location->cnt;
                if(location->cnt>_max)
                {
                    _max=location->cnt;
                    return true;
                }
                else
                return false;
            }


            int main()
            {
                Trie t;
                int n;
                while(scanf("%d",&n)!=EOF)
                {
                    _max=0;
                    char s[12];
                    for(int i=0;i<n;++i)
                    {
                        scanf("%s",s);
                        if(t.insert(s))
                        {
                            strcpy(ans,s);
                        }

                    }
                    printf("%s %d\n",ans,_max);
                }


                return 0;
            }
            色老头网站久久网| 99久久国产亚洲综合精品| 久久久久久亚洲精品成人 | 久久线看观看精品香蕉国产| 国产亚洲精品美女久久久| 国产精品久久久久久一区二区三区| 91视频国产91久久久| 久久综合色区| 久久发布国产伦子伦精品| 日韩精品久久久久久| 国产精品久久久久蜜芽| 2022年国产精品久久久久 | 成人久久精品一区二区三区| 久久精品国产精品亜洲毛片| 久久人妻AV中文字幕| 色综合久久中文色婷婷| 亚洲AV无码一区东京热久久| 久久国产热这里只有精品| 色婷婷综合久久久久中文一区二区| 91久久精品无码一区二区毛片| 久久精品日日躁夜夜躁欧美| 久久99精品久久久久久9蜜桃| 久久久无码精品亚洲日韩按摩 | 久久免费看黄a级毛片| 欧美激情精品久久久久久久九九九| 久久Av无码精品人妻系列 | 欧美精品丝袜久久久中文字幕| 精品久久久久久久久中文字幕| 大香伊人久久精品一区二区| 国产—久久香蕉国产线看观看| 国产精品一区二区久久国产| 18禁黄久久久AAA片| 午夜视频久久久久一区| 久久涩综合| 亚洲国产小视频精品久久久三级 | 久久久久久久久久免免费精品 | 久久免费看黄a级毛片| 99久久精品免费看国产一区二区三区| 久久九九久精品国产| 久久免费视频一区| 亚洲国产日韩综合久久精品|