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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

vector<string> str[21];
vector<string> seq;
vector<char> mp(26);
int n;
char ans[26];
int ok;

map<pair<int, vector<char>>, bool> memo;

void dfs(int num) {
  // cout << num << endl;
  // if (memo[make_pair(num,mp)]) return;
  // memo[make_pair(num,mp)] = 1;

  if (num == seq.size()) {
    ok++;
    REP(i, 26)
    ans[i] = mp[i];
    return;
  }
  bool change[26];
  string s = seq[num];
  if (ok > 1)
    return;
  FOR(it, str[s.size()]) {
    string t = *it;
    bool dame = 0;
    memset(change, 0, sizeof(change));
    REP(i, s.size()) {
      if (mp[s[i] - 'A'] == 0) {
        if (mp[t[i] - 'A']) {
          dame = 1;
          break;
        }
        mp[s[i] - 'A'] = t[i];
        mp[t[i] - 'A'] = s[i];
        change[s[i] - 'A'] = 1;
        change[t[i] - 'A'] = 1;
      } else if (mp[s[i] - 'A'] != t[i]) {
        dame = 1;
        break;
      }
    }
    // REP(i, 26) if (mp[i]) printf("(%c,%c)",i+'A', mp[i]);
    // cout << endl;
    if (!dame)
      dfs(num + 1);
    REP(i, 26)
    if (change[i])
      mp[i] = 0;
  }
}

int main() {
  while (cin >> n, n) {
    REP(i, 21) str[i].clear();
    REP(i, n) {
      string tmp;
      cin >> tmp;
      str[tmp.size()].push_back(tmp);
    }
    seq.clear();
    string line;
    cin.ignore();
    getline(cin, line);
    //    cout << "line : " << line << endl;
    stringstream sin(line);
    string tmp;
    while (sin >> tmp, tmp[tmp.size() - 1] != '.')
      seq.push_back(tmp);
    seq.push_back(tmp.substr(0, tmp.size() - 1));

    vector<pair<int, string>> v;
    vector<int> num(21);
    FOR(it, seq)
    num[it->size()]++;
    FOR(it, seq) v.push_back(make_pair(num[it->size()], *it));
    sort(ALL(v));
    seq.clear();
    FOR(it, v) {
      seq.push_back(it->second);
      //      cout << it->second << endl;
    }

    mp = vector<char>(26);
    memo.clear();
    // cout << line << endl;
    ok = 0;
    dfs(0);
    if (ok == 1) {
      REP(i, line.size()) {
        if (line[i] == ' ' || line[i] == '.')
          cout << line[i];
        else
          cout << ans[line[i] - 'A'];
      }
      cout << endl;
    } else {
      cout << "-." << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

vector<string> str[21];
vector<string> seq;
char mp[26];
int n;
char ans[26];
int ok;

void dfs(int num) {
  // cout << num << endl;
  if (num == seq.size()) {
    ok++;
    REP(i, 26)
    ans[i] = mp[i];
    return;
  }
  bool change[26];
  string s = seq[num];
  if (ok > 1)
    return;
  FOR(it, str[s.size()]) {
    string t = *it;
    bool dame = 0;
    memset(change, 0, sizeof(change));
    REP(i, s.size()) {
      if (mp[s[i] - 'A'] == 0) {
        if (mp[t[i]]) {
          dame = 1;
          break;
        }
        mp[s[i] - 'A'] = t[i];
        mp[t[i] - 'A'] = s[i];
        change[s[i] - 'A'] = 1;
        change[t[i] - 'A'] = 1;
      } else if (mp[s[i] - 'A'] != t[i]) {
        dame = 1;
        break;
      }
    }
    // REP(i, 26) if (mp[i]) printf("(%c,%c)",i+'A', mp[i]);
    // cout << endl;
    if (!dame)
      dfs(num + 1);
    REP(i, 26)
    if (change[i])
      mp[i] = 0;
  }
}

int main() {
  while (cin >> n, n) {
    REP(i, 21) str[i].clear();
    REP(i, n) {
      string tmp;
      cin >> tmp;
      str[tmp.size()].push_back(tmp);
    }
    seq.clear();
    string line;
    cin.ignore();
    getline(cin, line);
    stringstream sin(line);
    string tmp;
    while (sin >> tmp, tmp[tmp.size() - 1] != '.')
      seq.push_back(tmp);
    seq.push_back(tmp.substr(0, tmp.size() - 1));

    vector<pair<int, string>> v;
    vector<int> num(21);
    FOR(it, seq)
    num[it->size()]++;
    FOR(it, seq) v.push_back(make_pair(num[it->size()], *it));
    sort(ALL(v));
    seq.clear();
    FOR(it, v) {
      seq.push_back(it->second);
      //      cout << it->second << endl;
    }

    memset(mp, 0, sizeof(mp));
    // cout << line << endl;
    ok = 0;
    dfs(0);
    if (ok == 1) {
      REP(i, line.size()) {
        if (line[i] == ' ' || line[i] == '.')
          cout << line[i];
        else
          cout << ans[line[i] - 'A'];
      }
      cout << endl;
    } else {
      cout << "-." << endl;
    }
  }
}