Problem p02947 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
//
#include <bits/stdc++.h>
//
using namespace std;
//
#define krep(i, k, n) for (int i = k; i < n; ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define toupp(s) transform(s.begin(), s.end(), s.begin(), ::toupper) // string
#define tolow(s) transform(s.begin(), s.end(), s.begin(), ::tolower) // string
#define pb(v) push_back(v)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define __sort(v) sort(v.begin(), v.end())               // vector
#define __stable_sort(v) stable_sort(v.begin(), v.end()) // vector
#define __exists(c, v) (find(all(c), (v)) != (c).end())  // vector
#define __unique(v)                                                            \
  __sort(v);                                                                   \
  v.erase(unique(v.begin(), v.end()), v.end()) // vector
#define __swap(a, b, tmp)                                                      \
  tmp = a;                                                                     \
  a = b;                                                                       \
  b = tmp;
#define __memset(a) memset(a, 0, sizeof(a));
//
#define dumpVar(v) cerr << setw(30) << #v " = [" << v << "]" << endl
#define dumpArr(v, n)                                                          \
  for (int i = 0; i < n; ++i) {                                                \
    cerr << v[i] << ", ";                                                      \
  }                                                                            \
  cerr << endl
#define dumpPair(p) cerr << "<" << p.first << ", " << p.second << ">" << endl
//
typedef long long ll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef list<int> li_i;
typedef list<double> li_d;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
//

long kaijo(long x);

int main() {
  ios::sync_with_stdio(false);
  //
  /* code start */

  vector<char> _c_arr;
  int n;
  long counter = 0;
  cin >> n;
  vi arr;
  vs s_arr;

  rep(i, n) {
    string str = "";
    rep(j, 10) {
      char c;

      cin >> c;
      if (str == "") {
        str = c;
      } else {
        int flag = 1;
        for (int k = 0; k < str.length(); ++k) {
          if (str[k] > c) {
            string::iterator it = str.begin() + k;
            str.insert(it, c);
            flag = 0;
            break;
          }
        }
        if (flag) {
          str = str + c;
        }
      }

      // dumpVar(str);
    }
    s_arr.pb(str);
  }

  dumpArr(s_arr, n);
  __sort(s_arr);
  dumpArr(s_arr, n);

  for (int i = 0; i < n; ++i) {
    long same_num = 0;
    long skip_num = 0;

    for (int j = i + 1; j < n; ++j) {
      if (i + 1 == j && s_arr[i] == s_arr[j])
        same_num++;
      if (s_arr[i] == s_arr[j]) {
        same_num++;
        skip_num++;
      } else
        break;
    }
    dumpVar(i);
    dumpVar(same_num);
    i += skip_num;
    counter += kaijo(same_num) / kaijo(same_num - 2) / 2;
  }
  cout << counter << endl;

  /* code end   */
  //
  return 0;
}

long kaijo(long x) {
  long tmp = 1;
  krep(i, 1, x + 1) { tmp = tmp * i; }
  return tmp;
}

/*
3
 3 * 2 * 1  / 2
 4 * 3 * 2 * 1 / 2 /2

*/

Fixed submission:
#define _USE_MATH_DEFINES
//
#include <bits/stdc++.h>
//
using namespace std;
//
#define krep(i, k, n) for (int i = k; i < n; ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define toupp(s) transform(s.begin(), s.end(), s.begin(), ::toupper) // string
#define tolow(s) transform(s.begin(), s.end(), s.begin(), ::tolower) // string
#define pb(v) push_back(v)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define __sort(v) sort(v.begin(), v.end())               // vector
#define __stable_sort(v) stable_sort(v.begin(), v.end()) // vector
#define __exists(c, v) (find(all(c), (v)) != (c).end())  // vector
#define __unique(v)                                                            \
  __sort(v);                                                                   \
  v.erase(unique(v.begin(), v.end()), v.end()) // vector
#define __swap(a, b, tmp)                                                      \
  tmp = a;                                                                     \
  a = b;                                                                       \
  b = tmp;
#define __memset(a) memset(a, 0, sizeof(a));
//
#define dumpVar(v) cerr << setw(30) << #v " = [" << v << "]" << endl
#define dumpArr(v, n)                                                          \
  for (int i = 0; i < n; ++i) {                                                \
    cerr << v[i] << ", ";                                                      \
  }                                                                            \
  cerr << endl
#define dumpPair(p) cerr << "<" << p.first << ", " << p.second << ">" << endl
//
typedef long long ll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef list<int> li_i;
typedef list<double> li_d;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
//

long kaijo(long x);

int main() {
  ios::sync_with_stdio(false);
  //
  /* code start */

  vector<char> _c_arr;
  int n;
  long counter = 0;
  cin >> n;
  vi arr;
  vs s_arr;

  rep(i, n) {
    string str = "";
    rep(j, 10) {
      char c;

      cin >> c;
      if (str == "") {
        str = c;
      } else {
        int flag = 1;
        for (int k = 0; k < str.length(); ++k) {
          if (str[k] > c) {
            string::iterator it = str.begin() + k;
            str.insert(it, c);
            flag = 0;
            break;
          }
        }
        if (flag) {
          str = str + c;
        }
      }

      // dumpVar(str);
    }
    s_arr.pb(str);
  }

  dumpArr(s_arr, n);
  __sort(s_arr);
  dumpArr(s_arr, n);

  for (int i = 0; i < n; ++i) {
    long same_num = 0;
    long skip_num = 0;

    for (int j = i + 1; j < n; ++j) {
      if (i + 1 == j && s_arr[i] == s_arr[j])
        same_num++;
      if (s_arr[i] == s_arr[j]) {
        same_num++;
        skip_num++;
      } else
        break;
    }
    dumpVar(i);
    dumpVar(same_num);
    i += skip_num;
    counter += same_num * (same_num - 1) / 2;
  }
  cout << counter << endl;

  /* code end   */
  //
  return 0;
}

long kaijo(long x) {
  long tmp = 1;
  krep(i, 1, x + 1) { tmp = tmp * i; }
  return tmp;
}

/*
3
 3 * 2 * 1  / 2
 4 * 3 * 2 * 1 / 2 /2

*/