Problem p00321 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
// include
//------------------------------------------
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

bool compss(const string &a, const string &b) {
  int i = 0;
  while (i < a.size() && i < b.size()) {
    if (a[i] < b[i])
      return true;
    if (a[i] > b[i])
      return false;
    i++;
  }
  return a.size() < b.size();
}

typedef pair<string, string> PSS;
typedef vector<PSS> VSS;

bool comp(const PSS &a, const PSS &b) {
  if (compss(a.first, b.first))
    return true;
  if (compss(b.first, a.first))
    return false;
  if (compss(a.second, b.second))
    return true;
  //	if(compss(b.second,a.second))return false;
  return false;
}

int n, f;
int ctn[100][100];
int mp[10];
map<string, int> sref;
map<int, string> revref;
int main() {
  cin >> n >> f;
  int tps = 0;
  REP(i, n) {
    int t;
    cin >> t;
    REP(j, t) {
      char ch[38];
      cin >> ch;
      string s(ch);
      if (sref.find(s) == sref.end()) {
        sref[s] = tps++;
        revref[tps - 1] = s;
      }
      mp[j] = sref[s];
    }
    REP(j, t) {
      FOR(k, j + 1, t) {
        int a = mp[j], b = mp[k];
        if (a > b)
          swap(a, b);
        ctn[a][b]++;
      }
    }
  }

  VSS ps;

  REP(i, tps) {
    FOR(j, i + 1, tps) {
      if (ctn[i][j] >= f) {
        string a = revref[i], b = revref[j];
        if (!compss(a, b))
          swap(a, b);
        ps.push_back(MP(a, b));
      }
    }
  }

  sort(ALL(ps), comp);

  cout << ps.size() << endl;
  EACH(i, ps) { cout << (*i).first + " " + (*i).second << endl; }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
// include
//------------------------------------------
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long ll;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

bool compss(const string &a, const string &b) {
  int i = 0;
  while (i < a.size() && i < b.size()) {
    if (a[i] < b[i])
      return true;
    if (a[i] > b[i])
      return false;
    i++;
  }
  return a.size() < b.size();
}

typedef pair<string, string> PSS;
typedef vector<PSS> VSS;

bool comp(const PSS &a, const PSS &b) {
  if (compss(a.first, b.first))
    return true;
  if (compss(b.first, a.first))
    return false;
  if (compss(a.second, b.second))
    return true;
  //	if(compss(b.second,a.second))return false;
  return false;
}

int n, f;
int ctn[1000][1000];
int mp[10];
map<string, int> sref;
map<int, string> revref;
int main() {
  cin >> n >> f;
  int tps = 0;
  REP(i, n) {
    int t;
    cin >> t;
    REP(j, t) {
      char ch[38];
      cin >> ch;
      string s(ch);
      if (sref.find(s) == sref.end()) {
        sref[s] = tps++;
        revref[tps - 1] = s;
      }
      mp[j] = sref[s];
    }
    REP(j, t) {
      FOR(k, j + 1, t) {
        int a = mp[j], b = mp[k];
        if (a > b)
          swap(a, b);
        ctn[a][b]++;
      }
    }
  }

  VSS ps;

  REP(i, tps) {
    FOR(j, i + 1, tps) {
      if (ctn[i][j] >= f) {
        string a = revref[i], b = revref[j];
        if (!compss(a, b))
          swap(a, b);
        ps.push_back(MP(a, b));
      }
    }
  }

  sort(ALL(ps), comp);

  cout << ps.size() << endl;
  EACH(i, ps) { cout << (*i).first + " " + (*i).second << endl; }

  return 0;
}