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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// 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)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 1e8;

string f(string s, string t) {
  for (int i = min(SZ(t), SZ(s)); i >= 1; --i) {
    if (t.substr(0, i) == s.substr(SZ(s) - i, i)) {
      return s + t.substr(i);
    }
  }
  return s + t;
}

string le(string &s, string &t) {
  if (SZ(s) < SZ(t))
    return s;
  else if (SZ(s) > SZ(t))
    return t;
  return min(s, t);
}

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

  int N;
  while (cin >> N, N) {
    VS vs(N);
    REP(i, N) cin >> vs[i];
    SORT(vs);
    vs.erase(unique(ALL(vs)), vs.end());
    N = SZ(vs);
    vector<bool> use(N, true);
    REP(i, SZ(vs)){REP(j, SZ(vs)){if (i == j) continue;
    if (vs[i].find(vs[j]) != string::npos)
      use[j] = false;
    string cat = f(vs[i], vs[j]);
    if (cat == vs[i])
      use[j] = false;
    if (cat == vs[j])
      use[i] = false;
  }
}
{
  VS vs_;
  REP(i, N) if (use[i]) vs_.PB(vs[i]);
  vs = vs_;
}

N = SZ(vs);
string sum[1 << 10][11];
REP(i, N) sum[1 << i][i] = vs[i];

REP(b, 1 << N) REP(i, N) {
  if (!((b >> i) & 1))
    continue;
  for (int j = 0; j < N; ++j) {
    if ((b >> j) & 1)
      continue;
    string &s = sum[b | (1 << j)][j];
    string cat = f(sum[b][i], vs[j]);
    if (s != "")
      s = le(s, cat);
    else
      s = cat;
  }
}

string res(1000, 127);
REP(i, N)
res = le(res, sum[(1 << N) - 1][i]);

cout << res << endl;
}

return 0;
}

Error: 0

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// 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)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 1e8;

int f(string &s, string &t) {
  for (int i = min(SZ(t), SZ(s)); i >= 1; --i) {
    if (t.substr(0, i) == s.substr(SZ(s) - i, i)) {
      return i;
    }
  }
  return 0;
}

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

  int N;
  while (cin >> N, N) {
    VS vs(N);
    REP(i, N) cin >> vs[i];
    REP(i, SZ(vs)) REP(j, SZ(vs)) {
      if (i == j)
        continue;
      int r = f(vs[i], vs[j]);
      if (r == SZ(vs[j]))
        vs.erase(vs.begin() + j);
      else if (r == SZ(vs[i]))
        vs.erase(vs.begin() + i);
    }

    N = SZ(vs);
    int G[10][10];
    REP(i, N) REP(j, N) { G[i][j] = f(vs[i], vs[j]); }

    int dp[10][1 << 10];
    string sum[10][1 << 10];
    fill((int *)dp, (int *)dp + 10 * (1 << 10), -1);
    REP(i, N) dp[i][1 << i] = 0, sum[i][1 << i] = vs[i];
    for (int i = 0; i < N; ++i) {
      for (int b = 1; b < (1 << N); ++b) {
        if (dp[i][b] < 0)
          continue;
        for (int j = 0; j < N; ++j) {
          if ((b >> j) & 1)
            continue;
          int &nxt = dp[j][b | (1 << j)];
          string &s = sum[j][b | (1 << j)];
          if (nxt < dp[i][b] + G[i][j]) {
            nxt = dp[i][b] + G[i][j];
            s = sum[i][b];
            int r = f(s, vs[j]);
            s.erase(s.end() - r, s.end());
            s += vs[j];
          } else if (nxt == dp[i][b] + G[i][j]) {
            string t = sum[i][b];
            int r = f(t, vs[j]);
            t.erase(t.end() - r, t.end());
            t += vs[j];
            s = min(s, t);
          }
        }
      }
    }

    string res(1000, 'z');
    REP(i, N) {
      if (SZ(sum[i][(1 << N) - 1]) < SZ(res))
        res = sum[i][(1 << N) - 1];
      else if (SZ(sum[i][(1 << N) - 1]) == SZ(res))
        res = min(res, sum[i][(1 << N) - 1]);
    }

    cout << res << endl;
  }

  return 0;
}