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

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, a, b) (long long i = a; i < b; i++)

int beki(int n) {
  int ans = 1;
  for (int i = 0; i < n; i++)
    ans *= 10;
  return ans;
}

using namespace std;

struct POINT {
  int x;
  int y;
  POINT(int X, int Y) {
    x = X;
    y = Y;
  }
};

unsigned long num;
string str[12];
int kirei[10][4] = {0};
int kr;
bool suji[10] = {0};
int nakaji = 0;

int cul() {
  int sum = 0;
  for (int i = 0; i < kr; i++)
    sum += (kirei[i][3] * kirei[i][2]);
  if (!sum) {
    return 1;
  } else
    return 0;
}

void saiki(int itr) {
  for (int x = 0; x < 10; x++) {
    if (!suji[x] && !(x == 0 && kirei[itr][1] == true)) {

      int tmp = kirei[itr][2];
      kirei[itr][2] = x;
      suji[x] = true;

      if (itr + 1 == kr)
        nakaji += cul();
      else
        saiki(itr + 1);

      kirei[itr][2] = tmp;
      suji[x] = false;
    }
  }
}

int main(void) {

  while (1) {

    bool abc[26][2] = {0};
    kr = 0;

    nakaji = 0;

    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 4; j++) {
        kirei[i][j] = 0;
      }
      suji[i] = 0;
    }

    cin >> num;
    if (num == 0)
      break;

    for (int i = 0; i < num; i++) {
      cin >> str[i];
      if (str[i].length() != 1)
        abc[str[i][0] - 'A'][1] = true;
      for (int j = 0; j < str[i].length(); j++) {
        abc[str[i][j] - 'A'][0] = true;
      }
    }

    int kei[26] = {0};
    for (int i = 0; i < num - 1; i++) {
      int ju = 1;
      for (int j = str[i].length() - 1; j >= 0; j--) {
        kei[str[i][j] - 'A'] += ju;
        ju *= 10;
      }
    }
    int i = num - 1;
    int ju = 1;
    for (int j = str[i].length() - 1; j >= 0; j--) {
      kei[str[i][j] - 'A'] -= ju;
      ju *= 10;
    }

    for (int i = 0; i < 26; i++) {
      if (abc[i][0]) {
        kirei[kr][0] = i + 'A';
        kirei[kr][1] = abc[i][1];
        kirei[kr++][3] = kei[i];
      }
    }

    saiki(0);

    cout << nakaji << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, a, b) (long long i = a; i < b; i++)

using namespace std;

struct POINT {
  int x;
  int y;
  POINT(int X, int Y) {
    x = X;
    y = Y;
  }
};

unsigned long num;
string str[12];
int kirei[10][3] = {0};
int kr;
bool suji[10] = {0};
int nakaji = 0;
int mm[26] = {0};
int beki[8] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};

int cul() {
  int number[12] = {0};
  int sum = 0;

  for (int i = 0; i < num - 1; i++) {
    int len = str[i].length();
    for (int j = len; j > 0; j--) {
      number[i] += ((beki[j - 1]) * kirei[mm[str[i][len - j] - 'A']][2]);
    }
    sum += number[i];
  }
  int len = str[num - 1].length();
  for (int j = len; j > 0; j--) {
    number[num - 1] +=
        ((beki[j - 1]) * kirei[mm[str[num - 1][len - j] - 'A']][2]);
  }
  return (sum == number[num - 1]);
}

void saiki(int itr) {
  for (int x = 0; x < 10; x++) {
    if (!suji[x] && (x || !kirei[itr][1])) {

      int tmp = kirei[itr][2];
      kirei[itr][2] = x;
      suji[x] = true;

      if (itr + 1 == kr)
        nakaji += cul();
      else
        saiki(itr + 1);

      kirei[itr][2] = tmp;
      suji[x] = false;
    }
  }
}

int main(void) {

  while (1) {

    bool abc[26][2] = {0};
    kr = 0;

    nakaji = 0;

    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 3; j++) {
        kirei[i][j] = 0;
      }
      suji[i] = 0;
    }

    cin >> num;
    if (num == 0)
      break;

    for (int i = 0; i < num; i++) {
      cin >> str[i];
      if (str[i].length() != 1)
        abc[str[i][0] - 'A'][1] = true;
      for (int j = 0; j < str[i].length(); j++) {
        abc[str[i][j] - 'A'][0] = true;
      }
    }

    for (int i = 0; i < 26; i++) {
      if (abc[i][0]) {
        kirei[kr][0] = i + 'A';
        kirei[kr++][1] = abc[i][1];
        mm[i] = kr - 1;
      }
    }

    saiki(0);

    cout << nakaji << endl;
  }

  return 0;
}