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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define ull unsigned long long int
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define min_(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

const int digit[8] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};

struct Alfa {
  char c;
  int num;
  int size;
  bool checked;
};

Alfa alfa[13];
char str[12][10];
int N, cnt;
bool calc() {
  int sum = 0;
  int l = 0;
  while (alfa[l].c != 0) {
    if (alfa[l].num == 0 && alfa[l].checked == true)
      return (false);
    // cout << alfa[l].c<<endl;
    sum += alfa[l].num * alfa[l].size;
    l++;
  }
  if (sum == 0) {

    /*	l = 0;
            while (alfa[l].c != 0){
                    cout << alfa[l].c << alfa[l].num;
                    l++;
            }
            cout << endl;*/
    return true;
  } else
    return false;
}

void set_num(int s) {
  int t;
  if (alfa[s].c == 0) {
    if (calc())
      cnt++;
    return;
  }
  rep(i, s, 10) {
    t = alfa[s].num;
    alfa[s].num = alfa[i].num;
    alfa[i].num = t;
    set_num(s + 1);
    t = alfa[s].num;
    alfa[s].num = alfa[i].num;
    alfa[i].num = t;
  }
}

int main(void) {
  while (true) {
    cin >> N;
    if (N == 0)
      return (0);
    memset(alfa, 0, sizeof(alfa));
    rep(i, 0, N) {
      cin >> str[i];
      int p = cnt = 0;
      while (str[i][p] != '\\0') {
        int k = 0;
        while (alfa[k].c != 0 && alfa[k].c != str[i][p])
          k++;
        alfa[k].c = str[i][p];
        if (p == 0 && str[i][1] != '\\0')
          alfa[k].checked = true;
        if (i < N - 1)
          alfa[k].size += digit[strlen(str[i]) - p - 1];
        else
          alfa[k].size -= digit[strlen(str[i]) - p - 1];
        p++;
      }
    }
    int l = 0;
    // while (alfa[l].c != 0){cout << alfa[l].c<<endl;rep(i, 0,
    // alfa[l].size)cout << alfa[l].data[i] << endl;l++;}
    rep(i, 0, 10) alfa[i].num = 9 - i;
    set_num(0);
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define ull unsigned long long int
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define min_(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

const int digit[8] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000};

struct Alfa {
  char c;
  int num;
  int data[100];
  int size;
  bool checked;
};

Alfa alfa[13];
char str[12][10];
int N, cnt;
bool calc() {
  int sum = 0;
  int l = 0;
  while (alfa[l].c != 0) {
    if (alfa[l].num == 0 && alfa[l].checked == true)
      return (false);
    // cout << alfa[l].c<<endl;
    rep(i, 0, alfa[l].size) // cout << alfa[l].data[i] << endl;
    {
      if (alfa[l].data[i] / 10 == N - 1)
        sum -= digit[alfa[l].data[i] % 10] * alfa[l].num;
      else
        sum += digit[alfa[l].data[i] % 10] * alfa[l].num;
    }

    l++;
  }
  if (sum == 0) {

    /*	l = 0;
            while (alfa[l].c != 0){
                    cout << alfa[l].c << alfa[l].num;
                    l++;
            }
            cout << endl;*/
    return true;
  } else
    return false;
}

void set_num(int s) {
  int t;
  if (alfa[s].c == 0) {
    if (calc())
      cnt++;
    return;
  }
  rep(i, s, 10) {
    t = alfa[s].num;
    alfa[s].num = alfa[i].num;
    alfa[i].num = t;
    set_num(s + 1);
    t = alfa[s].num;
    alfa[s].num = alfa[i].num;
    alfa[i].num = t;
  }
}

int main(void) {
  while (true) {
    cin >> N;
    if (N == 0)
      return (0);
    memset(alfa, 0, sizeof(alfa));
    rep(i, 0, N) {
      cin >> str[i];
      int p = cnt = 0;
      while (str[i][p] != '\\0') {
        int k = 0;
        while (alfa[k].c != 0 && alfa[k].c != str[i][p])
          k++;
        alfa[k].c = str[i][p];
        if (p == 0 && str[i][1] != '\\0')
          alfa[k].checked = true;
        alfa[k].data[alfa[k].size++] = i * 10 + strlen(str[i]) - p - 1;
        p++;
      }
    }
    int l = 0;
    // while (alfa[l].c != 0){cout << alfa[l].c<<endl;rep(i, 0,
    // alfa[l].size)cout << alfa[l].data[i] << endl;l++;}
    rep(i, 0, 10) alfa[i].num = 9 - i;
    set_num(0);
    cout << cnt << endl;
  }
}