• <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;
            }
            久久精品无码午夜福利理论片| 日韩久久久久中文字幕人妻 | 成人a毛片久久免费播放| 久久久久综合网久久| 久久久国产一区二区三区| 久久强奷乱码老熟女网站| 久久精品无码午夜福利理论片 | 久久夜色精品国产www| 久久亚洲AV无码精品色午夜 | 亚洲中文精品久久久久久不卡| 久久国产热精品波多野结衣AV| 国内精品欧美久久精品| 中文字幕人妻色偷偷久久| 91精品国产91久久久久久青草 | 久久久人妻精品无码一区| 亚洲国产精品无码久久一线| 久久av高潮av无码av喷吹| 久久99精品国产麻豆| 精品熟女少妇AV免费久久| 久久亚洲2019中文字幕| 中文字幕成人精品久久不卡| 亚洲人成精品久久久久| 久久久久无码精品| 国产精品久久久久久久午夜片| 久久精品国产清高在天天线| 久久天天躁狠狠躁夜夜躁2014| 久久99精品国产99久久6| 国产精品久久久久天天影视| 无码日韩人妻精品久久蜜桃| 香蕉久久久久久狠狠色| 久久亚洲精品无码播放| 久久久综合香蕉尹人综合网| 91久久香蕉国产熟女线看| 久久最近最新中文字幕大全 | 久久国产福利免费| 国产精品免费久久久久影院| 国产亚洲精久久久久久无码AV| 久久国产高清字幕中文| 亚洲午夜久久久精品影院| 精品久久人人妻人人做精品| 91亚洲国产成人久久精品|