Problem p00242 (C++) — buggy submission, verdict: Time Limit Exceeded

// 2013/07/31 Tazoe

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct dic {
  string ds;
  int di;
};

bool operator<(const dic &left, const dic &right) { return left.di < right.di; }

bool operator>(const dic &left, const dic &right) { return left.di > right.di; }

int main() {
  while (true) {
    string N;
    getline(cin, N);

    if (N == "0")
      break;

    istringstream isN(N);
    int n;
    isN >> n;

    //		cout << n << endl;

    vector<string> w;

    for (int i = 0; i < n; i++) {
      string S;
      getline(cin, S);

      istringstream isS(S);
      while (!isS.eof()) {
        string s;
        isS >> s;

        //				cout << s << endl;

        w.push_back(s);
      }
    }

    sort(w.begin(), w.end());
    /*
                    for(int i=0; i<w.size(); i++){
                            cout << w[i] << endl;
                    }
    */
    string K;
    getline(cin, K);

    istringstream isK(K);
    char k;
    isK >> k;

    //		cout << k << endl;

    vector<struct dic> D;

    for (int i = 0; i < w.size(); i++) {
      if (w[i][0] != k)
        continue;

      //			cout << w[i] << endl;

      bool flg = false;

      for (int j = 0; j < D.size(); j++) {
        if (D[j].ds == w[i]) {
          D[j].di++;
          flg = true;
          break;
        }
      }

      if (!flg) {
        struct dic tmp;
        tmp.ds = w[i];
        tmp.di = 1;
        D.push_back(tmp);
      }
    }

    stable_sort(D.begin(), D.end(), greater<dic>());
    /*
                    for(int i=0; i<D.size(); i++){
                            cout << D[i].di << ' ' << D[i].ds << endl;
                    }
    */
    if (D.size() == 0) {
      cout << "NA" << endl;
    } else {
      int m = D.size();
      if (m > 5) {
        m = 5;
      }

      for (int i = 0; i < m; i++) {
        cout << D[i].ds;

        if (i == m - 1) {
          cout << endl;
        } else {
          cout << ' ';
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

// 2012/07/27 Tazoe

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

struct word {
  string w;
  int c;
};

bool operator<(const struct word &left, const struct word &right) {
  return (left.c > right.c) || (left.c == right.c && left.w < right.w);
}

int main() {
  while (true) {
    string s;
    getline(cin, s);

    //		cout << s << endl;

    if (s == "0")
      break;

    string k;
    getline(cin, k);

    //		cout << k[0] << endl;

    istringstream is(s);
    string w;
    vector<struct word> d;
    while (is >> w) {
      if (w[0] == k[0]) {
        bool flg = false;
        for (int i = 0; i < d.size(); i++) {
          if (d[i].w == w) {
            d[i].c++;
            flg = true;
            break;
          }
        }

        if (!flg) {
          struct word tmp;
          tmp.w = w;
          tmp.c = 1;
          d.push_back(tmp);
        }
      }
    }

    sort(d.begin(), d.end());

    //		for(int i=0; i<d.size(); i++)
    //			cout << d[i].w << ' ' << d[i].c << endl;

    if (d.size() != 0) {
      for (int i = 0; i < d.size(); i++) {
        cout << d[i].w;
        if (i == 4) {
          cout << endl;
          break;
        }

        if (i != d.size() - 1) {
          cout << ' ';
        } else {
          cout << endl;
        }
      }
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}