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

#include <bits/stdc++.h>
#define int long long
#define N 17
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
int Max(int &a, int b) { return a = max(a, b); }
string Min(string &a, string b) { return a = min(a, b); }
int n, K;
string his[N];

string getNx(const string &a, const string &str) {
  string res;
  int j = 0, ch[26] = {};

  for (char s : str)
    if (s != '?')
      ch[s - 'A']++;
  for (char s : str) {
    if (j < K && (a[j] == s || (s == '?' && !ch[a[j] - 'A'])))
      res = a[j++] + res;
    else
      res += s;
  }
  if (j < K)
    return "";
  return res;
}

string ans = "zzzzzzzz";
void dfs(int bit, string str, string ord) {
  if (bit == (1 << n) - 1) {
    ans = min(ans, ord);
    return;
  }

  for (int i = 0; i < n; i++) {
    if (bit >> i & 1)
      continue;
    string nstr = getNx(his[i], str);
    int nbit = bit | (1 << i);
    if (nstr == "")
      continue;
    dfs(nbit, nstr, ord + char('A' + i));
  }
}

typedef pair<string, string> P;
string DP() {
  map<string, string> dp[1 << N]; // (key = current thread, value = min order)
  dp[0][string(n, '?')] = "";

  for (int bit = 0; bit < (1 << n); bit++) { // used
    for (int i = 0; i < n; i++) {            // next use
      if (bit >> i & 1)
        continue;
      for (P p : dp[bit]) {
        int nbit = bit | (1 << i);
        string nsta = getNx(his[i], p.first);
        string norder = p.second + char('A' + i);
        if (nsta == "")
          continue;

        if (!dp[nbit].count(nsta))
          dp[nbit][nsta] = norder;
        else
          Min(dp[nbit][nsta], norder);
      }
    }
  }
  string ans(50, 'Z');
  for (P p : dp[(1 << n) - 1])
    Min(ans, p.second);
  return ans;
}

signed main() {
  cin >> n >> K;
  for (int i = 0; i < n; i++)
    cin >> his[i];
  // dfs(0,string(n,'?'),"");
  // cout<<ans<<endl;
  cout << DP() << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define N 17
using namespace std;
const int INF = 1LL << 55;
const int mod = (1e9) + 7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
int Max(int &a, int b) { return a = max(a, b); }
int Min(int &a, int b) { return a = min(a, b); }

int n, K;
string his[N];

string getStr(const string &a, const string &str) {
  string res;
  int j = 0, ch[26] = {};

  for (char s : str)
    if (s != '?')
      ch[s - 'A']++;
  for (char s : str) {
    if (j < K && (a[j] == s || (s == '?' && !ch[a[j] - 'A'])))
      res = a[j++] + res;
    else
      res += s;
  }
  if (j < K)
    return "";
  return res;
}

string ans = "zzzzzzzz";
void dfs(int bit, string str, string ord) {
  if (bit == (1 << n) - 1) {
    ans = min(ans, ord);
    return;
  }

  for (int i = 0; i < n; i++) {
    if (bit >> i & 1)
      continue;
    string nstr = getStr(his[i], str);
    int nbit = bit | (1 << i);
    if (nstr == "")
      continue;
    dfs(nbit, nstr, ord + char('A' + i));
  }
}

signed main() {
  cin >> n >> K;
  for (int i = 0; i < n; i++)
    cin >> his[i];
  dfs(0, string(n, '?'), "");
  cout << ans << endl;
  return 0;
}