#include #include #include #include using namespace std; int find(const string query, const string words[], const int counts[], const int N) { int pos = 0; while (pos < N) { if (words[pos] == query) return pos; pos += 1; } return -1; } int most(const string words[], const int counts[], const int N) { int pos = 0, biggest = 0, where; while (pos < N) { if (counts[pos] > biggest) { biggest = counts[pos]; where = pos; } pos += 1; } return where; } int main() { string words[48000]; int counts[48000]; int N = 0; ifstream fi("peterpan"); if (fi.fail()) { cerr << "Can't open\n"; exit(1); } while (true) { string query; fi >> query; if (fi.fail()) break; const int pos = find(query, words, counts, N); if (pos < 0) { words[N] = query; counts[N] = 1; N += 1; } else counts[pos] += 1; } fi.close(); cout << N << " unique words\n"; while (true) { int c = most(words, counts, N); if (c == 0) break; cout << words[c] << " appears " << counts[c] << " times\n"; string safes = words[c]; words[c] = words[N - 1]; words[N - 1] = safes; int safei = counts[c]; counts[c] = counts[N - 1]; counts[N - 1] = safei; N -= 1; } }