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

#include <algorithm>
#include <iostream>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define phb push_back
#define ppb pop_back
using namespace std;

int N;
string D[20];
vector<string> S;

vector<string> t;
vector<int> mtc, res;
int tbl[128], clv[128];

bool read();
void solve();

void dfs(int);

int main() {
  while (read())
    solve();

  return 0;
}

bool read() {
  string str, ln;
  istringstream iss;

  getline(cin, ln), iss.clear(), iss.str(ln);
  if (iss >> N, N == 0)
    return false;

  for (int i = 0; i < N; ++i)
    getline(cin, D[i]);

  getline(cin, ln), ln.back() = ' ', iss.clear(), iss.str(ln);
  S.clear();
  while (iss >> str)
    S.phb(str);

  return true;
}

void solve() {
  t.clear();
  for (int i = 0; i < sz(S); ++i)
    if (find(all(t), S[i]) == t.end())
      t.phb(S[i]);

  auto cmpf = [&](const string &ra, const string &rb) -> bool {
    set<char> sa, sb;

    for (int i = 0; i < sz(ra); ++i)
      sa.insert(ra[i]);
    for (int i = 0; i < sz(rb); ++i)
      sb.insert(rb[i]);

    return sz(sa) > sz(sb);
  };

  sort(&D[0], &D[N], cmpf);
  sort(all(t), cmpf);

  fill(&tbl[0], &tbl[128], 0);
  fill(&clv[0], &clv[128], -1);
  mtc.clear(), res.clear();

  try {
    dfs(0);
    for (int i = 0; i < sz(S); ++i) {
      int k = int(find(all(t), S[i]) - t.begin());
      cout << D[res[k]] << (i + 1 == sz(S) ? "." : " ");
    }
  } catch (...) {
    cout << "-.";
  }
  cout << "\\n";
}

void dfs(int u) {
  if (u == sz(t)) {
    if (!res.empty())
      throw exception();
    res = mtc;
  } else {
    for (int i = 0; i < N; ++i)
      if (sz(D[i]) == sz(t[u])) {
        int j = 0, c1, c2;

        while (j < sz(D[i])) {
          c1 = int(D[i][j]), c2 = int(t[u][j]);
          if ((tbl[c1] != 0 && tbl[c1] != c2) ||
              (tbl[c2] != 0 && tbl[c2] != c1))
            break;
          if (tbl[c1] == 0)
            tbl[c1] = c2, tbl[c2] = c1, clv[c1] = clv[c2] = u;
          ++j;
        }

        if (j == sz(D[i])) {
          mtc.phb(i);
          dfs(u + 1);
          mtc.ppb();
        }

        while ((--j) >= 0) {
          c1 = int(D[i][j]), c2 = int(t[u][j]);
          if (clv[c1] == u)
            tbl[c1] = tbl[c2] = 0, clv[c1] = clv[c2] = -1;
        }
      }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define phb push_back
#define ppb pop_back
using namespace std;

int N;
string D[20];
vector<string> S;

vector<string> t;
vector<int> mtc, res;
int tbl[128], clv[128];

bool read();
void solve();

void dfs(int);

int main() {
  while (read())
    solve();

  return 0;
}

bool read() {
  string str, ln;
  istringstream iss;

  getline(cin, ln), iss.clear(), iss.str(ln);
  if (iss >> N, N == 0)
    return false;

  for (int i = 0; i < N; ++i)
    getline(cin, D[i]);

  getline(cin, ln), ln.back() = ' ', iss.clear(), iss.str(ln);
  S.clear();
  while (iss >> str)
    S.phb(str);

  return true;
}

void solve() {
  t.clear();
  for (int i = 0; i < sz(S); ++i)
    if (find(all(t), S[i]) == t.end())
      t.phb(S[i]);

  auto cmpf = [&](const string &ra, const string &rb) -> bool {
    return sz(ra) > sz(rb);
  };

  sort(&D[0], &D[N], cmpf);
  sort(all(t), cmpf);

  fill(&tbl[0], &tbl[128], 0);
  fill(&clv[0], &clv[128], -1);
  mtc.clear(), res.clear();

  try {
    dfs(0);
    for (int i = 0; i < sz(S); ++i) {
      int k = int(find(all(t), S[i]) - t.begin());
      cout << D[res[k]] << (i + 1 == sz(S) ? "." : " ");
    }
  } catch (...) {
    cout << "-.";
  }
  cout << "\\n";
}

void dfs(int u) {
  if (u == sz(t)) {
    if (!res.empty())
      throw exception();
    res = mtc;
  } else {
    for (int i = 0; i < N; ++i)
      if (sz(D[i]) == sz(t[u])) {
        int j = 0, c1, c2;

        while (j < sz(D[i])) {
          c1 = int(D[i][j]), c2 = int(t[u][j]);
          if ((tbl[c1] != 0 && tbl[c1] != c2) ||
              (tbl[c2] != 0 && tbl[c2] != c1))
            break;
          if (tbl[c1] == 0)
            tbl[c1] = c2, tbl[c2] = c1, clv[c1] = clv[c2] = u;
          ++j;
        }

        if (j == sz(D[i])) {
          mtc.phb(i);
          dfs(u + 1);
          mtc.ppb();
        }

        while ((--j) >= 0) {
          c1 = int(D[i][j]), c2 = int(t[u][j]);
          if (clv[c1] == u)
            tbl[c1] = tbl[c2] = 0, clv[c1] = clv[c2] = -1;
        }
      }
  }
}