#include #include #include #include #include #include #include using namespace std; const int arrsize = 10000; struct entry { string word; int count; entry * next; entry(string w, int c, entry * n) { word = w; count = c; next = n; } }; entry * table[arrsize]; unsigned int hashfn(string s) { unsigned int v = 92439827; for (int i = 0; i < s.length(); i += 1) v = v * 169 + s[i]; return v % arrsize; } void add(string s) { int hv = hashfn(s); entry * e = table[hv]; while (e != NULL) { if (e->word == s) { e->count += 1; return; } e = e->next; } table[hv] = new entry(s, 1, table[hv]); } int lookup(string s) { int hv = hashfn(s); entry * e = table[hv]; while (e != NULL) { if (e->word == s) return e->count; e = e->next; } return 0; } void examine() { int counts[11]; for (int i = 0; i < 11; i += 1) counts[i] = 0; for (int i = 0; i < arrsize; i += 1) { int len = 0; entry * e = table[i]; while (e != NULL) { len += 1; e = e->next; } if (len > 10) len = 10; counts[len] += 1; } for (int i = 0; i < 11; i += 1) cout << i << ": " << counts[i] << "\n"; } int main() { for (int i = 0; i < arrsize; i += 1) table[i] = NULL; ifstream fin("peterpan.txt"); if (fin.fail()) { cout << "file not found\n"; exit(1); } string s; while (true) { fin >> s; if (fin.fail()) break; add(s); } fin.close(); examine(); while (true) { cout << "> "; cin >> s; if (cin.fail()) break; cout << lookup(s) << "\n"; } }