字符串哈希-BKDRHash
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                字符串哈希-BKDRHash
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                代碼如下:
#include <iostream> #include <vector> using namespace std; const int N = 10005;struct node {char name[35]; };unsigned int BKDRHash(char *str) {unsigned int key = 0, seed = 31;while (*str) {key = key * seed + (*str++);}return key & 0x7fffffff; }vector<node>List[N];//這里要為N,因為%N,所以要N,開小的話程序就會出問題。 //List的作用:把沖突的hash值存起來 int main() {int n, key ;cin >> n;node t;for (int i = 1; i <= n; i++) {cin >> t.name;key = BKDRHash(t.name) % N;//計算hash值,并求余List[key].push_back(t);}int cnt;cin >> cnt;//查詢次數while (cnt--) {bool flag = false;char s[35];cin >> s;key = BKDRHash(s) % N;for (int i = 0; i < List[key].size(); i++) {if (strcmp(List[key][i].name, s) == 0) {cout << "yes" << endl;flag = true;break;}}if (!flag)cout << "no" << endl;}return 0; }總結
以上是生活随笔為你收集整理的字符串哈希-BKDRHash的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 鲁大师怎么给电脑跑分鲁大师电脑如何
 - 下一篇: 三星刷机教程(卡刷)