Problem p02773 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG // TLEの原因になるので注意！！！！！！！！！！！
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

vector<string> SS;

int main() {
  int n;
  cin >> n;
  string s[n];
  rep(i, n) {
    cin >> s[i];
    SS.push_back(s[i]);
  }
  sort(SS.begin(), SS.end());
  cerr << endl;

  rep(i, n) { cerr << SS[i] << endl; }

  ll k = 0;
  string kk = "-1";
  vector<pair<ll, string>> PP;
  rep(i, n) {
    if (SS[i] == kk) {
      k++;
      if (i == n - 1) {
        PP.push_back(mp(k, kk));
      }
    } else {
      PP.push_back(mp(k, kk));
      cerr << k << ' ' << kk << endl;
      k = 1;
      kk = SS[i];
      if (i == n - 1) {
        PP.push_back(mp(k, kk));
      }
    }
  }
  sort(PP.begin(), PP.end(), greater<pair<ll, string>>());
  ll kkk = PP[0].first;
  cerr << kkk << endl;
  ll i = 0;
  cerr << endl;
  vector<string> SSS;
  while (PP[i].first == kkk) {
    SSS.push_back(PP[i].second);
    i++;
  }
  rep(j, SSS.size()) { cout << SSS[SSS.size() - j - 1] << endl; }
  /*
  pair<int,string> p[n];
  rep(i,SS.size()){
      p[i]=pair(SS[i].second,SS[i].first);
      PP.push_back(p[i]);
  }
  PP.sort();
  int k=PP[0].first;
  int i=0;
  while(PP[i].first==k){
      cout<<PP[i].second<<endl;
      i++;
  }
  */
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG // TLEの原因になるので注意！！！！！！！！！！！
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

vector<string> SS;

int main() {
  int n;
  cin >> n;
  string s[n];
  rep(i, n) {
    cin >> s[i];
    SS.push_back(s[i]);
  }
  sort(SS.begin(), SS.end());
  cerr << endl;
  /*
  rep(i,n){
      cerr<<SS[i]<<endl;
  }
  */
  ll k = 0;
  string kk = "-1";
  vector<pair<ll, string>> PP;
  rep(i, n) {
    if (SS[i] == kk) {
      k++;
      if (i == n - 1) {
        PP.push_back(mp(k, kk));
      }
    } else {
      PP.push_back(mp(k, kk));
      cerr << k << ' ' << kk << endl;
      k = 1;
      kk = SS[i];
      if (i == n - 1) {
        PP.push_back(mp(k, kk));
      }
    }
  }
  sort(PP.begin(), PP.end(), greater<pair<ll, string>>());
  ll kkk = PP[0].first;
  cerr << kkk << endl;
  ll i = 0;
  cerr << endl;
  vector<string> SSS;
  while (PP[i].first == kkk) {
    SSS.push_back(PP[i].second);
    i++;
  }
  rep(j, SSS.size()) { cout << SSS[SSS.size() - j - 1] << endl; }
  /*
  pair<int,string> p[n];
  rep(i,SS.size()){
      p[i]=pair(SS[i].second,SS[i].first);
      PP.push_back(p[i]);
  }
  PP.sort();
  int k=PP[0].first;
  int i=0;
  while(PP[i].first==k){
      cout<<PP[i].second<<endl;
      i++;
  }
  */
  return 0;
}