Guest User

NeoGAF question

a guest
Mar 7th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. int main() {
  2.   cout << "Enter some words: ";
  3.   vector<string> words;
  4.   string w;
  5.   while(cin >> w) {
  6.     words.push_back(w);
  7.   }
  8.   sort(words.begin(), words.end());
  9.   int count = 1;
  10.   int most_so_far = 0;
  11.   vector<string> most_frequent;
  12.   for(int i = 0; i < words.size(); ++i) {
  13.     if(words[i] == words[i+1]) {
  14.       ++count;
  15.     }
  16.     else if(words[i] != words[i+1]) {
  17.       if(count > most_so_far) {
  18.     most_so_far = count;
  19.     most_frequent.clear();
  20.     most_frequent.push_back(words[i]);
  21.     count = 1;
  22.       }
  23.       else if(count == most_so_far) {
  24.     most_frequent.push_back(words[i]);
  25.     count = 1;
  26.       }
  27.       else {        //count < most_so_far
  28.     count = 1;
  29.       }
  30.     }
  31.   }
  32.   sort(most_frequent.begin(), most_frequent.end());
  33.  
  34.   cout << "The most frequently occuring words are: \n\n";
  35.  
  36.   for(int i = 0; i < most_frequent.size(); ++i) {
  37.     cout << most_so_far << " " << most_frequent[i] << endl;
  38.   }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment