• <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小加

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

            // trietree.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。
            //
            #include<cstdio>
            #include<iostream>
            #include<string.h>

            using namespace std;
            const int num_chars = 26;            //關(guān)鍵碼最大位

            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   //關(guān)鍵碼類型
                {
                    bool isfin;
                    int cnt;                     //關(guān)鍵碼當(dāng)前位數(shù)
                    Trie_node* branch[num_chars];   //關(guān)鍵碼存放數(shù)組
                    Trie_node();
                };
                Trie_node* root;
            };

            Trie::Trie_node::Trie_node()   //結(jié)點(diǎn)定義
            {
                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;
            }
            久久噜噜电影你懂的| 久久夜色精品国产亚洲av| 一本一道久久综合狠狠老| 亚洲乱码中文字幕久久孕妇黑人| 色偷偷偷久久伊人大杳蕉| 国产精品久久久久久搜索| 久久久久久极精品久久久| 欧美久久亚洲精品| 久久精品国产99久久无毒不卡| 久久香蕉一级毛片| 精品久久久无码人妻中文字幕| 精品一区二区久久| 久久精品亚洲AV久久久无码| 久久Av无码精品人妻系列| 污污内射久久一区二区欧美日韩| 午夜精品久久久久久久久| 久久综合精品国产一区二区三区 | 久久久精品人妻一区二区三区蜜桃| 人妻久久久一区二区三区| 精品久久久久久久久久中文字幕| 中文无码久久精品| 久久精品无码一区二区app| 国产三级久久久精品麻豆三级| 久久精品无码一区二区app| 久久这里只有精品久久| 少妇高潮惨叫久久久久久| 一极黄色视频久久网站| 国产精品丝袜久久久久久不卡| 久久丫精品国产亚洲av| 99精品国产99久久久久久97| 久久青青草原精品国产软件| 亚洲综合婷婷久久| 99久久国产综合精品麻豆| 天堂久久天堂AV色综合| 国内高清久久久久久| 久久久久久精品久久久久| 久久亚洲精品国产精品婷婷| 久久亚洲AV无码西西人体| 精品久久久久久无码国产| 久久91精品综合国产首页| 久久精品国产亚洲7777|