• <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;
            }
            久久精品www| 99久久久精品免费观看国产| 久久精品成人| 少妇无套内谢久久久久| 一本一本久久a久久综合精品蜜桃| 日韩va亚洲va欧美va久久| 久久这里只精品99re66| 人妻精品久久久久中文字幕一冢本| 久久www免费人成看片| 国产精品永久久久久久久久久| 久久久久久久久久免免费精品| 久久久精品2019免费观看| 久久国产视屏| 丁香五月网久久综合| 日韩十八禁一区二区久久| 久久精品国产99久久无毒不卡 | 久久99国产一区二区三区| 青青草原综合久久大伊人| 99久久无码一区人妻| 久久99毛片免费观看不卡| 久久亚洲精品无码AV红樱桃| 久久国产精品无| 精品无码久久久久久午夜| 久久久精品人妻一区二区三区蜜桃| 久久久久亚洲精品日久生情| 蜜桃麻豆WWW久久囤产精品| 久久综合九色综合网站| 伊人久久综合成人网| 久久精品国产精品亚洲毛片| 精品无码久久久久久午夜| 精品精品国产自在久久高清| 国产精品免费久久久久久久久| 久久久久亚洲精品天堂久久久久久| 精品久久久久久国产三级| 天天爽天天狠久久久综合麻豆| 一本久道久久综合狠狠爱| 国内精品伊人久久久久影院对白| 91精品国产91久久久久久| 伊人久久大香线蕉无码麻豆| 国产精品久久久福利| 亚洲中文字幕伊人久久无码|