Problem p00896 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

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

vector<string> func(string input) {
  vector<string> ret;
  stringstream ss;
  ss << input;
  string s;
  vector<pair<int, string>> tmp;
  while (ss >> s) {
    if (s[s.size() - 1] == '.')
      s = s.substr(0, s.size() - 1);
    // ret[s.size()].push_back(s);
    set<char> se;
    rep(i, s.size()) { se.insert(s[i]); }
    tmp.push_back(make_pair(-(int)se.size(), s));
  }
  sort(tmp.begin(), tmp.end());
  rep(i, tmp.size()) { ret.push_back(tmp[i].second); }
  return ret;
}

vector<vector<int>> ans;
vector<vector<string>> vv;

bool check(string &a, string &b, vector<int> tik) {
  rep(i, a.size()) {
    int aa = a[i] - 'A';
    int bb = b[i] - 'A';
    if (tik[aa] == -1 && tik[bb] == -1) {
      tik[aa] = bb;
      tik[bb] = aa;
    } else if (tik[aa] == bb && tik[bb] == aa) {
      continue;
    } else {
      return false;
    }
  }
  return true;
}

void update(string &a, string &b, vector<int> &tik) {
  /*cout<<a<<" "<<b<<endl;
  rep(i,26) cout<<tik[i]<<" ";
  cout<<endl;
  */
  rep(i, a.size()) {
    int aa = a[i] - 'A';
    int bb = b[i] - 'A';
    if (tik[aa] == -1 && tik[bb] == -1) {
      tik[aa] = bb;
      tik[bb] = aa;
    }
  }
  /*
  rep(i,26) cout<<tik[i]<<" ";
  cout<<endl;
  */
}

void saiki(int x, vector<string> &w, vector<int> tik) {
  if (x == w.size()) {
    ans.push_back(tik);
    return;
  }
  string &s = w[x];
  rep(i, vv[s.size()].size()) {
    if (check(s, vv[s.size()][i], tik)) {
      vector<int> tmp = tik;
      update(s, vv[s.size()][i], tik);
      saiki(x + 1, w, tik);
      tik = tmp;
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    // cout<<n<<endl;
    vv = vector<vector<string>>(21);
    rep(i, n) {
      string s;
      cin >> s;
      vv[s.size()].push_back(s);
    }
    string input;
    getline(cin, input);
    getline(cin, input);
    // cout<<input<<endl;
    // return 0;
    /*
    vector<vector<string> > in = func(input);
    rep(i,21){
            sort(in[i].begin(), in[i].end());
            in[i].erase(unique(in[i].begin(), in[i].end()), in[i].end());
    }
    */
    vector<string> w = func(input);
    /*
    for(int i=20; i>=1; i--){
            rep(j,in[i].size()){
                    w.push_back(in[i][j]);
            }
    }
    */
    vector<int> tik(26, -1);
    ans.clear();
    saiki(0, w, tik);
    if (ans.size() > 1) {
      cout << "-." << endl;
    } else {
      vector<int> tik = ans[0];
      /*cout<<"ttt "<<endl;
      rep(i,26){
              cout<<t[i]<<" ";
      }
      cout<<endl;
      */
      rep(i, input.size()) {
        if (isupper(input[i])) {
          // cout<<input[i]<<" "<<tik[input[i]-'A']<<endl;;
          cout << char(tik[input[i] - 'A'] + 'A');
        } else {
          cout << input[i];
        }
      }
      cout << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

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

vector<string> func(string input) {
  vector<string> ret;
  stringstream ss;
  ss << input;
  string s;
  vector<pair<int, string>> tmp;
  while (ss >> s) {
    if (s[s.size() - 1] == '.')
      s = s.substr(0, s.size() - 1);
    // ret[s.size()].push_back(s);
    set<char> se;
    rep(i, s.size()) { se.insert(s[i]); }
    tmp.push_back(make_pair(-(int)se.size(), s));
  }
  sort(tmp.begin(), tmp.end());
  rep(i, tmp.size()) { ret.push_back(tmp[i].second); }
  return ret;
}

vector<vector<int>> ans;
vector<vector<string>> vv;

bool check(string &a, string &b, vector<int> tik) {
  rep(i, a.size()) {
    int aa = a[i] - 'A';
    int bb = b[i] - 'A';
    if (tik[aa] == -1 && tik[bb] == -1) {
      tik[aa] = bb;
      tik[bb] = aa;
    } else if (tik[aa] == bb && tik[bb] == aa) {
      continue;
    } else {
      return false;
    }
  }
  return true;
}

void update(string &a, string &b, vector<int> &tik) {
  /*cout<<a<<" "<<b<<endl;
  rep(i,26) cout<<tik[i]<<" ";
  cout<<endl;
  */
  rep(i, a.size()) {
    int aa = a[i] - 'A';
    int bb = b[i] - 'A';
    if (tik[aa] == -1 && tik[bb] == -1) {
      tik[aa] = bb;
      tik[bb] = aa;
    }
  }
  /*
  rep(i,26) cout<<tik[i]<<" ";
  cout<<endl;
  */
}

void saiki(int x, vector<string> &w, vector<int> tik) {
  if (x == w.size()) {
    ans.push_back(tik);
    return;
  }
  string &s = w[x];
  if (ans.size() >= 2)
    return;
  rep(i, vv[s.size()].size()) {
    if (check(s, vv[s.size()][i], tik)) {
      vector<int> tmp = tik;
      update(s, vv[s.size()][i], tik);
      saiki(x + 1, w, tik);
      tik = tmp;
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    // cout<<n<<endl;
    vv = vector<vector<string>>(21);
    rep(i, n) {
      string s;
      cin >> s;
      vv[s.size()].push_back(s);
    }
    string input;
    getline(cin, input);
    getline(cin, input);
    // cout<<input<<endl;
    // return 0;
    /*
    vector<vector<string> > in = func(input);
    rep(i,21){
            sort(in[i].begin(), in[i].end());
            in[i].erase(unique(in[i].begin(), in[i].end()), in[i].end());
    }
    */
    vector<string> w = func(input);
    /*
    for(int i=20; i>=1; i--){
            rep(j,in[i].size()){
                    w.push_back(in[i][j]);
            }
    }
    */
    vector<int> tik(26, -1);
    ans.clear();
    saiki(0, w, tik);
    if (ans.size() > 1) {
      cout << "-." << endl;
    } else {
      vector<int> tik = ans[0];
      /*cout<<"ttt "<<endl;
      rep(i,26){
              cout<<t[i]<<" ";
      }
      cout<<endl;
      */
      rep(i, input.size()) {
        if (isupper(input[i])) {
          // cout<<input[i]<<" "<<tik[input[i]-'A']<<endl;;
          cout << char(tik[input[i] - 'A'] + 'A');
        } else {
          cout << input[i];
        }
      }
      cout << endl;
    }
  }
}