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

            A Za, A Za, Fighting...

            堅(jiān)信:勤能補(bǔ)拙

            PKU 2418 Hardwood Species

            問題:
            http://poj.org/problem?id=2418

            思路:
            題意清晰明了,不難,用三種方法分別實(shí)現(xiàn):
            快速排序
            動(dòng)態(tài)生成節(jié)點(diǎn)的二叉查找樹
            靜態(tài)分配節(jié)點(diǎn)的二叉查找樹

            結(jié)果發(fā)現(xiàn),原來對于快速排序與靜態(tài)分配節(jié)點(diǎn)都不是很熟悉,二維數(shù)組的快速排序分析見http://www.shnenglu.com/Joe/archive/2010/10/29/131746.html,而動(dòng)態(tài)生成節(jié)點(diǎn)則需要注意如果函數(shù)需要修改指針,那么必須傳遞指向指針的指針,因?yàn)镃是值傳遞的

            另外,我以為靜態(tài)分配節(jié)點(diǎn)應(yīng)該比動(dòng)態(tài)分配節(jié)點(diǎn)節(jié)約很多時(shí)間的,結(jié)果居然差不多...而快速排序在這題明顯是最耗時(shí)的

            代碼(快排)
             1 /* 35640K 1844MS */
             2 #include<stdio.h>
             3 #include<stdlib.h>
             4 #include<string.h>
             5 #define MAX_LEN 36
             6 #define MAX_NUM 1000001
             7 char species[MAX_NUM][MAX_LEN];
             8 
             9 int
            10 cmp(const void *arg1, const void *arg2)
            11 {
            12     return strcmp((char *)arg1, (char *)arg2);
            13 }
            14 
            15 int
            16 main(int argc, char **argv)
            17 {
            18     int i, count, total = 0;
            19     while(gets(species[total]) != NULL)
            20         ++total;
            21     qsort(species, total, sizeof(species[0]), cmp);
            22     count = 1;
            23     for(i=1; i<total; i++) {
            24         if(strcmp(species[i], species[i-1]) == 0
            25             ++count;
            26         else {
            27             printf("%s %.4f\n", species[i-1], (count*100.0)/total);
            28             count = 1;
            29         }
            30     }
            31     printf("%s %.4f\n", species[total-1], (count*100.0)/total);
            32 }

            代碼(動(dòng)態(tài)分配節(jié)點(diǎn)的BST,原本想實(shí)現(xiàn)下destroy函數(shù)的,結(jié)果怕麻煩就留給系統(tǒng)回收吧(*^__^*) 嘻嘻……)
             1 /* binary search tree(dynamic allocation) */
             2 /* 544K 1188MS */
             3 #include<stdio.h>
             4 #include<stdlib.h>
             5 #include<string.h>
             6 #include<assert.h>
             7 #define MAX_LEN 36
             8 struct Node {
             9     char spec[MAX_LEN];
            10     int count;
            11     struct Node *left, *right;
            12 };
            13 int total;
            14 
            15 struct Node *
            16 create_node(char *str)
            17 {    
            18     struct Node *node = (struct Node *)malloc(sizeof(struct Node));
            19     assert(node != NULL);
            20     strcpy(node->spec, str);
            21     node->left = node->right = NULL;
            22     node->count = 1;
            23     return node;
            24 }
            25 
            26 void
            27 insert(struct Node **root, char *str)
            28 {
            29     int ret;
            30     struct Node *node;
            31     if(*root==NULL) {
            32         *root = create_node(str);
            33         return;
            34     }
            35     ret = strcmp((*root)->spec, str);
            36     if(ret == 0)
            37         ++((*root)->count);
            38     else if(ret < 0)
            39         insert(&((*root)->right), str);
            40     else
            41         insert(&((*root)->left), str);
            42 }
            43 
            44 void
            45 inorder(struct Node *root)
            46 {
            47     if(root == NULL)
            48         return;
            49     inorder(root->left);
            50     printf("%s %.4f\n", root->spec, (root->count)*100.0/total);
            51     inorder(root->right);
            52 }
            53 
            54 void
            55 destroy(struct Node **root)
            56 {
            57 }
            58 
            59 int
            60 main(int argc, char **argv)
            61 {
            62     char str[MAX_LEN];
            63     struct Node *bst = NULL;
            64     total = 0;
            65     while(gets(str) != NULL) {
            66         ++total;
            67         insert(&bst, str);
            68     }
            69     inorder(bst);
            70 }

            代碼(靜態(tài)分配節(jié)點(diǎn)的BST)
             1 /* binary search tree(static allocation) */
             2 /* 492K 1188MS */
             3 #include<stdio.h>
             4 #include<stdlib.h>
             5 #include<string.h>
             6 #include<assert.h>
             7 #define MAX_LEN 36
             8 #define MAX_NUM 10007
             9 #define ROOT 1
            10 struct Node {
            11     char spec[MAX_LEN];
            12     int count;
            13     int left, right;
            14 }bst[MAX_NUM];
            15 int cur_index, total;
            16 
            17 int
            18 find(int root, char *str)
            19 {
            20     int ret, parent, cur = root;
            21     while(cur != 0) {
            22         parent = cur;
            23         ret = strcmp(bst[cur].spec, str);
            24         if(ret == 0) {
            25             ++bst[cur].count;
            26             return 0;
            27         } else if(ret < 0) {
            28             cur = bst[cur].right; 
            29         } else {
            30             cur = bst[cur].left;
            31         }
            32     }
            33     return parent;
            34 }
            35 
            36 #define ADD(index, str) { \
            37     strcpy(bst[index].spec, str); \
            38     bst[index].left = bst[index].right = 0; \
            39     bst[index].count = 1; \
            40     ++index; }
            41 
            42 void
            43 insert(int parent, char *str)
            44 {
            45     int ret = strcmp(bst[parent].spec, str);
            46     assert(ret != 0);
            47     if(ret < 0)
            48         bst[parent].right = cur_index;
            49     else 
            50         bst[parent].left = cur_index;
            51     ADD(cur_index, str);
            52 }
            53 
            54 void
            55 inorder(int index) 
            56 {
            57     if(index == 0)
            58         return;
            59     inorder(bst[index].left);
            60     printf("%s %.4f\n", bst[index].spec, (bst[index].count*100.0)/total);
            61     inorder(bst[index].right);
            62 }
            63 
            64 int
            65 main(int argc, char **argv)
            66 {
            67     int parent;
            68     char str[MAX_LEN];
            69     total = 1;
            70     cur_index = ROOT;
            71     gets(str);
            72     ADD(cur_index, str); /* create the root node first */
            73     while(gets(str) != NULL) {
            74         ++total;
            75         if((parent=find(ROOT, str)) > 0)
            76             insert(parent, str);
            77     }
            78     inorder(ROOT);
            79 }

            posted on 2010-10-30 00:59 simplyzhao 閱讀(318) 評論(0)  編輯 收藏 引用 所屬分類: A_排序

            導(dǎo)航

            <2010年7月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲一区中文字幕久久| 午夜天堂av天堂久久久| 久久久久久亚洲精品不卡| 日本精品久久久久影院日本 | 欧美亚洲国产精品久久蜜芽| 国产精品亚洲综合久久 | 亚洲中文字幕伊人久久无码| 久久精品国产一区二区三区不卡| 久久天天日天天操综合伊人av| 久久人人爽人人爽人人片AV东京热| 久久久久久人妻无码| 久久精品水蜜桃av综合天堂| 一本久久a久久精品综合夜夜| 色偷偷88欧美精品久久久| 久久久久久国产精品免费无码| 婷婷久久综合| 四虎国产永久免费久久| 少妇高潮惨叫久久久久久| 亚洲国产精品无码久久九九| 久久精品黄AA片一区二区三区| 色婷婷狠狠久久综合五月| 欧美亚洲国产精品久久蜜芽| 亚洲国产成人久久一区久久| 午夜精品久久久久久久久| 亚洲国产精品综合久久一线| 国产精品久久久久天天影视| 久久国产亚洲精品| 伊人久久成人成综合网222| 国产日韩久久久精品影院首页| 一本久久a久久精品亚洲| 2022年国产精品久久久久| 国产成人精品久久| 久久精品国产亚洲AV蜜臀色欲| 久久国产香蕉视频| 久久精品无码一区二区三区日韩 | 久久久亚洲裙底偷窥综合| 久久国产成人精品国产成人亚洲| 精品国产乱码久久久久久郑州公司| 狠狠综合久久AV一区二区三区| 要久久爱在线免费观看| 欧美日韩中文字幕久久伊人|