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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int n;
string str[12];
bool zero[10];
int k[10];
char c[10];
int alpha[26];
int num;
bool use[10];

class verval {
public:
  int sum, comb;
  verval(void){};
  verval(int a, int b) {
    sum = a;
    comb = b;
  };
  const bool operator<(const verval x) const { return sum < x.sum; };
};

vector<verval> v[2];

void make(int from, int to, int sum, int type) {
  if (from == to) {
    int tmp = 0;
    for (int i = 0; i < 10; i++)
      if (use[i])
        tmp += (1 << i);

    v[type].push_back(verval(sum, tmp));
    return;
  }
  for (int i = 0; i < 10; i++) {
    if (!i && zero[from])
      continue;
    if (!use[i]) {
      use[i] = true;
      make(from + 1, to, sum + i * k[from], type);
      use[i] = false;
    }
  }
}
int main() {
  for (;;) {
    cin >> n;
    if (!n)
      break;

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

    num = 0;
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      for (int j = 0; j < (int)str[i].size(); j++) {
        if (alpha[str[i][j] - 'A'] < 0) {
          alpha[str[i][j] - 'A'] = num;
          c[num] = str[i][j];
          num++;
        }
      }
    }

    for (int i = 0; i < num; i++) {
      zero[i] = false;
      k[i] = 0;
    }

    for (int i = 0; i < n; i++) {
      int tmp = 1;
      for (int j = (int)str[i].size() - 1; j >= 0; j--) {
        if (str[i].size() > 1 && !j)
          zero[alpha[str[i][j] - 'A']] = true;
        if (i != n - 1)
          k[alpha[str[i][j] - 'A']] += tmp;
        else
          k[alpha[str[i][j] - 'A']] -= tmp;
        tmp *= 10;
      }
    }
    for (int i = 0; i < 10; i++)
      use[i] = false;
    v[0].clear();
    make(0, num / 2, 0, 0);
    sort(v[0].begin(), v[0].end());

    for (int i = 0; i < 10; i++)
      use[i] = false;
    v[1].clear();
    make(num / 2, num, 0, 1);
    sort(v[1].begin(), v[1].end());

    int ans = 0;
    for (int i = 0; i < (int)v[0].size(); i++) {
      int l = 0, r = (int)v[1].size() - 1;
      int search = -1 * v[0][i].sum;
      while (l + 1 < r) {
        int mid = (l + r) / 2;
        if (v[1][mid].sum < search)
          l = mid;
        else
          r = mid;
      }
      while (r < (int)v[1].size()) {
        if (v[1][r].sum != search)
          break;

        if (!(v[0][i].comb & v[1][r].comb))
          ans++;
        r++;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

string alpha;
int num[10];
string str[12];
int use[26];
int check[10];
bool usenum[10];
int ans, n, sum, c, i, j;
bool prefix[10];
int k[10];

void rec(int x) {
  int sum = 0;
  if (x == (int)alpha.size()) {
    for (int i = 0; i < x; i++)
      sum += k[i] * num[i];
    if (!sum)
      ans++;
    return;
  }
  for (int i = 0; i < 10; i++) {
    if (prefix[x] && !i)
      continue;
    if (!usenum[i]) {
      usenum[i] = true;
      num[x] = i;
      rec(x + 1);
      usenum[i] = false;
    }
  }
}

int main() {
  int cnt;
  int ten[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};

  while (1) {
    cin >> n;
    if (!n)
      break;

    for (i = 0; i < n; i++)
      cin >> str[i];

    for (i = 0; i < 10; i++)
      prefix[i] = false;
    for (i = 0; i < 10; i++)
      k[i] = 0;
    for (i = 0; i < 26; i++)
      use[i] = -1;
    cnt = 0;
    alpha.clear();
    for (i = 0; i < n; i++) {
      for (j = 0; j < (int)str[i].size(); j++) {
        if (use[str[i][j] - 'A'] < 0) {
          use[str[i][j] - 'A'] = cnt;
          alpha.push_back(str[i][j]);
          if ((int)str[i].size() > 1 && !j)
            prefix[cnt] = true;
          cnt++;
        }
        if (i == n - 1)
          k[use[str[i][j] - 'A']] -= ten[(int)str[i].size() - 1 - j];
        else
          k[use[str[i][j] - 'A']] += ten[(int)str[i].size() - 1 - j];
      }
    }

    ans = 0;
    for (i = 0; i < 10; i++)
      usenum[i] = false;

    rec(0);
    cout << ans << endl;
  }
}