Problem p03408 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define FOR(e, c) for (auto &e : c)
#define SORT(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define rvisort(v) sort(v.begin(), v.end(), greater<int>());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout << d << endl;
#define coutd(d) cout << std::setprecision(10) << d << endl;
#define cinline(n) getline(cin, n);
#define replace_all(s, b, a) replace(s.begin(), s.end(), b, a);

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vs = vector<string>;
using vd = vector<double>;
using ul = unsigned long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vs s(n);
  rep(i, n) { cin >> s[i]; }
  int m;
  cin >> m;
  vs t(m);
  rep(i, m) cin >> t[i];

  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  int b = 0;
  int ans = 0;
  while (b < n) {
    int tmp = 1;
    while (b < n) {
      if (s[b] == s[b + 1]) {
        b++;
        tmp++;
      } else {
        break;
      }
    }
    rep(i, m) {
      if (s[b] == t[i])
        tmp--;
    }
    if (ans < tmp)
      ans = tmp;
    b++;
  }
  cout(ans);
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define FOR(e, c) for (auto &e : c)
#define SORT(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define rvisort(v) sort(v.begin(), v.end(), greater<int>());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout << d << endl;
#define coutd(d) cout << std::setprecision(10) << d << endl;
#define cinline(n) getline(cin, n);
#define replace_all(s, b, a) replace(s.begin(), s.end(), b, a);

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vs = vector<string>;
using vd = vector<double>;
using ul = unsigned long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vs s(n);
  rep(i, n) { cin >> s[i]; }
  int m;
  cin >> m;
  vs t(m);
  rep(i, m) cin >> t[i];

  sort(s.begin(), s.end());
  sort(t.begin(), t.end());

  int b = 0;
  int ans = 0;
  while (b < n) {
    int tmp = 1;
    while (b + 1 < n) {
      if (s[b] == s[b + 1]) {
        b++;
        tmp++;
      } else {
        break;
      }
    }
    rep(i, m) {
      if (s[b] == t[i])
        tmp--;
    }
    if (ans < tmp)
      ans = tmp;
    b++;
  }
  cout(ans);
  return 0;
}