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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

string minstr(string a, string b) {
  if (a.length() == 0)
    return b;
  if (b.length() == 0)
    return a;
  if (a.length() != b.length()) {
    return a.length() < b.length() ? a : b;
  }
  return min(a, b);
}

int main() {
  int N;
  while (cin >> N, N) {
    vs S;
    vs in(N);
    REP(i, N) cin >> in[i];
    SORT(in);
    in.erase(unique(ALL(in)), in.end());
    N = SZ(in);
    REP(i, N) {
      bool ok = true;
      REP(j, N) {
        if (i == j)
          continue;
        if (in[j].find(in[i]) != string::npos) {
          ok = false;
          break;
        }
      }
      if (ok)
        S.push_back(in[i]);
    }

    N = SZ(S);
    vector<vs> tail(N, vs(N));
    REP(i, N) REP(j, N) {
      if (i == j)
        continue;
      RREP(k, S[i].length()) {
        if (k < (int)S[j].length()) {
          bool ok = true;
          REP(l, k) {
            if (S[i][S[i].length() - k + l] != S[j][l]) {
              ok = false;
              break;
            }
          }
          if (ok) {
            tail[i][j] = S[j].substr(k);
            break;
          }
        }
      }
    }

    vector<vs> dp(N, vs(1 << N));
    REP(i, N) { dp[i][1 << i] = S[i]; }

    REP(X, 1 << N) {
      REP(i, N) if (BIT(X, i)) {
        REP(j, N) if (!BIT(X, j)) {
          dp[j][X | (1 << j)] =
              minstr(dp[j][X | (1 << j)], dp[i][X] + tail[i][j]);
        }
      }
    }
    string mi;
    REP(i, N) { mi = minstr(mi, dp[i][(1 << N) - 1]); }
    cout << mi << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(a) ((int)(a).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define SQ(x) ((x) * (x))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

string minstr(string a, string b) {
  if (a.length() == 0)
    return b;
  if (b.length() == 0)
    return a;
  if (a.length() != b.length()) {
    return a.length() < b.length() ? a : b;
  }
  return min(a, b);
}

int main() {
  int N;
  while (cin >> N, N) {
    vs S;
    vs in(N);
    REP(i, N) cin >> in[i];
    SORT(in);
    in.erase(unique(ALL(in)), in.end());
    REP(i, N) {
      bool ok = true;
      REP(j, N) {
        if (i == j)
          continue;
        if (in[j].find(in[i]) != string::npos) {
          ok = false;
          break;
        }
      }
      if (ok)
        S.push_back(in[i]);
    }

    int M = SZ(S);
    vector<vs> tail(M, vs(M));
    REP(i, M) REP(j, M) {
      if (i == j)
        continue;
      RREP(k, S[i].length()) {
        if (k < (int)S[j].length()) {
          bool ok = true;
          REP(l, k) {
            if (S[i][S[i].length() - k + l] != S[j][l]) {
              ok = false;
              break;
            }
          }
          if (ok) {
            tail[i][j] = S[j].substr(k);
            break;
          }
        }
      }
    }

    vector<vs> dp(M, vs(1 << M));
    REP(i, M) { dp[i][1 << i] = S[i]; }

    REP(X, 1 << M) {
      REP(i, M) if (BIT(X, i)) {
        REP(j, M) if (!BIT(X, j)) {
          dp[j][X | (1 << j)] =
              minstr(dp[j][X | (1 << j)], dp[i][X] + tail[i][j]);
        }
      }
    }
    string mi;
    REP(i, M) { mi = minstr(mi, dp[i][(1 << M) - 1]); }
    cout << mi << endl;
  }
}