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

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 21;
int N;
string word[MAXN];
string line;
vector<string> V;
vector<int> F;
map<char, char> ans;

bool ok(const string &x, const string &w, map<char, char> m) {
  if (x.size() != w.size())
    return false;
  for (int i = 0; i < x.size(); ++i) {
    if (m.count(x[i]) && m[x[i]] != w[i])
      return false;
    if (m.count(w[i]) && m[w[i]] != x[i])
      return false;
    m[x[i]] = w[i];
    m[w[i]] = x[i];
  }
  return true;
}

void reg(const string &x, const string &w, map<char, char> &m) {
  for (int i = 0; i < x.size(); ++i) {
    m[x[i]] = w[i];
    m[w[i]] = x[i];
  }
}

bool update(int k, map<char, char> m) {
  F = vector<int>(V.size());
  for (int i = k; i < V.size(); ++i) {
    for (int j = 0; j < N; ++j) {
      if (ok(V[i], word[j], m)) {
        F[i] |= 1 << j;
      }
    }
    if (!F[i])
      return false;
  }
  return true;
}

void rec(int k, map<char, char> m) {
  if (k == V.size()) {
    if (ans.size())
      throw 0;
    ans = m;
    return;
  }
  for (int i = 0; i < N; ++i) {
    if (F[k] >> i & 1)
      ;
    else
      continue;
    map<char, char> nm = m;
    reg(V[k], word[i], nm);
    vector<int> undo = F;
    if (update(k + 1, nm)) {
      rec(k + 1, nm);
    }
    F = undo;
  }
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i < N; ++i) {
      cin >> word[i];
    }
    cin.ignore();
    getline(cin, line);
    stringstream ss(line.substr(0, line.size() - 1));
    set<string> ws;
    for (string s; ss >> s;)
      ws.insert(s);
    V.clear();
    for (set<string>::iterator it = ws.begin(); it != ws.end(); ++it) {
      V.push_back(*it);
    }
    update(0, map<char, char>());
    ans = map<char, char>();
    try {
      rec(0, map<char, char>());
      if (ans.empty())
        throw 0;
      for (int i = 0; i < line.size(); ++i) {
        if (isalpha(line[i]))
          cout << ans[line[i]];
        else
          cout << line[i];
      }
      cout << endl;
    } catch (...) {
      cout << "-." << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 21;
int N;
string word[MAXN];
string line;
vector<string> V;
vector<int> F;
map<char, char> ans;

bool ok(const string &x, const string &w, map<char, char> m) {
  if (x.size() != w.size())
    return false;
  for (int i = 0; i < x.size(); ++i) {
    if (m.count(x[i]) && m[x[i]] != w[i])
      return false;
    m[x[i]] = w[i];
    m[w[i]] = x[i];
  }
  return true;
}

void reg(const string &x, const string &w, map<char, char> &m) {
  for (int i = 0; i < x.size(); ++i) {
    m[x[i]] = w[i];
    m[w[i]] = x[i];
  }
}

void update(map<char, char> m) {
  F = vector<int>(V.size());
  for (int i = 0; i < V.size(); ++i) {
    for (int j = 0; j < N; ++j) {
      if (ok(V[i], word[j], m)) {
        F[i] |= 1 << j;
      }
    }
  }
}

void rec(int k, map<char, char> m) {
  if (k == V.size()) {
    if (ans.size())
      throw 0;
    ans = m;
    return;
  }
  for (int i = 0; i < N; ++i) {
    if (F[k] >> i & 1)
      ;
    else
      continue;
    map<char, char> nm = m;
    reg(V[k], word[i], nm);
    vector<int> undo = F;
    update(nm);
    rec(k + 1, nm);
    F = undo;
  }
}

int main() {
  while (cin >> N && N) {
    for (int i = 0; i < N; ++i) {
      cin >> word[i];
    }
    cin.ignore();
    getline(cin, line);
    stringstream ss(line.substr(0, line.size() - 1));
    set<string> ws;
    for (string s; ss >> s;)
      ws.insert(s);
    V.clear();
    for (set<string>::iterator it = ws.begin(); it != ws.end(); ++it) {
      V.push_back(*it);
    }
    update(map<char, char>());
    ans = map<char, char>();
    try {
      rec(0, map<char, char>());
      if (ans.empty())
        throw 0;
      for (int i = 0; i < line.size(); ++i) {
        if (isalpha(line[i]))
          cout << ans[line[i]];
        else
          cout << line[i];
      }
      cout << endl;
    } catch (...) {
      cout << "-." << endl;
    }
  }
  return 0;
}