字符串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 stdnamespace 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 stdnamespace 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?stdnamespace?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) |
編輯 收藏