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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// INSERT ABOVE HERE
signed main() {
  int n, k;
  cin >> n >> k;
  vector<string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  if (n < 10) {
    vector<int> v(n);
    iota(v.begin(), v.end(), 0);
    do {
      vector<int> used(n, 0), th;
      int ng = 0;
      for (int i = 0; i < n; i++) {
        if (i) {
          int pos = 0, lst = 0;
          for (int j = 0; j < k; j++) {
            int x = s[v[i]][j] - 'A';
            if (!used[x]) {
              lst = 1;
              continue;
            }
            if (lst)
              pos = th.size();
            while (pos < (int)th.size() && th[pos] != x)
              pos++;
          }
          if (pos == (int)th.size()) {
            ng = 1;
            break;
          }
        }
        for (int j = 0; j < k; j++) {
          int x = s[v[i]][j] - 'A';
          if (used[x])
            th.erase(find(th.begin(), th.end(), x));
          th.insert(th.begin(), x);
          used[x] = 1;
        }
      }
      if (ng)
        continue;
      for (int i = 0; i < n; i++)
        cout << char('A' + v[i]);
      cout << endl;
      break;
    } while (next_permutation(v.begin(), v.end()));
    return 0;
  }

  vector<vector<int>> G(n + 1);
  for (int i = 0; i < n; i++)
    G[n].emplace_back(i);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      int ng = 0;
      for (int a = 0; a < k; a++)
        for (int b = a + 1; b < k; b++)
          for (int c = 0; c < k; c++)
            for (int d = c + 1; d < k; d++)
              ng |= (s[i][a] == s[j][c] && s[i][b] == s[j][d]);
      if (!ng)
        G[i].emplace_back(j);
      cnt += !ng;
    }
  }
  // cout<<cnt<<endl;

  vector<int> us(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      int x = s[i][j] - 'A';
      us[i] |= (1 << x);
    }
  }

  using T = tuple<int, int, int, int, vector<int>>;
  set<T> fs;
  vector<int> ans(n, -1);
  function<void(int, int, int, int)> dfs = [&](int b, int d, int v, int used) {
    if (d == n) {
      for (int i = 0; i < n; i++)
        cout << char('A' + ans[i]);
      cout << endl;
      exit(0);
    }

    int vi = 0;
    vector<int> th;
    for (int i = d - 1; i >= 0; i--) {
      if ((vi | us[ans[i]]) == vi)
        continue;
      for (int j = k - 1; j >= 0; j--) {
        int x = s[ans[i]][j] - 'A';
        if ((~vi >> x) & 1)
          th.emplace_back(x);
      }
      vi |= us[ans[i]];
    }

    if (fs.count(T(b, d, v, used, th)))
      return;

    for (int u : G[v]) {
      if ((~b >> u) & 1)
        continue;
      if (used) {
        int pos = 0, lst = 0;
        for (int j = 0; j < k; j++) {
          int x = s[u][j] - 'A';
          if ((~used >> x) & 1) {
            lst = 1;
            continue;
          }
          if (lst)
            pos = th.size();
          while (pos < (int)th.size() && th[pos] != x)
            pos++;
        }
        if (pos == (int)th.size())
          continue;
      }
      ans[d] = u;
      dfs(b ^ (1 << u), d + 1, u, used | us[u]);
      ans[d] = -1;
    }

    fs.emplace(b, d, v, used, th);
  };

  dfs((1 << n) - 1, 0, n, 0);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

// INSERT ABOVE HERE
signed main() {
  int n, k;
  cin >> n >> k;
  vector<string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  if (n <= 10) {
    vector<int> v(n);
    iota(v.begin(), v.end(), 0);
    do {
      vector<int> used(n, 0), th;
      int ng = 0;
      for (int i = 0; i < n; i++) {
        if (i) {
          int pos = 0, lst = 0;
          for (int j = 0; j < k; j++) {
            int x = s[v[i]][j] - 'A';
            if (!used[x]) {
              lst = 1;
              continue;
            }
            if (lst)
              pos = th.size();
            while (pos < (int)th.size() && th[pos] != x)
              pos++;
          }
          if (pos == (int)th.size()) {
            ng = 1;
            break;
          }
        }
        for (int j = 0; j < k; j++) {
          int x = s[v[i]][j] - 'A';
          if (used[x])
            th.erase(find(th.begin(), th.end(), x));
          th.insert(th.begin(), x);
          used[x] = 1;
        }
      }
      if (ng)
        continue;
      for (int i = 0; i < n; i++)
        cout << char('A' + v[i]);
      cout << endl;
      break;
    } while (next_permutation(v.begin(), v.end()));
    return 0;
  }

  vector<vector<int>> G(n + 1);
  for (int i = 0; i < n; i++)
    G[n].emplace_back(i);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      int ng = 0;
      for (int a = 0; a < k; a++)
        for (int b = a + 1; b < k; b++)
          for (int c = 0; c < k; c++)
            for (int d = c + 1; d < k; d++)
              ng |= (s[i][a] == s[j][c] && s[i][b] == s[j][d]);
      if (!ng)
        G[i].emplace_back(j);
    }
  }

  vector<int> ans(n);
  function<void(int, int, int, vector<int>)> dfs = [&](int b, int d, int v,
                                                       vector<int> th) {
    if (d == n) {
      for (int i = 0; i < n; i++)
        cout << char('A' + ans[i]);
      cout << endl;
      exit(0);
    }
    vector<int> used(n, 0);
    for (int x : th)
      used[x] = 1;

    for (int u : G[v]) {
      if ((~b >> u) & 1)
        continue;
      int ng = 0;
      if (!th.empty()) {
        int pos = 0, lst = 0;
        for (int j = 0; j < k; j++) {
          int x = s[u][j] - 'A';
          if (!used[x]) {
            lst = 1;
            continue;
          }
          if (lst)
            pos = th.size();
          while (pos < (int)th.size() && th[pos] != x)
            pos++;
        }
        if (pos == (int)th.size()) {
          ng = 1;
          break;
        }
      }
      if (ng)
        continue;
      vector<int> nth(th);
      for (int j = 0; j < k; j++) {
        int x = s[u][j] - 'A';
        if (used[x])
          nth.erase(find(nth.begin(), nth.end(), x));
        nth.insert(nth.begin(), x);
      }
      ans[d] = u;
      dfs(b ^ (1 << u), d + 1, u, nth);
    }
  };

  dfs((1 << n) - 1, 0, n, vector<int>());
  return 0;
}