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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
vector<string> w, v;
vector<string> xyz[30];
string s;
vector<char> vc, ans;
typedef pair<int, int> P;
vector<P> vp;
vector<int> used;
int valid;
vector<int> wc1, wc2;
void dfs() {
  for (int i = 0; i < (int)v.size(); i++) {
    string &t = v[i];
    bool flg = 0;
    for (int j = 0; j < (int)xyz[t.size()].size(); j++) {
      string &u = xyz[t.size()][j];
      bool ok = 1;
      for (int k = 0; k < (int)t.size(); k++) {
        if (!used[t[k] - 'A'])
          continue;
        if (!used[u[k] - 'A']) {
          ok = 0;
          break;
        }
        ok &= vc[t[k] - 'A'] == u[k] - 'A';
        if (!ok)
          break;
      }
      // cout<<t<<" "<<u<<":"<<ok<<endl;
      if (ok) {
        flg = 1;
        break;
      }
    }
    if (!flg)
      return;
  }
  int cnt = 0;
  for (int i = 0; i < 26; i++)
    if (used[vp[i].second])
      cnt++;
  // cout<<cnt<<endl;
  // if(cnt>1) return;
  if (cnt == 26) {
    valid++;
    ans = vc;
    return;
  }
  int x = 0;
  while (used[vp[x].second])
    x++;
  // cout<<x<<endl;
  if (vp[x].first) {
    for (int k = 0; k < 26; k++) {
      if (used[vp[k].second])
        continue;
      used[vp[x].second] = used[vp[k].second] = 1;
      vc[vp[x].second] = vp[k].second;
      vc[vp[k].second] = vp[x].second;
      // cout<<x<<" "<<k<<endl;
      // cout<<(char)(vp[x].second+'A')<<"<->"<<(char)(vp[k].second+'A')<<endl;
      dfs();
      if (valid > 1)
        return;
      used[vp[x].second] = used[vp[k].second] = 0;
      vc[vp[x].second] = vc[vp[k].second] = 0;
    }
  } else {
    used[vp[x].second] = 1;
    vc[vp[x].second] = vp[x].second;
    dfs();
    used[vp[x].second] = 0;
    vc[vp[x].second] = 0;
  }
}
signed main() {
  while (cin >> n, n) {
    for (int i = 0; i < 30; i++)
      xyz[i].clear();
    w.resize(n);
    for (int i = 0; i < n; i++)
      cin >> w[i], xyz[w[i].size()].push_back(w[i]);
    cin.ignore();
    getline(cin, s);
    assert(s.back() == '.');
    s.pop_back();
    v.clear();
    for (int i = 0; i < (int)s.size(); i++) {
      string t;
      while (i < (int)s.size() && s[i] != ' ')
        t += s[i++];
      // cout<<t<<endl;
      v.push_back(t);
    }
    vc.clear();
    vc.resize(26, 0);
    wc2.clear();
    wc2.resize(26, 0);
    for (int i = 0; i < (int)v.size(); i++)
      for (int j = 0; j < (int)v[i].size(); j++)
        wc2[v[i][j] - 'A']++;
    vp.resize(26);
    for (int i = 0; i < 26; i++)
      vp[i] = P(wc2[i], i);
    sort(vp.rbegin(), vp.rend());
    used.clear();
    used.resize(26, 0);
    valid = 0;
    dfs();
    // cout<<valid<<endl;
    if (valid != 1) {
      cout << "-." << endl;
      continue;
    }
    // puts("found");
    for (int i = 0; i < (int)v.size(); i++) {
      if (i)
        cout << " ";
      for (int j = 0; j < (int)v[i].size(); j++) {
        // cout<<v[i][j]-'A'<<endl;
        cout << (char)(ans[v[i][j] - 'A'] + 'A');
      }
    }
    cout << "." << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
vector<string> w, v;
string s;
vector<char> vc, ans;
typedef pair<int, int> P;
vector<P> vp;
vector<int> used;
int valid;
vector<int> wc1, wc2;
void dfs() {
  for (int i = 0; i < (int)v.size(); i++) {
    string t = v[i];
    bool flg = 0;
    for (int j = 0; j < n; j++) {
      string u = w[j];
      if (t.size() != u.size())
        continue;
      bool ok = 1;
      for (int k = 0; k < (int)t.size(); k++) {
        if (!used[t[k] - 'A'] || !used[u[k] - 'A'])
          continue;
        ok &= vc[t[k] - 'A'] == u[k] - 'A';
        if (!ok)
          break;
      }
      if (ok) {
        flg = 1;
        break;
      }
    }
    if (!flg)
      return;
  }
  int cnt = 0;
  for (int i = 0; i < 26; i++)
    if (used[vp[i].second] || vp[i].first == 0)
      cnt++;
  // cout<<cnt<<endl;
  if (cnt == 26) {
    valid++;
    ans = vc;
    return;
  }
  int x = 0;
  while (used[vp[x].second] || vp[x].first == 0)
    x++;
  // cout<<x<<endl;
  for (int k = 0; k < 26; k++) {
    if (used[vp[k].second] || wc1[vp[k].second] == 0)
      continue;
    if (wc1[vp[x].second] != wc2[vp[k].second])
      continue;
    used[vp[x].second] = used[vp[k].second] = 1;
    vc[vp[x].second] = vp[k].second;
    vc[vp[k].second] = vp[x].second;
    // cout<<x<<" "<<k<<endl;
    // cout<<(char)(vp[x].second+'A')<<"<->"<<(char)(vp[k].second+'A')<<endl;
    dfs();
    if (valid > 1)
      return;
    used[vp[x].second] = used[vp[k].second] = 0;
    vc[vp[x].second] = vc[vp[k].second] = 0;
  }
}
signed main() {
  while (cin >> n, n) {
    w.resize(n);
    for (int i = 0; i < n; i++)
      cin >> w[i];
    cin.ignore();
    getline(cin, s);
    assert(s.back() == '.');
    s.pop_back();
    v.clear();
    for (int i = 0; i < (int)s.size(); i++) {
      string t;
      while (i < (int)s.size() && s[i] != ' ')
        t += s[i++];
      // cout<<t<<endl;
      v.push_back(t);
    }
    vc.clear();
    vc.resize(26, 0);
    wc1.clear();
    wc2.clear();
    wc1.resize(26, 0);
    wc2.resize(26, 0);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < (int)w[i].size(); j++)
        wc1[w[i][j] - 'A']++;
    for (int i = 0; i < (int)v.size(); i++)
      for (int j = 0; j < (int)v[i].size(); j++)
        wc2[v[i][j] - 'A']++;
    vp.resize(26);
    for (int i = 0; i < 26; i++)
      vp[i] = P(wc2[i], i);
    sort(vp.rbegin(), vp.rend());
    used.clear();
    used.resize(26, 0);
    valid = 0;
    dfs();
    // cout<<valid<<endl;
    if (valid != 1) {
      cout << "-." << endl;
      continue;
    }
    // puts("found");
    for (int i = 0; i < (int)v.size(); i++) {
      if (i)
        cout << " ";
      for (int j = 0; j < (int)v[i].size(); j++) {
        // cout<<v[i][j]-'A'<<endl;
        cout << (char)(ans[v[i][j] - 'A'] + 'A');
      }
    }
    cout << "." << endl;
  }
  return 0;
}