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

#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int tr[26], ans[26];
vector<string> split(string s) {
  vector<string> res;
  stringstream ss(s);

  string a;
  while (ss >> a) {
    res.push_back(a);
  }

  return res;
}

bool check(char a, char b) {
  if (tr[a - 'A'] != -1 && tr[a - 'A'] != b)
    return false;
  if (tr[b - 'A'] != -1 && tr[b - 'A'] != a)
    return false;

  return true;
}

bool match(string w, string msg) {
  for (int i = 0; i < msg.size(); i++) {
    if (!check(w[i], msg[i]))
      return false;
  }

  return true;
}

int dfs(int p, vector<string> &words, vector<string> &msg) {
  if (p == msg.size()) {
    memcpy(ans, tr, sizeof(ans));
    return 1;
  }

  int res = 0;
  for (int i = 0; i < words.size(); i++) {
    if (res >= 2)
      return 1192;

    if (msg[p].size() != words[i].size())
      continue;
    if (!match(words[i], msg[p]))
      continue;

    bool valid = true;
    int t[26];
    memcpy(t, tr, sizeof(t));
    for (int j = 0; j < msg[p].size(); j++) {
      if (!check(words[i][j], msg[p][j])) {
        valid = false;
        break;
      }
      tr[msg[p][j] - 'A'] = words[i][j];
      tr[words[i][j] - 'A'] = msg[p][j];
    }
    if (valid) {
      res += dfs(p + 1, words, msg);
    }
    memcpy(tr, t, sizeof(tr));
  }

  return res;
}

bool comp(const string &a, const string &b) {
  if (a.size() != b.size())
    return a.size() > b.size();
  return a > b;
}

int main() {
  int N;
  while (cin >> N, N) {
    vector<string> words(N);
    for (int i = 0; i < N; i++)
      cin >> words[i];

    string str;
    getline(cin, str);
    getline(cin, str);

    str.erase(str.end() - 1);

    sort(words.begin(), words.end(), comp);

    vector<string> msg = split(str);
    sort(msg.begin(), msg.end());
    msg.erase(unique(msg.begin(), msg.end()), msg.end());
    sort(msg.begin(), msg.end(), comp);

    for (int i = 0; i < 26; i++)
      tr[i] = -1;

    int res = dfs(0, words, msg);
    if (res != 1)
      cout << "-." << endl;
    else {
      for (int i = 0; i < str.size(); i++) {
        if (isalpha(str[i]))
          cout << (char)ans[str[i] - 'A'];
        else
          cout << str[i];
      }

      cout << "." << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int tr[26], ans[26];
vector<string> split(string s) {
  vector<string> res;
  stringstream ss(s);

  string a;
  while (ss >> a) {
    res.push_back(a);
  }

  return res;
}

bool check(char a, char b) {
  if (tr[a - 'A'] != -1 && tr[a - 'A'] != b)
    return false;
  if (tr[b - 'A'] != -1 && tr[b - 'A'] != a)
    return false;

  return true;
}

bool match(string w, string msg) {
  for (int i = 0; i < msg.size(); i++) {
    if (!check(w[i], msg[i]))
      return false;
  }

  return true;
}

int dfs(int p, vector<string> &words, vector<string> &msg) {

  if (p == msg.size()) {
    memcpy(ans, tr, sizeof(ans));
    return 1;
  }

  int res = 0;
  for (int i = 0; i < words.size(); i++) {
    if (msg[p].size() != words[i].size())
      continue;
    if (!match(words[i], msg[p]))
      continue;

    bool valid = true;
    int t[26];
    memcpy(t, tr, sizeof(t));
    for (int j = 0; j < msg[p].size(); j++) {
      if (!check(words[i][j], msg[p][j])) {
        valid = false;
        break;
      }
      tr[msg[p][j] - 'A'] = words[i][j];
      tr[words[i][j] - 'A'] = msg[p][j];
    }
    if (valid) {
      res += dfs(p + 1, words, msg);
    }
    memcpy(tr, t, sizeof(tr));
  }

  return res;
}

bool comp(const string &a, const string &b) {
  if (a.size() != b.size())
    return a.size() > b.size();
  return a > b;
}

int main() {
  int N;
  while (cin >> N, N) {
    vector<string> words(N);
    for (int i = 0; i < N; i++)
      cin >> words[i];

    string str;
    getline(cin, str);
    getline(cin, str);

    str.erase(str.end() - 1);

    sort(words.begin(), words.end(), comp);

    vector<string> msg = split(str);
    sort(msg.begin(), msg.end());
    msg.erase(unique(msg.begin(), msg.end()), msg.end());
    sort(msg.begin(), msg.end(), comp);

    for (int i = 0; i < 26; i++)
      tr[i] = -1;

    int res = dfs(0, words, msg);
    if (res != 1)
      cout << "-." << endl;
    else {
      for (int i = 0; i < str.size(); i++) {
        if (isalpha(str[i]))
          cout << (char)ans[str[i] - 'A'];
        else
          cout << str[i];
      }

      cout << "." << endl;
    }
  }
}