• <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>
            隨筆 - 62  文章 - 96  trackbacks - 0
            <2007年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(7)

            隨筆分類(lèi)(66)

            隨筆檔案(62)

            文章分類(lèi)(31)

            文章檔案(32)

            友情鏈接

            最新隨筆

            積分與排名

            • 積分 - 235120
            • 排名 - 108

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            字符串hash函數(shù),解決沖突用開(kāi)放定址法,每次對(duì)哈希值加1
            在下列程序中,不是按常規(guī)方法用哈希表來(lái)記錄關(guān)鍵字,
            而是用整型數(shù)組Htable記錄關(guān)鍵字在字符串ch中的位置。
            在插入時(shí)不用把關(guān)鍵字復(fù)制到哈希表中,只是記錄一個(gè)索引,從而提高了效率。
            當(dāng)查詢(xún)時(shí),只要把Htable的值映射到字符串ch中就可以了。
            注意ch的下標(biāo)要從1開(kāi)始,因?yàn)镠table中的零值認(rèn)為是空,處理起來(lái)比較方便。
            #include<iostream>
            #include
            <string>
            using 
            namespace std;
            const int MAXN = 9973//哈希表長(zhǎng)度
            const int len = 30//字符串的最大長(zhǎng)度
            int Htable[MAX];
            char ch[MAX][len]; //存儲(chǔ)關(guān)鍵字的字符串
            unsigned 
            long Hash(char * key)
            {
                unsigned 
            long h = 0;
                
            while(*key)
                {
                    h 
            = (h << 4+ *key++;
                    unsigned 
            long g = h & 0xf0000000L;
                    
            if(g)
                        h 
            ^= g >> 24;
                    h 
            &= ~g;
                }
                
            return h % MAX;
            }
            int search(char * key)
            {
                unsigned 
            long i = Hash(key);
                
            while(Htable[i])
                {
                    
            if(strcmp(ch[Htable[i]], key) == 0)
                        
            return i;
                    i 
            = (i + 1) % MAX;
                }
                
            return -1;
            }
            int insert(char * key, int j) //j為關(guān)鍵字在ch中的位置,即索引
            {
                unsigned 
            long i = Hash(key);
                
            while(Htable[i])
                    i 
            = (i + 1) % MAX;
                Htable[i] 
            = j;
                
            return i;
            }
            posted @ 2007-04-07 16:22 beyonlin 閱讀(5518) | 評(píng)論 (3)編輯 收藏

            在優(yōu)先隊(duì)列中,優(yōu)先級(jí)高的元素先出隊(duì)列。
            標(biāo)準(zhǔn)庫(kù)默認(rèn)使用元素類(lèi)型的<操作符來(lái)確定它們之間的優(yōu)先級(jí)關(guān)系。
            優(yōu)先隊(duì)列的第一種用法,也是最常用的用法:

            priority_queue<int> qi;

            通過(guò)<操作符可知在整數(shù)中元素大的優(yōu)先級(jí)高。
            故示例1中輸出結(jié)果為:9 6 5 3 2

            第二種方法:
            在示例1中,如果我們要把元素從小到大輸出怎么辦呢?
            這時(shí)我們可以傳入一個(gè)比較函數(shù),使用functional.h函數(shù)對(duì)象作為比較函數(shù)。

            priority_queue<int, vector<int>, greater<int> >qi2;

            其中
            第二個(gè)參數(shù)為容器類(lèi)型。
            第二個(gè)參數(shù)為比較函數(shù)。
            故示例2中輸出結(jié)果為:2 3 5 6 9

            第三種方法:
            自定義優(yōu)先級(jí)。

            struct node
            {
                
            friend bool operator< (node n1, node n2)
                {
                    
            return n1.priority < n2.priority;
                }
                
            int priority;
                
            int value;
            };

            在該結(jié)構(gòu)中,value為值,priority為優(yōu)先級(jí)。
            通過(guò)自定義operator<操作符來(lái)比較元素中的優(yōu)先級(jí)。
            在示例3中輸出結(jié)果為:
            優(yōu)先級(jí)  值
            9          5
            8          2
            6          1
            2          3
            1          4
            但如果結(jié)構(gòu)定義如下:

            struct node
            {
                
            friend bool operator> (node n1, node n2)
                {
                    
            return n1.priority > n2.priority;
                }
                
            int priority;
                
            int value;
            };

            則會(huì)編譯不過(guò)(G++編譯器)
            因?yàn)闃?biāo)準(zhǔn)庫(kù)默認(rèn)使用元素類(lèi)型的<操作符來(lái)確定它們之間的優(yōu)先級(jí)關(guān)系。
            而且自定義類(lèi)型的<操作符與>操作符并無(wú)直接聯(lián)系,故會(huì)編譯不過(guò)。

            //代碼清單

            #include<iostream>
            #include
            <functional>
            #include
            <queue>
            using 
            namespace std;
            struct node
            {
                
            friend bool operator< (node n1, node n2)
                {
                    
            return n1.priority < n2.priority;
                }
                
            int priority;
                
            int value;
            };
            int main()
            {
                
            const int len = 5;
                
            int i;
                
            int a[len= {3,5,9,6,2};
                
            //示例1
                priority_queue
            <int> qi;
                
            for(i = 0; i < len; i++)
                    qi.push(a[i]);
                
            for(i = 0; i < len; i++)
                {
                    cout
            <<qi.top()<<" ";
                    qi.pop();
                }
                cout
            <<endl;
                
            //示例2
                priority_queue
            <int, vector<int>, greater<int> >qi2;
                
            for(i = 0; i < len; i++)
                    qi2.push(a[i]);
                
            for(i = 0; i < len; i++)
                {
                    cout
            <<qi2.top()<<" ";
                    qi2.pop();
                }
                cout
            <<endl;
                
            //示例3
                priority_queue
            <node> qn;
                node b[
            len];
                b[
            0].priority = 6; b[0].value = 1
                b[
            1].priority = 9; b[1].value = 5
                b[
            2].priority = 2; b[2].value = 3
                b[
            3].priority = 8; b[3].value = 2
                b[
            4].priority = 1; b[4].value = 4

                
            for(i = 0; i < len; i++)
                    qn.push(b[i]);
                cout
            <<"優(yōu)先級(jí)"<<'\t'<<"值"<<endl;
                for(i = 0; i < len; i++)
                {
                    cout
            <<qn.top().priority<<'\t'<<qn.top().value<<endl;
                    qn.pop();
                }
                
            return 0;
            }
            posted @ 2007-04-06 02:09 beyonlin 閱讀(57168) | 評(píng)論 (4)編輯 收藏
            k為數(shù)組a中最大值,n為數(shù)組a的長(zhǎng)度。
            countSort用于對(duì)整型數(shù)組排序,時(shí)間復(fù)雜度為O(k+n)。
            當(dāng)k = O(n)時(shí),時(shí)間復(fù)雜度變?yōu)镺(n)。
            c[i]記錄a數(shù)組中數(shù)值大于或等于i的個(gè)數(shù)
            int countSort(int * a, int k, int n)
            {
                
            int i;
                
            int * c = new int [k+1], * b = new int [n];
                
            for(i = 0; i <= k; i++)
                    c[i] 
            = 0;
                
            for(i = 0; i < n; i++)
                    c[a[i]]
            ++;
                
            for(i = 1; i <= k; i++)
                    c[i] 
            += c[i-1];
                
            for(i = n - 1; i >= 0; i--)
                {
                    b[c[a[i]]
            -1= a[i];
                    c[a[i]]
            --;
                }
                
            for(i = 0; i < n; i++)
                    a[i] 
            = b[i];
                delete [] b;
                delete [] c;
                
            return 0;
            }
            posted @ 2007-04-03 00:57 beyonlin 閱讀(868) | 評(píng)論 (0)編輯 收藏

            學(xué)到了一個(gè)新的知識(shí)點(diǎn):函數(shù)對(duì)象。
            定義了調(diào)用操作符的類(lèi),其對(duì)象稱(chēng)為函數(shù)對(duì)象。
            例如
            #include<iostream>
            using?
            namespace?std;
            struct?absInt
            {
            ????
            int?operator()?(int?v)
            ????{
            ????????
            return?v?>?0???v?:?-v;
            ????}
            };
            int?main()
            {?
            ????absInt?obj;
            ????
            int?a?=?obj(-15);
            ????cout
            <<a<<endl;
            ????
            return?0;
            }
            輸出結(jié)果為15。
            對(duì)類(lèi)absInt的對(duì)象obj使用調(diào)用操作符就像使用函數(shù)一樣。
            posted @ 2007-03-16 01:29 beyonlin 閱讀(595) | 評(píng)論 (0)編輯 收藏
            template <typename Iter>
            void insertSort(Iter *begin, Iter *end)
            {
            	for(Iter *it = begin + 1; it != end; it++)
            	{
            		Iter tmp = *it;
            		Iter *it2 = it - 1;
            		while(it2 > begin - 1 && *it2 > tmp)
            		{
            			*(it2 + 1) = *it2;
            			it2 --;
            		}
            		*(it2 + 1) = tmp;
            	}
            }
            
            posted @ 2007-02-06 19:13 beyonlin 閱讀(830) | 評(píng)論 (3)編輯 收藏
            僅列出標(biāo)題
            共12頁(yè): 1 2 3 4 5 6 7 8 9 Last 
            久久婷婷五月综合97色直播| 久久久久久久久66精品片| 久久精品成人免费国产片小草| 色偷偷偷久久伊人大杳蕉| 久久久久99这里有精品10| 亚洲精品乱码久久久久久蜜桃图片 | 热re99久久精品国99热| 久久国产高清字幕中文| 久久精品毛片免费观看| 一本色综合网久久| 狠狠综合久久综合88亚洲| 午夜视频久久久久一区| 精品视频久久久久| 狠狠色丁香婷婷综合久久来来去 | 办公室久久精品| 日本免费久久久久久久网站| 国内精品久久久久久99| 996久久国产精品线观看| 久久99国产精品久久久| 亚洲天堂久久久| 品成人欧美大片久久国产欧美| 久久精品aⅴ无码中文字字幕不卡 久久精品成人欧美大片 | 人人狠狠综合88综合久久| 国产情侣久久久久aⅴ免费| 人妻无码精品久久亚瑟影视| 久久国产亚洲精品麻豆| 久久久久久国产精品美女| 国内精品久久久久久久影视麻豆| 中文精品久久久久人妻不卡| 国内精品久久久久国产盗摄| 久久久久久九九99精品| 久久伊人精品一区二区三区| 国内精品伊人久久久久777| 国内精品免费久久影院| 久久国产亚洲精品麻豆| 久久夜色精品国产噜噜噜亚洲AV| 亚洲国产精品成人久久蜜臀| 亚洲精品无码久久久久久| 日韩欧美亚洲综合久久影院Ds| 久久青青草原国产精品免费 | 亚洲精品国精品久久99热一|