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

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

int N, M, nonzero[256];
vector<char> list;
vector<string> s;
int conv[256], leftConv[256], rightConv[256];

int check() {
  int left = 0, right = 0;
  for (int i = 0; i < N; i++) {
    string num;
    for (int j = 0; j < s[i].size(); j++) {
      num.push_back(conv[s[i][j]] + '0');
    }
    // cout << num << endl;
    if (num[0] == '0' && num.size() != 1)
      return 0;
    else if (i == N - 1) {
      right = atoi(num.c_str());
    } else {
      left += atoi(num.c_str());
    }
  }
  // cout << left << " " << right << endl;
  if (right == left)
    return 1;
  else
    return 0;
}

int sum = 0;
int rec(int idx, int used) {

  /// cout << sum << endl;
  if (idx == M) {
    return sum == 0;
  }

  int ret = 0;
  for (int i = 0; i <= 9; i++) {
    if (nonzero[list[idx]] && i == 0)
      continue;
    if (used >> i & 1) {
      sum += (rightConv[list[idx]] * i) - (leftConv[list[idx]] * i);
      ret += rec(idx + 1, used & ~(1 << i));
      sum -= (rightConv[list[idx]] * i) - (leftConv[list[idx]] * i);
    }
  }

  return ret;
}

int main() {

  while (cin >> N, N) {
    memset(nonzero, 0, sizeof(nonzero));
    memset(conv, 0, sizeof(conv));
    memset(leftConv, 0, sizeof(leftConv));
    memset(rightConv, 0, sizeof(rightConv));
    for (int i = 0; i < N; i++) {
      string str;
      cin >> str;
      if (str.size() != 1) {
        nonzero[str[0]] = 1;
      }
      for (int j = 0; j < str.size(); j++) {
        list.push_back(str[j]);
        if (i < N - 1) {
          leftConv[str[j]] += pow(10, (str.size() - j - 1));
        } else {
          rightConv[str[j]] += pow(10, (str.size() - j - 1));
        }
      }
      s.push_back(str);
    }

    sort(list.begin(), list.end());
    list.erase(unique(list.begin(), list.end()), list.end());
    M = list.size();

    cout << rec(0, (1 << 10) - 1) << endl;
    list.clear();
    s.clear();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int N, M;
vector<char> list;
vector<string> s;
map<char, int> conv;

map<string, int> dp;
map<string, bool> dp1;

int check() {
  int left = 0, right = 0;
  for (int i = 0; i < N; i++) {
    string num;
    for (int j = 0; j < s[i].size(); j++) {
      num.push_back(conv[s[i][j]] + '0');
    }
    // cout << num << endl;
    if (num[0] == '0' && num.size() != 1)
      return 0;
    else if (i == N - 1) {
      right = atoi(num.c_str());
    } else {
      left += atoi(num.c_str());
    }
  }
  // cout << left << " " << right << endl;
  if (right == left)
    return 1;
  else
    return 0;
}

int rec(int idx, int used) {

  if (idx == M) {
    return check();
  }

  int ret = 0;
  for (int i = 0; i <= 9; i++) {
    if (used >> i & 1) {
      conv[list[idx]] = i;
      ret += rec(idx + 1, used & ~(1 << i));
    }
  }

  return ret;
}

int main() {

  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      string str;
      cin >> str;
      for (int i = 0; i < str.size(); i++) {
        list.push_back(str[i]);
      }
      s.push_back(str);
    }

    sort(list.begin(), list.end());
    list.erase(unique(list.begin(), list.end()), list.end());
    M = list.size();

    cout << rec(0, (1 << 10) - 1) << endl;
    list.clear();
    s.clear();
  }
}