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

///////////////////////////////////////////////////
/* __                                       __   */
/*|  |--.-----.---.-.----.----.-----.-----.|  |_ */
/*|  _  |  -__|  _  |  __|  __|  -__|  _  ||   _|*/
/*|_____|_____|___._|____|____|_____|   __||____|*/
/*                                  |__|         */
///////////////////////////////////////////////////
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long lint;
using namespace std;
#define rep(counter, limit) for (int counter = 0; counter < (limit); counter++)
#define INF 1000000000000
#define MOD 10000007
int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
bool primenum(int a) {
  if (a == 1) {
    return false;
  }
  if (a == 2) {
    return true;
  }
  if (a % 2 == 0) {
    return false;
  }
  for (int i = 3; i < sqrt(a) + 1; i += 2) {
    if (a != i) {
      if (a % i == 0) {
        return false;
      }
    }
  }
  return true;
}
struct setupios {
  setupios() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} setupios;
int main() {

  int n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  sort(s.begin(), s.end());
  int ans = 1;
  int size = s.size() - 1;
  rep(i, size) {
    if (s[i] != s[i + 1])
      ans++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

///////////////////////////////////////////////////
/* __                                       __   */
/*|  |--.-----.---.-.----.----.-----.-----.|  |_ */
/*|  _  |  -__|  _  |  __|  __|  -__|  _  ||   _|*/
/*|_____|_____|___._|____|____|_____|   __||____|*/
/*                                  |__|         */
///////////////////////////////////////////////////
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
typedef long long lint;
using namespace std;
#define rep(counter, limit) for (int counter = 0; counter < (limit); counter++)
#define INF 1000000000000
#define MOD 10000007
int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
int lcm(int a, int b) { return a * b / gcd(a, b); }
bool primenum(int a) {
  if (a == 1) {
    return false;
  }
  if (a == 2) {
    return true;
  }
  if (a % 2 == 0) {
    return false;
  }
  for (int i = 3; i < sqrt(a) + 1; i += 2) {
    if (a != i) {
      if (a % i == 0) {
        return false;
      }
    }
  }
  return true;
}
struct setupios {
  setupios() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} setupios;
int main() {

  int n;
  cin >> n;
  vector<string> s;
  string w = "";
  rep(i, n) {
    cin >> w;
    if (!binary_search(s.begin(), s.end(), w)) {
      s.push_back(w);
      sort(s.begin(), s.end());
    }
  }
  cout << s.size() << endl;
}