Problem p01147 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <utility>
// #include <memory>
// #include <functional>
// #include <deque>
// #include <cctype>
// #include <ctime>
// #include <numeric>
// #include <list>
// #include <iomanip>

// #if __cplusplus >= 201103L
// #include <array>
// #include <tuple>
// #include <initializer_list>
// #include <forward_list>
//
// #define cauto const auto&
// #else

// #endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif
#define LDcout(x, n) fixed << setprecision(n) << x

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
ll myhash(pair<string, int> a) {
  ll ret = 0;
  ll t = 1145148931919LL;
  rep(i, a.F.size()) {
    ret *= t;
    ret += a.F[i];
    ret %= MOD;
  }
  ret *= t;
  ret += a.S;
  return ret;
}

unordered_map<ll, string> ma;
// map<ll,string> ma2;
int n;
string ans;
vector<string> v;
vint used;
bool check(string a, string b) {
  int bb = b.size();
  int aa = a.size();
  rep(i, bb - aa + 1) {
    if (a == b.substr(i, aa))
      return true;
  }
  return false;
}
string overlap(string a, int b) {
  ll t = myhash(mkp(a, b));
  if (ma.count(t)) {
    // cout<<"aa"<<endl;
    return ma[t];
  }
  for (int i = v[b].size(); i >= 0; i--) {
    if (a.size() < i)
      continue;
    if (a.substr(a.size() - i) == v[b].substr(0, i)) {
      return ma[t] = v[b].substr(i);
    }
  }
  cout << "error " << a << " " << b << endl;
  return "";
}

void dfs(int state, string a) {
  // if(used[state]) return;
  // used[state]=1;
  if (state == ((1 << n) - 1)) {
    if (ans.size() > a.size())
      ans = a;
    else if (ans.size() == a.size())
      ans = min(ans, a);
    return;
  }
  rep(i, n) {
    if ((1 << i) & state)
      continue;
    if (a.size() < v[i].size())
      dfs(state + (1 << i), a + overlap(a, i));
    else
      dfs(state + (1 << i), a + overlap(a.substr(a.size() - v[i].size()), i));
  }
}

void mainmain() {
  // int n;
  while (cin >> n, n) {
    // cout<<n<<endl;
    ma.clear();
    v = vector<string>(n);
    rep(i, n) cin >> v[i];
    sort(ALL(v));
    v.erase(unique(ALL(v)), v.end());
    rep(i, v.size()) {
      bool f = false;
      rep(j, v.size()) {
        if (i == j)
          continue;
        if (check(v[i], v[j]))
          f = true;
      }
      if (f) {
        v.erase(v.begin() + i);
        i--;
      }
    }
    n = v.size();
    // used=vint(1<<n,0);
    ans = string(1000, '}');
    dfs(0, "");
    cout << ans << endl;
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  mainmain();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <utility>
// #include <memory>
// #include <functional>
// #include <deque>
// #include <cctype>
// #include <ctime>
// #include <numeric>
// #include <list>
// #include <iomanip>

// #if __cplusplus >= 201103L
// #include <array>
// #include <tuple>
// #include <initializer_list>
// #include <forward_list>
//
// #define cauto const auto&
// #else

// #endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif
#define LDcout(x, n) fixed << setprecision(n) << x

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
ll myhash(pair<string, int> a) {
  ll ret = 0;
  ll t = 1145148931919LL;
  rep(i, a.F.size()) {
    ret *= t;
    ret += a.F[i];
    ret %= MOD;
  }
  ret *= t;
  ret += a.S;
  return ret;
}

unordered_map<ll, string> ma;
// map<ll,string> ma2;
int n;
string ans;
vector<string> v;
vint used;
bool check(string a, string b) {
  int bb = b.size();
  int aa = a.size();
  rep(i, bb - aa + 1) {
    if (a == b.substr(i, aa))
      return true;
  }
  return false;
}
string overlap(string a, int b) {
  // ll t=myhash(mkp(a,b));
  // if(ma.count(t)){
  // cout<<"aa"<<endl;
  // return ma[t];
  // }
  for (int i = v[b].size(); i >= 0; i--) {
    if (a.size() < i)
      continue;
    if (a.substr(a.size() - i) == v[b].substr(0, i)) {
      return v[b].substr(i);
    }
  }
  cout << "error " << a << " " << b << endl;
  return "";
}

void dfs(int state, string a) {
  // if(used[state]) return;
  // used[state]=1;
  if (state == ((1 << n) - 1)) {
    if (ans.size() > a.size())
      ans = a;
    else if (ans.size() == a.size())
      ans = min(ans, a);
    return;
  }
  rep(i, n) {
    if ((1 << i) & state)
      continue;
    if (a.size() < v[i].size())
      dfs(state + (1 << i), a + overlap(a, i));
    else
      dfs(state + (1 << i), a + overlap(a.substr(a.size() - v[i].size()), i));
  }
}

void mainmain() {
  // int n;
  while (cin >> n, n) {
    // cout<<n<<endl;
    ma.clear();
    v = vector<string>(n);
    rep(i, n) cin >> v[i];
    sort(ALL(v));
    v.erase(unique(ALL(v)), v.end());
    rep(i, v.size()) {
      bool f = false;
      rep(j, v.size()) {
        if (i == j)
          continue;
        if (check(v[i], v[j]))
          f = true;
      }
      if (f) {
        v.erase(v.begin() + i);
        i--;
      }
    }
    n = v.size();
    // used=vint(1<<n,0);
    ans = string(1000, '}');
    dfs(0, "");
    cout << ans << endl;
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  mainmain();
}