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

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
string str[30];
char in[100];
string q[100];
int pts[100];
int conv[30];
int to[100];
int ans[100];
int sz;
int n;
int dfs() {
  int at = -1;
  int len = 0;
  int pt = 0;
  for (int i = 0; i < sz; i++) {
    if (!~to[i]) {
      if (pt < pts[i]) {
        at = i;
        len = q[i].size();
        pt = pts[i];
      }
    }
  }
  if (!len) {
    for (int i = 0; i < sz; i++)
      ans[i] = to[i];
    return 1;
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (str[i].size() != len)
      continue;
    bool ok = true;
    for (int j = 0; j < str[i].size(); j++) {
      if ((~conv[str[i][j] - 'A'] && conv[str[i][j] - 'A'] != q[at][j] - 'A') ||
          (~conv[q[at][j] - 'A'] && conv[q[at][j] - 'A'] != str[i][j] - 'A'))
        ok = false;
    }
    if (ok) {
      int rev[26];
      for (int j = 0; j < 26; j++)
        rev[j] = conv[j];
      to[at] = i;
      for (int j = 0; j < str[i].size(); j++) {
        if (!~conv[str[i][j] - 'A']) {
          conv[str[i][j] - 'A'] = q[at][j] - 'A';
        } else if (conv[str[i][j] - 'A'] != q[at][j] - 'A')
          ok = false;
        if (!~conv[q[at][j] - 'A']) {
          conv[q[at][j] - 'A'] = str[i][j] - 'A';
        } else if (conv[q[at][j] - 'A'] != str[i][j] - 'A')
          ok = false;
      }
      if (ok)
        ret += dfs();
      to[at] = -1;
      for (int j = 0; j < 26; j++)
        conv[j] = rev[j];
    }
    if (ret > 1)
      return 2;
  }
  return ret;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    n = a;
    for (int i = 0; i < a; i++) {
      scanf("%s", in);
      str[i] = in;
    }
    sz = 0;
    while (1) {
      scanf("%s", in);
      if (in[strlen(in) - 1] == '.') {
        in[strlen(in) - 1] = 0;
        q[sz++] = in;
        break;
      }
      q[sz++] = in;
    }
    for (int i = 0; i < 26; i++)
      conv[i] = -1;
    for (int i = 0; i < sz; i++)
      to[i] = -1;
    for (int i = 0; i < sz; i++)
      pts[i] = 0;
    for (int i = 0; i < 26; i++) {
      for (int j = 0; j < sz; j++) {
        bool ok = false;
        for (int k = 0; k < q[j].size(); k++) {
          if (q[j][k] == 'A' + i)
            ok = true;
        }
        if (ok)
          pts[j]++;
      }
    }
    int ret = dfs();
    if (ret > 1)
      printf("-.\\n");
    else {
      for (int i = 0; i < sz; i++) {
        if (i)
          printf(" ");
        printf("%s", str[ans[i]].c_str());
      }
      printf(".\\n");
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
string str[30];
char in[100];
string q[100];
int conv[30];
int to[100];
int ans[100];
int sz;
int n;
int dfs() {
  int at = -1;
  int len = 0;
  for (int i = 0; i < sz; i++) {
    if (!~to[i]) {
      if (len < q[i].size()) {
        at = i;
        len = q[i].size();
      }
    }
  }
  if (!len) {
    for (int i = 0; i < sz; i++)
      ans[i] = to[i];
    return 1;
  }
  int ret = 0;
  for (int i = 0; i < n; i++) {
    if (str[i].size() != len)
      continue;
    bool ok = true;
    for (int j = 0; j < str[i].size(); j++) {
      if ((~conv[str[i][j] - 'A'] && conv[str[i][j] - 'A'] != q[at][j] - 'A') ||
          (~conv[q[at][j] - 'A'] && conv[q[at][j] - 'A'] != str[i][j] - 'A'))
        ok = false;
    }
    if (ok) {
      int rev[26];
      for (int j = 0; j < 26; j++)
        rev[j] = conv[j];
      to[at] = i;
      for (int j = 0; j < str[i].size(); j++) {
        if (!~conv[str[i][j] - 'A']) {
          conv[str[i][j] - 'A'] = q[at][j] - 'A';
        } else if (conv[str[i][j] - 'A'] != q[at][j] - 'A')
          ok = false;
        if (!~conv[q[at][j] - 'A']) {
          conv[q[at][j] - 'A'] = str[i][j] - 'A';
        } else if (conv[q[at][j] - 'A'] != str[i][j] - 'A')
          ok = false;
      }
      if (ok)
        ret += dfs();
      to[at] = -1;
      for (int j = 0; j < 26; j++)
        conv[j] = rev[j];
    }
    if (ret > 1)
      return 2;
  }
  return ret;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    n = a;
    for (int i = 0; i < a; i++) {
      scanf("%s", in);
      str[i] = in;
    }
    sz = 0;
    while (1) {
      scanf("%s", in);
      if (in[strlen(in) - 1] == '.') {
        in[strlen(in) - 1] = 0;
        q[sz++] = in;
        break;
      }
      q[sz++] = in;
    }
    for (int i = 0; i < 26; i++)
      conv[i] = -1;
    for (int i = 0; i < sz; i++)
      to[i] = -1;
    int ret = dfs();
    if (ret > 1)
      printf("-.\\n");
    else {
      for (int i = 0; i < sz; i++) {
        if (i)
          printf(" ");
        printf("%s", str[ans[i]].c_str());
      }
      printf(".\\n");
    }
  }
}