Hash table:
Build table complexity: O (n) k is the length of the string, n is the number of strings
Query O (1)!!!
Implementing the main operations
1. Make the string real
2. Put the string in. If the location is found to be empty, direct deposit, if already has the value, pull out a list
3. Enquiry
The algorithm seems to have no conflict, because once a conflict occurs, the strcmp string function compares them, although the complexity is higher
Reprint Source--http://blog.csdn.net/hlyfalsy/article/details/9238537
Konjac Konjac just made a little explanation.
/************************************************ * Author:P owatr * Created time:2015-9-4 15:34:45 * File Name : Hash.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int MAXN = 3e6 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e6 + 7;const int BASE = 131;st Ruct edge{edge* Next; Char s[88];} _HASH[MAXN], *h[mod], *cur;int get_id (char *s) {int hash = 0; int len = strlen (s); for (int i = 0; i < len; i++) Hash = (hash * BASE% mod + s[i])% mod ; Return hash;//string Conversion}void Bkdr_hash (char *s) {int code = GET_ID (s);//hash table header flag edge* p = h[code];//takes the address (the last of the code as the header flag) Number of addresses) while (p) {//If the head flag is not reached, always go up if (!strcmp (P->s, s)) return; else P = p->next; } strcpy (Cur->s, s); Cur->next = h[code];//The assignment between the addresses corresponds to the assignment between the values, A = B, and the address of B is changed without affecting the h[code of a] = cur++; }int Find (char *s) {int code = GET_ID (s); edge* p = H[code]; while (p) {//global variable initial address is 0, local variable has initial address, do not know why if this address is not processed, the default is 0 if (!strcmp (P->s, s)) return P-_hash;//p for the current string The address on the hash table, _hash is the initial address else P = p->next; } return-1;} int main () {cur = _hash;//Note If printf ("%d", cur), output is cur address instead of value: This in the XOR for a while, in fact, each read cur only increased by 2 char ss[100]; while (~SCANF ("%s", ss)) {if (ss[0] = = ' @ ') break; GetChar (); Bkdr_hash (ss); Gets (ss); Bkdr_hash (ss); } int m; scanf ("%d", &m); GetChar (); while (m--) {gets (ss); int id = FIND (ss); if (id = =-1) printf ("what?\n"); else {char *node = _HASH[ID^1].S; if (node[0] = = ' [') {for (int i = 1; node[i]! = '] '; i++) printf ("%c", Node[i]); Puts (""); } else {puts (node); }}} return 0;}
hdu1880--hash Table (BKDR algorithm)--Magic Spell Dictionary