• <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;
            }
            精品水蜜桃久久久久久久| 色天使久久综合网天天| 亚洲国产小视频精品久久久三级 | 伊人久久大香线蕉精品不卡| 欧美粉嫩小泬久久久久久久| 午夜不卡久久精品无码免费| 91久久精一区二区三区大全| 久久久久久无码国产精品中文字幕| 囯产精品久久久久久久久蜜桃| 中文字幕成人精品久久不卡| 亚洲午夜无码AV毛片久久| 久久亚洲日韩精品一区二区三区| 久久这里只有精品视频99| 久久婷婷国产综合精品| 久久强奷乱码老熟女| 97久久精品无码一区二区| 亚洲人成无码www久久久| 亚洲精品无码久久久影院相关影片| 色噜噜狠狠先锋影音久久| 久久人爽人人爽人人片AV| 女同久久| 蜜臀久久99精品久久久久久 | 99久久国产免费福利| 久久精品国产亚洲AV香蕉| 亚洲日本va午夜中文字幕久久| 69久久精品无码一区二区| 18岁日韩内射颜射午夜久久成人| 精品久久国产一区二区三区香蕉 | 一本久久a久久精品vr综合| 日韩久久无码免费毛片软件| 久久久久一本毛久久久| 一本久久久久久久| 狠狠色丁香婷婷综合久久来来去| 久久精品一区二区三区不卡| 精品久久久久久中文字幕| 久久久久免费看成人影片| 精品人妻久久久久久888| 热久久这里只有精品| 久久久久免费精品国产| 国产成人无码精品久久久久免费| 爱做久久久久久|