Problem p01147 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(a) (a).begin(), (a).end()
const int INF = INT_MAX / 3;
const double EPS = 1e-14;

template <typename U, typename T> void chmin(U &x, T y) { x = min(x, y); }

template <typename U, typename T> void chmax(U &x, T y) { x = max(x, y); }

typedef long long int lli;
typedef long double ld;

const int NMAX = 10;
string coststr[NMAX][NMAX];
pair<int, string> dp[1 << NMAX][NMAX];
int N;

string strcost(string src, string dst) {
  int upb = min(src.length(), dst.length());
  int i = upb;
  while (i >= 0 and src.substr(src.length() - i) != dst.substr(0, i)) {
    --i;
  }
  return dst.substr(i);
}

pair<int, string> solve(int S, int i) {
  if ((S | (1 << i)) == (1 << N) - 1)
    return make_pair(0, "");
  if (dp[S][i].first < INF)
    return dp[S][i];
  pair<int, string> ans = make_pair(INF, "~");
  REP(j, N) {
    if ((S >> j) & 1 or i == j)
      continue;
    chmin(ans, [i, j](pair<int, string> p) {
      p.first += coststr[i][j].length();
      p.second = coststr[i][j] + p.second;
      return p;
    }(solve(S | (1 << i), j)));
  }
  dp[S][i] = ans;
  return dp[S][i];
}

int includeder(string query, vector<string> ss) {
  int i = 0;
  for (string s : ss) {
    if (s.find(query) != string::npos) {
      ++i;
    }
  }
  return i;
}

int main() {
  string s, d;
  while (cin >> N and N) {
    REP(i, 1 << NMAX) REP(j, NMAX) { dp[i][j] = make_pair(INF, ""); }
    vector<string> queries, s_;
    REP(i, N) {
      string s;
      cin >> s;
      if (find(ALL(queries), s) == queries.end()) {
        queries.push_back(s);
      }
    }
    for (string q : queries) {
      if (includeder(q, queries) == 1) {
        s_.push_back(q);
      }
    }
    N = s_.size();
    REP(i, N) REP(j, N) { coststr[i][j] = strcost(s_[i], s_[j]); }
    pair<int, string> ans = make_pair(INF, "");
    REP(i, N) {
      chmin(ans, [](pair<int, string> c, string s) {
        c.first += s.length();
        c.second = s + c.second;
        return c;
      }(solve(0, i), s_[i]));
    }
    cout << ans.second << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(a) (a).begin(), (a).end()
const int INF = INT_MAX / 3;
const double EPS = 1e-14;

template <typename U, typename T> void chmin(U &x, T y) { x = min(x, y); }

template <typename U, typename T> void chmax(U &x, T y) { x = max(x, y); }

typedef long long int lli;
typedef long double ld;

const int NMAX = 10;
string coststr[NMAX][NMAX];
pair<int, string> dp[1 << NMAX][NMAX];
int N;

string strcost(string src, string dst) {
  int upb = min(src.length(), dst.length());
  int i = upb;
  while (i >= 0 and src.substr(src.length() - i) != dst.substr(0, i)) {
    --i;
  }
  return dst.substr(i);
}

pair<int, string> solve(int S, int i) {
  if ((S | (1 << i)) == (1 << N) - 1)
    return make_pair(0, "");
  if (dp[S][i].first < INF)
    return dp[S][i];
  pair<int, string> ans = make_pair(INF, "~");
  REP(j, N) {
    if ((S >> j) & 1 or i == j)
      continue;
    chmin(ans, [i, j](pair<int, string> p) {
      p.first += coststr[i][j].length();
      p.second = coststr[i][j] + p.second;
      return p;
    }(solve(S | (1 << i), j)));
  }
  dp[S][i] = ans;
  return dp[S][i];
}

int includeder(string query, vector<string> ss) {
  int i = 0;
  for (string s : ss) {
    if (s.find(query) != string::npos) {
      ++i;
    }
  }
  return i;
}

int main() {
  string s, d;
  while (cin >> N and N) {
    REP(i, 1 << NMAX) REP(j, NMAX) { dp[i][j] = make_pair(INF, ""); }
    vector<string> queries(N), s_;
    REP(i, N) { cin >> queries[i]; }
    for (string q : queries) {
      if (includeder(q, queries) == 1) {
        s_.push_back(q);
      }
    }
    REP(i, N) REP(j, N) { coststr[i][j] = strcost(s_[i], s_[j]); }
    pair<int, string> ans = make_pair(INF, "");
    REP(i, N) {
      chmin(ans, [](pair<int, string> c, string s) {
        c.first += s.length();
        c.second = s + c.second;
        return c;
      }(solve(0, i), s_[i]));
    }
    cout << ans.second << endl;
  }
  return 0;
}