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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int n;
vector<string> word;
vector<string> seq;
map<char, int> mp;
map<char, char> conv;
vector<pair<int, char>> ord;
string ans;
int num;

bool check() {
  rep(i, seq.size()) {
    bool flag = false;
    rep(j, word.size()) {
      if (word[j].size() != seq[i].size())
        continue;
      bool flag2 = true;
      rep(k, seq[i].size()) {
        if (conv[seq[i][k]] == '$')
          continue;
        if (conv[seq[i][k]] != word[j][k])
          flag2 = false;
      }
      if (flag2)
        flag = true;
    }
    if (!flag)
      return false;
  }
  return true;
}

void dfs(int idx) {
  // for(auto p : conv) {
  // cout << p.first << " " << p.second << endl;
  //}
  // cout << check() << endl;
  if (!check())
    return;
  if (idx == ord.size()) {
    string tmp = "";
    rep(i, seq.size()) {
      if (i)
        tmp += " ";
      rep(j, seq[i].size()) { tmp += conv[seq[i][j]]; }
    }
    ans = tmp;
    num++;
    return;
  }
  if (conv[ord[idx].second] != '$') {
    dfs(idx + 1);
    return;
  }
  /// cout << ord[idx].second << endl;
  rep(i, 26) {
    if (conv[(char)('A' + i)] != '$')
      continue;
    conv[ord[idx].second] = (char)('A' + i);
    conv[(char)('A' + i)] = ord[idx].second;
    dfs(idx + 1);
    if (num > 1)
      return;
    conv[ord[idx].second] = '$';
    conv[(char)('A' + i)] = '$';
  }
}

signed main() {
  while (cin >> n, n) {
    word.clear();
    word.resize(n);
    rep(i, n) cin >> word[i];
    seq.clear();
    while (1) {
      string s;
      cin >> s;
      seq.push_back(s);
      if (s.back() == '.') {
        seq[seq.size() - 1].pop_back();
        break;
      }
    }
    ord.clear();
    mp.clear();
    conv.clear();
    rep(i, seq.size()) rep(j, seq[i].size()) {
      if (seq[i][j] == '.')
        continue;
      mp[seq[i][j]]++;
    }
    for (auto p : mp) {
      ord.push_back(make_pair(p.second, p.first));
    }
    sort(ord.begin(), ord.end(), greater<pair<int, int>>());
    ans = "-";
    rep(i, 26) conv[(char)('A' + i)] = '$';
    conv['.'] = '.';
    num = 0;
    dfs(0);
    // cout << num << endl;
    if (num != 1)
      ans = "-";
    cout << ans + "." << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int n;
vector<string> word;
vector<string> seq;
map<char, int> mp;
map<char, char> conv;
vector<pair<int, char>> ord;
string ans;
int num;

bool check() {
  rep(i, seq.size()) {
    bool flag = false;
    rep(j, word.size()) {
      if (word[j].size() != seq[i].size())
        continue;
      bool flag2 = true;
      rep(k, seq[i].size()) {
        if (conv[seq[i][k]] == '$')
          continue;
        if (conv[seq[i][k]] != word[j][k])
          flag2 = false;
      }
      if (flag2)
        flag = true;
    }
    if (!flag)
      return false;
  }
  return true;
}

void dfs(int idx) {
  // for(auto p : conv) {
  // cout << p.first << " " << p.second << endl;
  //}
  // cout << check() << endl;
  if (!check())
    return;
  if (idx == ord.size()) {
    string tmp = "";
    rep(i, seq.size()) {
      if (i)
        tmp += " ";
      rep(j, seq[i].size()) { tmp += conv[seq[i][j]]; }
    }
    ans = tmp;
    num++;
    return;
  }
  if (conv[ord[idx].second] != '$') {
    dfs(idx + 1);
    return;
  }
  /// cout << ord[idx].second << endl;
  rep(i, 26) {
    if (conv[(char)('A' + i)] != '$')
      continue;
    conv[ord[idx].second] = (char)('A' + i);
    conv[(char)('A' + i)] = ord[idx].second;
    dfs(idx + 1);
    conv[ord[idx].second] = '$';
    conv[(char)('A' + i)] = '$';
  }
}

signed main() {
  while (cin >> n, n) {
    word.clear();
    word.resize(n);
    rep(i, n) cin >> word[i];
    seq.clear();
    while (1) {
      string s;
      cin >> s;
      seq.push_back(s);
      if (s.back() == '.') {
        seq[seq.size() - 1].pop_back();
        break;
      }
    }
    ord.clear();
    mp.clear();
    conv.clear();
    rep(i, seq.size()) rep(j, seq[i].size()) {
      if (seq[i][j] == '.')
        continue;
      mp[seq[i][j]]++;
    }
    for (auto p : mp) {
      ord.push_back(make_pair(p.second, p.first));
    }
    sort(ord.begin(), ord.end(), greater<pair<int, int>>());
    ans = "-";
    rep(i, 26) conv[(char)('A' + i)] = '$';
    conv['.'] = '.';
    num = 0;
    dfs(0);
    // cout << num << endl;
    if (num != 1)
      ans = "-";
    cout << ans + "." << endl;
  }
  return 0;
}