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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef pair<string, int> Psi;
typedef vector<int> Vi;
int N;
vector<string> ss, words[21];
vector<Psi> vpsi;
Vi ans;
bool app[26];
void init() {
  ans.clear();
  ss.clear();
  rep(i, 21) words[i].clear();
  vpsi.clear();
  rep(i, 26) app[i] = 0;
}
const bool lencomp(Psi x, Psi y) {
  string s = x.fs, t = y.fs;
  bool a[26] = {}, b[26] = {};
  rep(i, s.size()) a[s[i] - 'A'] = 1;
  rep(i, t.size()) b[t[i] - 'A'] = 1;
  int c = 0, d = 0;
  rep(i, 26) if (a[i]) c++;
  rep(i, 26) if (b[i]) d++;
  return c > d;
}
inline bool eq(Vi &a, Vi &b) {
  rep(i, 26) if (app[i] && a[i] != b[i]) return 0;
  return 1;
}
bool dfs(int x, Vi vi) {
  if (x == ss.size()) {
    if (!ans.empty() && !eq(ans, vi))
      return false;
    ans = vi;
    return true;
  }
  string s = ss[x];
  int L = s.size();
  for (string w : words[L]) {
    Vi nvi = vi;
    bool ok = 1;
    rep(i, L) {
      if (nvi[s[i] - 'A'] >= 0 && nvi[s[i] - 'A'] + 'A' != w[i]) {
        ok = 0;
        break;
      }
      if (nvi[w[i] - 'A'] >= 0 && nvi[w[i] - 'A'] + 'A' != s[i]) {
        ok = 0;
        break;
      }
      nvi[s[i] - 'A'] = w[i] - 'A';
      nvi[w[i] - 'A'] = s[i] - 'A';
    }
    if (!ok)
      continue;
    if (!dfs(x + 1, nvi))
      return false;
  }
  return true;
}
int main() {
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    init();
    rep(i, N) {
      string s;
      cin >> s;
      words[s.size()].pb(s);
    }
    int I = 0;
    while (true) {
      string s;
      cin >> s;
      rep(i, s.size()) if (s[i] != '.') app[s[i] - 'A'] = 1;
      if (s.back() == '.') {
        vpsi.pb(Psi(s.substr(0, s.size() - 1), I++));
        break;
      }
      vpsi.pb(Psi(s, I++));
    }
    sort(all(vpsi), lencomp);
    for (Psi psi : vpsi)
      ss.pb(psi.fs);
    if (dfs(0, Vi(26, -1)) && !ans.empty()) {
      vector<string> vst(ss.size());
      rep(i, ss.size()) vst[vpsi[i].sc] = ss[i];
      rep(i, vst.size()) {
        rep(j, vst[i].size()) { cout << (char)(ans[vst[i][j] - 'A'] + 'A'); }
        cout << (i + 1 == vst.size() ? ".\\n" : " ");
      }
    } else {
      puts("-.");
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef pair<string, int> Psi;
typedef vector<int> Vi;
int N;
vector<string> ss, words[21];
vector<Psi> vpsi;
Vi ans;
bool app[26];
void init() {
  ans.clear();
  ss.clear();
  rep(i, 21) words[i].clear();
  vpsi.clear();
  rep(i, 26) app[i] = 0;
}
const bool lencomp(Psi x, Psi y) { return x.fs.size() > y.fs.size(); }
inline bool eq(Vi &a, Vi &b) {
  rep(i, 26) if (app[i] && a[i] != b[i]) return 0;
  return 1;
}
bool dfs(int x, Vi vi) {
  if (x == ss.size()) {
    if (!ans.empty() && !eq(ans, vi))
      return false;
    ans = vi;
    return true;
  }
  string s = ss[x];
  int L = s.size();
  for (string w : words[L]) {
    Vi nvi = vi;
    bool ok = 1;
    rep(i, L) {
      if (nvi[s[i] - 'A'] >= 0 && nvi[s[i] - 'A'] + 'A' != w[i]) {
        ok = 0;
        break;
      }
      if (nvi[w[i] - 'A'] >= 0 && nvi[w[i] - 'A'] + 'A' != s[i]) {
        ok = 0;
        break;
      }
      nvi[s[i] - 'A'] = w[i] - 'A';
      nvi[w[i] - 'A'] = s[i] - 'A';
    }
    if (!ok)
      continue;
    if (!dfs(x + 1, nvi))
      return false;
  }
  return true;
}
int main() {
  while (true) {
    cin >> N;
    if (N == 0)
      break;
    init();
    rep(i, N) {
      string s;
      cin >> s;
      words[s.size()].pb(s);
    }
    int I = 0;
    while (true) {
      string s;
      cin >> s;
      rep(i, s.size()) if (s[i] != '.') app[s[i] - 'A'] = 1;
      if (s.back() == '.') {
        vpsi.pb(Psi(s.substr(0, s.size() - 1), I++));
        break;
      }
      vpsi.pb(Psi(s, I++));
    }
    sort(all(vpsi), lencomp);
    for (Psi psi : vpsi)
      ss.pb(psi.fs);
    if (dfs(0, Vi(26, -1)) && !ans.empty()) {
      vector<string> vst(ss.size());
      rep(i, ss.size()) vst[vpsi[i].sc] = ss[i];
      rep(i, vst.size()) {
        rep(j, vst[i].size()) { cout << (char)(ans[vst[i][j] - 'A'] + 'A'); }
        cout << (i + 1 == vst.size() ? ".\\n" : " ");
      }
    } else {
      puts("-.");
    }
  }
}