• <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;
            }
            亚洲午夜久久久久妓女影院| 久久不射电影网| 久久久久人妻一区二区三区| 亚洲人成精品久久久久| 国产∨亚洲V天堂无码久久久| 久久福利青草精品资源站| 久久亚洲高清综合| 久久香综合精品久久伊人| 伊人久久大香线蕉影院95| 伊人久久精品影院| 日本道色综合久久影院| 思思久久精品在热线热| 亚洲国产成人久久精品动漫| 久久久久高潮综合影院| 伊人久久免费视频| 久久久久亚洲av无码专区导航| 久久国产一片免费观看| 国产∨亚洲V天堂无码久久久| 亚洲国产精品成人久久蜜臀| 久久综合狠狠色综合伊人| 久久精品国产亚洲av瑜伽| 久久99国产综合精品| 久久91精品国产91| 久久精品女人天堂AV麻| 国产欧美一区二区久久| 色婷婷综合久久久久中文一区二区| 久久无码国产| 久久精品免费大片国产大片| 久久99精品国产一区二区三区| 77777亚洲午夜久久多喷| 久久经典免费视频| 久久精品国产99国产电影网| 午夜精品久久久久久99热| 热久久最新网站获取| 亚洲а∨天堂久久精品| 久久久综合香蕉尹人综合网| 国产免费久久精品99久久| 久久国产AVJUST麻豆| 久久91精品国产91久| 精品国产乱码久久久久久呢| 中文字幕人妻色偷偷久久|