#include #include #include #include #include #include using namespace std; #define HASH_SIZE 1234567 struct info { char s[260]; int count; int nom; info(char* _s, int __n) { strcpy(s, _s); count = 1; nom = __n; } void add() { count++; } }; vector hash[HASH_SIZE]; static unsigned rand_keys[300]; unsigned hashit(char *s) { int l = strlen(s); unsigned key = 0; for (int i = 0; i < l; i++) { key = key ^ (((int)s[i]) * rand_keys[i]) ^ (key << 6) ^ (key>>1); } return key; } int main(void) { /* FILE *f = fopen("freq.inp", "wt"); for (int i = 0; i < 100000; i++) { string s; for (int j = 0; j < 255; j++) { s += rand()%26 + 'A'; } fprintf(f, "%s\n", s.c_str()); } fclose(f);*/ int nom = 0; for (int i = 0; i < 200; i++) rand_keys[i] = rand(); char ss[1000]; FILE *f = fopen("freq.inp", "rt"); if (f == NULL) { perror("ha"); return -1; } while (1 == fscanf(f, "%s", ss)) { nom ++; // string s(ss); /* map::iterator i; if ( ( i = m.find(s) ) != m.end() ) { i->second++; }else m.insert(make_pair(s, 1));*/ unsigned hk = hashit(ss) % HASH_SIZE; bool found = false; for (int i = 0; i < hash[hk].size(); i++) { if (0 == strcmp(hash[hk][i].s, ss)) { hash[hk][i].count++; found = true; break; } } if (!found) { info q(ss, nom); hash[hk].push_back(q); } } fclose(f); int mmax = -1; int cnom = 999666111; char ok[260]; int hentries = 0; for (int i = 0; i < HASH_SIZE; i++) { if (hash[i].size()) hentries ++; for (int j = 0; j < hash[i].size(); j++) { if (hash[i][j].count > mmax) { mmax = hash[i][j].count; cnom = hash[i][j].nom; strcpy(ok, hash[i][j].s); } else { if (hash[i][j].count == mmax && hash[i][j].nom < cnom) { cnom = hash[i][j].nom; strcpy(ok, hash[i][j].s); } } } } /* for (map::iterator i = m.begin(); i != m.end(); i++) { if (i->second > mmax) { mmax = i->second; ok = i->first; } }*/ FILE *out = fopen("freq.out", "wt"); fprintf(f, "%s\n", ok); fclose(f); // printf("%d\n", clock()); //printf("%d hash entries used\n", hentries); // system("pause"); return 0; }