Problem p00899 (C++)

Buggy submission (status: Runtime Error):
////////////////////////////////////////
///  tu3 pro-con template            ///
////////////////////////////////////////
#include "bits/stdc++.h"
using namespace std;

// -- loop macros -- //
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (int)(s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define FOREACH(i, container) for (auto &&i : container)
#define allof(c) c.begin(), c.end()
#define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n)

// -- functors -- //
#define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; }
#define COMPARISON(t, a, b, exp)                                               \
  [&](const t &a, const t &b) -> bool { return exp; }

#define PRED(a, exp) [&](const auto &a) -> bool { return exp; }
#define COMP(a, b, exp)                                                        \
  [&](const auto &a, const auto &b) -> bool { return exp; }
#define CONV1(a, exp)                                                          \
  [&](const auto &a) -> auto{ return exp; }
#define CONV2(a, b, exp)                                                       \
  [&](const auto &a, const auto &b) -> auto{ return exp; }
#define CONV3(a, b, c, exp)                                                    \
  [&](const auto &a, const auto &b, const auto &c) -> auto{ return exp; }

// -- typedefs -- //
#define EPS 1e-9

typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;

// -- I/O Helper -- //
struct _Reader {
  _Reader(istream &cin) : cin(cin) {}
  istream &cin;
  template <class T> _Reader operator,(T &rhs) {
    cin >> rhs;
    return *this;
  }
};
struct _Writer {
  _Writer(ostream &cout) : cout(cout) {}
  ostream &cout;
  bool f{false};
  template <class T> _Writer operator,(const T &rhs) {
    cout << (f ? " " : "") << rhs;
    f = true;
    return *this;
  }
};
#define READ(t, ...)                                                           \
  t __VA_ARGS__;                                                               \
  (_Reader{cin}), __VA_ARGS__
#define WRITE(...)                                                             \
  (_Writer{cout}), __VA_ARGS__;                                                \
  cout << '\n'
#define DEBUG(...)                                                             \
  (_Writer{cerr}), __VA_ARGS__;                                                \
  cerr << '\n'

// -- vevector -- //
template <class T> struct vevector : public vector<vector<T>> {
  vevector(size_t n = 0, size_t m = 0, const T &initial = T())
      : vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : public vector<vevector<T>> {
  vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T())
      : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : public vector<vevevector<T>> {
  vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0,
               const T &initial = T())
      : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};

namespace std {
template <class T1, class T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << p.first << " " << p.second;
  return out;
}
} // namespace std

template <class T> T read() {
  T t;
  cin >> t;
  return t;
}
template <class T> vector<T> read(int n) {
  vector<T> v;
  REP(i, n) { v.push_back(read<T>()); }
  return v;
}
template <class T> vevector<T> read(int n, int m) {
  vevector<T> v;
  REP(i, n) v.push_back(read<T>(m));
  return v;
}
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
  vevector<T> v;
  REP(i, n) v.push_back(readjag<T>());
  return v;
}

template <class T> struct iter_pair_t {
  T beg, end;
};
template <class T> iter_pair_t<T> iter_pair(T beg, T end) {
  return iter_pair_t<T>{beg, end};
}
template <class T> ostream &operator<<(ostream &out, iter_pair_t<T> v) {
  if (v.beg != v.end) {
    out << *v.beg++;
    while (v.beg != v.end) {
      out << " " << *v.beg++;
    }
  }
  return out;
}
template <class T1> ostream &operator<<(ostream &out, const vector<T1> &v) {
  return out << iter_pair(begin(v), end(v));
}

// -- etc -- //
template <class T> T infinity_value();
#define DEFINE_INFINITY_VALUE(T, val)                                          \
  template <> constexpr T infinity_value<T>() { return (val); }
DEFINE_INFINITY_VALUE(int, 1 << 28);
DEFINE_INFINITY_VALUE(uint, 1u << 28);
DEFINE_INFINITY_VALUE(llong, 1ll << 60);
DEFINE_INFINITY_VALUE(ullong, 1ull << 60);
DEFINE_INFINITY_VALUE(double, HUGE_VAL);
DEFINE_INFINITY_VALUE(float, HUGE_VAL);
#define INF(T) infinity_value<T>()

inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
template <class TInt> bool in_range(TInt val, TInt min, TInt max) {
  return val >= min && val < max;
}
template <> bool in_range<double>(double val, double min, double max) {
  return val - min > -EPS && val - max < EPS;
}
template <> bool in_range<float>(float val, float min, float max) {
  return val - min > -EPS && val - max < EPS;
}
template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) {
  return x >= 0 && x < w && y >= 0 && y < h;
}
vector<int> iotavn(int start, int count) {
  vector<int> r(count);
  iota(allof(r), start);
  return r;
}

//// start up ////
void solve();
int main() {
  //// for local debugging
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

  // auto classic_table = ctype<char>::classic_table();
  // vector<ctype<char>::mask> ctable(classic_table, classic_table +
  // ctype<char>::table_size); ctable[':'] |= ctype_base::space; // as delimitor
  // ctable['/'] |= ctype_base::space; // as delimitor
  // cin.imbue(locale(cin.getloc(), new ctype<char>(ctable.data())));

  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(std::numeric_limits<double>::max_digits10);
  solve();

  return 0;
}

// compare
template <class InIt1, class InIt2>
int partial_compare(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2) {
  return lexicographical_compare(first1, last1, first2, last2)   ? -1
         : lexicographical_compare(first2, last2, first1, last1) ? 1
                                                                 : 0;
}

// uint bit permutation
struct BitPermutation {
#ifdef _MSC_VER
  static uint __builtin_ctz(unsigned int x) {
    unsigned long r;
    _BitScanForward(&r, x);
    return r;
  }
#endif
  using TVal = uint;
  static int bsf(uint x) { return x ? __builtin_ctz(x) : -1; }

  static TVal next_bit_permutation(TVal x) {
    TVal t = x | (x - 1);
    TVal p = (~t & -~t) - 1;
    return (t + 1) | (p >> (bsf(x) + 1));
  }

  struct it {
    TVal val;
    TVal operator*() const { return val; }
    it &operator++() {
      val = next_bit_permutation(val);
      return *this;
    }
    bool operator!=(it o) const { return val != o.val; }
  };

  it s, e;
  BitPermutation(int n, int k)
      : s{(TVal(1) << k) - 1}, e{k > 0
                                     ? TVal(1) << n | ((TVal(1) << (k - 1)) - 1)
                                     : next_bit_permutation(0)} {
    assert(n >= k);
  }
  it begin() const { return s; }
  it end() const { return e; }
};

// ヘルドカープ (改) // TSP (巡回セールスマン問題)など解ける
// O(N^2 * 2^N) (N<=16くらいまで: N=15で700万 N=16で1677万 N=17で3800万)
// TDistance g: g(i,j, n) で 状態nのときの i -> j へのコストを返すファンクタ
// (0<=i,j<N)
using TCost = int;
template <class TDistance>
TCost HeldKarp(int N, TDistance g, int start = 0,
               bool needToReturnStart = false) {
  vevector<TCost> dp(1 << N, N, INF(TCost));
  dp[1 << start][start] = 0;

  FOR(i, 1, N) {
    for (int n : BitPermutation(N, i)) {
      REP(from, N) {
        if (dp[n][from] >= INF(TCost)) {
          continue;
        }
        REP(to, N) {
          if (n & 1 << to) {
            continue;
          }
          int next = n | 1 << to;
          TCost dist = dp[n][from] + g(from, to, n);
          if (dist < dp[next][to]) {
            dp[next][to] = dist;
          }
        }
      }
    }
  }

  TCost ans = INF(TCost);
  REP(i, N) {
    ans = min(ans, dp[(1 << N) - 1][i] +
                       (needToReturnStart ? g(i, start, (1 << N) - 1) : 0));
  }
  return ans;
}
////////////////////
/// template end ///
////////////////////

int getCommonLen(const string &a, string &b) {
  int N = min(a.size(), b.size());
  int maxMatch = 0;
  REP(i, N + 1) {
    if (partial_compare(partof(a, a.size() - i, i), partof(b, 0, i)) == 0) {
      maxMatch = i;
    }
  }
  return maxMatch;
}

void solve() {
  while (true) {
    READ(int, N);
    if (!N) {
      break;
    }
    auto S = read<string>(N);

    auto X = vector<string>();
    REP(i, N) {
      bool orig = true;
      REP(j, N) {
        if (i != j) {
          if (S[j].find(S[i]) != string::npos) {
            orig = false;
            break;
          }
        }
      }
      if (orig) {
        X.push_back(S[i]);
      }
    }
    S.swap(X);
    N = S.size();

    vevector<int> g(N + 1, N + 1, INF(int));
    REP(i, N) REP(j, N) g[i][j] = S[j].size() - getCommonLen(S[i], S[j]);
    REP(i, N) g[N][i] = S[i].size();
    int minLen = HeldKarp(
        N + 1, [&g](auto i, auto j, auto n) { return g[i][j]; }, N, false);

    WRITE(minLen);
  }
}


Fixed submission:
////////////////////////////////////////
///  tu3 pro-con template            ///
////////////////////////////////////////
#include "bits/stdc++.h"
using namespace std;

// -- loop macros -- //
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (int)(s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define FOREACH(i, container) for (auto &&i : container)
#define allof(c) c.begin(), c.end()
#define partof(c, i, n) c.begin() + (i), c.begin() + (i) + (n)

// -- functors -- //
#define PREDICATE(t, a, exp) [&](const t &a) -> bool { return exp; }
#define COMPARISON(t, a, b, exp)                                               \
  [&](const t &a, const t &b) -> bool { return exp; }

#define PRED(a, exp) [&](const auto &a) -> bool { return exp; }
#define COMP(a, b, exp)                                                        \
  [&](const auto &a, const auto &b) -> bool { return exp; }
#define CONV1(a, exp)                                                          \
  [&](const auto &a) -> auto{ return exp; }
#define CONV2(a, b, exp)                                                       \
  [&](const auto &a, const auto &b) -> auto{ return exp; }
#define CONV3(a, b, c, exp)                                                    \
  [&](const auto &a, const auto &b, const auto &c) -> auto{ return exp; }

// -- typedefs -- //
#define EPS 1e-9

typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;

// -- I/O Helper -- //
struct _Reader {
  _Reader(istream &cin) : cin(cin) {}
  istream &cin;
  template <class T> _Reader operator,(T &rhs) {
    cin >> rhs;
    return *this;
  }
};
struct _Writer {
  _Writer(ostream &cout) : cout(cout) {}
  ostream &cout;
  bool f{false};
  template <class T> _Writer operator,(const T &rhs) {
    cout << (f ? " " : "") << rhs;
    f = true;
    return *this;
  }
};
#define READ(t, ...)                                                           \
  t __VA_ARGS__;                                                               \
  (_Reader{cin}), __VA_ARGS__
#define WRITE(...)                                                             \
  (_Writer{cout}), __VA_ARGS__;                                                \
  cout << '\n'
#define DEBUG(...)                                                             \
  (_Writer{cerr}), __VA_ARGS__;                                                \
  cerr << '\n'

// -- vevector -- //
template <class T> struct vevector : public vector<vector<T>> {
  vevector(size_t n = 0, size_t m = 0, const T &initial = T())
      : vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : public vector<vevector<T>> {
  vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T())
      : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : public vector<vevevector<T>> {
  vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0,
               const T &initial = T())
      : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};

namespace std {
template <class T1, class T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << p.first << " " << p.second;
  return out;
}
} // namespace std

template <class T> T read() {
  T t;
  cin >> t;
  return t;
}
template <class T> vector<T> read(int n) {
  vector<T> v;
  REP(i, n) { v.push_back(read<T>()); }
  return v;
}
template <class T> vevector<T> read(int n, int m) {
  vevector<T> v;
  REP(i, n) v.push_back(read<T>(m));
  return v;
}
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
  vevector<T> v;
  REP(i, n) v.push_back(readjag<T>());
  return v;
}

template <class T> struct iter_pair_t {
  T beg, end;
};
template <class T> iter_pair_t<T> iter_pair(T beg, T end) {
  return iter_pair_t<T>{beg, end};
}
template <class T> ostream &operator<<(ostream &out, iter_pair_t<T> v) {
  if (v.beg != v.end) {
    out << *v.beg++;
    while (v.beg != v.end) {
      out << " " << *v.beg++;
    }
  }
  return out;
}
template <class T1> ostream &operator<<(ostream &out, const vector<T1> &v) {
  return out << iter_pair(begin(v), end(v));
}

// -- etc -- //
template <class T> T infinity_value();
#define DEFINE_INFINITY_VALUE(T, val)                                          \
  template <> constexpr T infinity_value<T>() { return (val); }
DEFINE_INFINITY_VALUE(int, 1 << 28);
DEFINE_INFINITY_VALUE(uint, 1u << 28);
DEFINE_INFINITY_VALUE(llong, 1ll << 60);
DEFINE_INFINITY_VALUE(ullong, 1ull << 60);
DEFINE_INFINITY_VALUE(double, HUGE_VAL);
DEFINE_INFINITY_VALUE(float, HUGE_VAL);
#define INF(T) infinity_value<T>()

inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
template <class TInt> bool in_range(TInt val, TInt min, TInt max) {
  return val >= min && val < max;
}
template <> bool in_range<double>(double val, double min, double max) {
  return val - min > -EPS && val - max < EPS;
}
template <> bool in_range<float>(float val, float min, float max) {
  return val - min > -EPS && val - max < EPS;
}
template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) {
  return x >= 0 && x < w && y >= 0 && y < h;
}
vector<int> iotavn(int start, int count) {
  vector<int> r(count);
  iota(allof(r), start);
  return r;
}

//// start up ////
void solve();
int main() {
  //// for local debugging
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  // auto classic_table = ctype<char>::classic_table();
  // vector<ctype<char>::mask> ctable(classic_table, classic_table +
  // ctype<char>::table_size); ctable[':'] |= ctype_base::space; // as delimitor
  // ctable['/'] |= ctype_base::space; // as delimitor
  // cin.imbue(locale(cin.getloc(), new ctype<char>(ctable.data())));

  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(std::numeric_limits<double>::max_digits10);
  solve();

  return 0;
}

// compare
template <class InIt1, class InIt2>
int partial_compare(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2) {
  return lexicographical_compare(first1, last1, first2, last2)   ? -1
         : lexicographical_compare(first2, last2, first1, last1) ? 1
                                                                 : 0;
}

// uint bit permutation
struct BitPermutation {
#ifdef _MSC_VER
  static uint __builtin_ctz(unsigned int x) {
    unsigned long r;
    _BitScanForward(&r, x);
    return r;
  }
#endif
  using TVal = uint;
  static int bsf(uint x) { return x ? __builtin_ctz(x) : -1; }

  static TVal next_bit_permutation(TVal x) {
    TVal t = x | (x - 1);
    TVal p = (~t & -~t) - 1;
    return (t + 1) | (p >> (bsf(x) + 1));
  }

  struct it {
    TVal val;
    TVal operator*() const { return val; }
    it &operator++() {
      val = next_bit_permutation(val);
      return *this;
    }
    bool operator!=(it o) const { return val != o.val; }
  };

  it s, e;
  BitPermutation(int n, int k)
      : s{(TVal(1) << k) - 1}, e{k > 0
                                     ? TVal(1) << n | ((TVal(1) << (k - 1)) - 1)
                                     : next_bit_permutation(0)} {
    assert(n >= k);
  }
  it begin() const { return s; }
  it end() const { return e; }
};

// ヘルドカープ (改) // TSP (巡回セールスマン問題)など解ける
// O(N^2 * 2^N) (N<=16くらいまで: N=15で700万 N=16で1677万 N=17で3800万)
// TDistance g: g(i,j, n) で 状態nのときの i -> j へのコストを返すファンクタ
// (0<=i,j<N)
using TCost = int;
template <class TDistance>
TCost HeldKarp(int N, TDistance g, int start = 0,
               bool needToReturnStart = false) {
  vevector<TCost> dp(1 << N, N, INF(TCost));
  dp[1 << start][start] = 0;

  FOR(i, 1, N) {
    for (int n : BitPermutation(N, i)) {
      REP(from, N) {
        if (dp[n][from] >= INF(TCost)) {
          continue;
        }
        REP(to, N) {
          if (n & 1 << to) {
            continue;
          }
          int next = n | 1 << to;
          TCost dist = dp[n][from] + g(from, to, n);
          if (dist < dp[next][to]) {
            dp[next][to] = dist;
          }
        }
      }
    }
  }

  TCost ans = INF(TCost);
  REP(i, N) {
    ans = min(ans, dp[(1 << N) - 1][i] +
                       (needToReturnStart ? g(i, start, (1 << N) - 1) : 0));
  }
  return ans;
}
////////////////////
/// template end ///
////////////////////

int getCommonLen(const string &a, string &b) {
  int N = min(a.size(), b.size());
  int maxMatch = 0;
  REP(i, N + 1) {
    if (partial_compare(partof(a, a.size() - i, i), partof(b, 0, i)) == 0) {
      maxMatch = i;
    }
  }
  return maxMatch;
}

void solve() {
  while (true) {
    READ(int, N);
    if (!N) {
      break;
    }
    auto S = read<string>(N);

    auto X = vector<string>();
    REP(i, N) {
      bool orig = true;
      REP(j, N) {
        if (i != j) {
          if (S[j].find(S[i]) != string::npos) {
            orig = false;
            break;
          }
        }
      }
      if (orig) {
        X.push_back(S[i]);
      }
    }
    S.swap(X);
    N = S.size();

    vevector<int> g(N + 1, N + 1, INF(int));
    REP(i, N) REP(j, N) g[i][j] = S[j].size() - getCommonLen(S[i], S[j]);
    REP(i, N) g[N][i] = S[i].size();
    int minLen = HeldKarp(
        N + 1, [&g](auto i, auto j, auto n) { return g[i][j]; }, N, false);

    WRITE(minLen);
  }
}