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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#define M 333

int toint[M];
int used[10];
int firstflg[M];

int getvalue(string &str) {
  int ret = 0;
  rep(i, str.size()) {
    ret *= 10;
    ret += toint[str[i]];
  }
  return ret;
}

int wariate(vector<char> &mozi, vector<int> &time, vector<string> &input,
            int depth, int prevtime) {
  if (depth == mozi.size()) {
    int sum = 0;
    rep(i, input.size() - 1) { sum += getvalue(input[i]); }
    if (sum == getvalue(input[input.size() - 1])) {
      return 1;
    }
    return 0;
  }

  if (prevtime != time[depth]) {
    int val = 0;
    rep(i, input.size() - 1) { val += getvalue(input[i]); }
    int mod = 1;
    rep(i, time[depth]) mod *= 10;
    /*
    rep(i,mozi.size()){
            printf("(%c->%d) ",mozi[i],toint[mozi[i]]);
    }*/

    int val2 = getvalue(input[input.size() - 1]);
    if (val % mod != val2 % mod) {
      // printf("$$ 0 $$ (%d, %d) m(%d)\\n",val%mod, val2%mod, mod);
      return 0;
    }
    // printf("%% 1 %% (%d, %d) m(%d)\\n",val%mod, val2%mod, mod);
  }
  int ans = 0;
  rep(i, 10) {
    if (i == 0) {
      if (firstflg[mozi[depth]] == 1)
        continue;
    }
    if (used[i] == 0) {
      toint[mozi[depth]] = i;
      used[i] = 1;
      ans += wariate(mozi, time, input, depth + 1, time[depth]);
      used[i] = 0;
    }
  }

  return ans;
}

int main() {

A:;
  rep(i, M) toint[i] = firstflg[i] = 0;
  rep(i, 10) used[i] = 0;

  int n;
  cin >> n;
  if (n == 0)
    return 0;
  map<char, int> has;
  vector<char> mozi;
  vector<int> time;
  vector<string> input;
  rep(i, n) {
    string str;
    cin >> str;
    input.push_back(str);
    if (str.size() != 1)
      firstflg[str[0]] = 1;
  }

  int lenmax = 10;
  rep(i, lenmax) {
    rep(j, n) {
      if (i >= input[j].size())
        continue;
      char c = input[j][input[j].size() - i - 1];
      if (has[c] == 0) {
        mozi.push_back(c);
        time.push_back(i);
      }
      has[c] = 1;
    }
  }
  /*
  rep(i,mozi.size()){
          printf("(%c,%d) ",mozi[i],time[i]);
  }puts("");
  */

  int ans = wariate(mozi, time, input, 0, 0);
  printf("%d\\n", ans);
  goto A;
}

/*
3
SEND
MORE
MONEY
0

*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#define M 333

int toint[M];
int used[10];
int firstflg[M];

int getvalue(string &str) {
  int ret = 0;
  rep(i, str.size()) {
    ret *= 10;
    ret += toint[str[i]];
  }
  return ret;
}

int wariate(vector<char> &mozi, vector<string> &input, int depth) {
  if (depth == mozi.size()) {
    /*
    if(getvalue(input[input.size()-1])==40){
            rep(i,mozi.size()){
                    printf("(%c,%d) ",mozi[i],toint[mozi[i]]);
            }puts("");

            rep(i,input.size()){
                    cout<<input[i]<<" "<<getvalue(input[i])<<endl;
            }puts("");
    }*/

    rep(i, input.size()) {
      if (toint[input[i][0]] == 0 && input[i].size() != 1)
        return 0;
    }

    int sum = 0;
    rep(i, input.size() - 1) { sum += getvalue(input[i]); }
    if (sum == getvalue(input[input.size() - 1])) {
      return 1;
    }
    return 0;
  }

  int ans = 0;
  rep(i, 10) {
    if (i == 0) {
      if (firstflg[mozi[depth]] == 1)
        continue;
    }
    if (used[i] == 0) {
      toint[mozi[depth]] = i;
      used[i] = 1;
      ans += wariate(mozi, input, depth + 1);
      used[i] = 0;
    }
  }

  return ans;
}

int main() {

A:;
  rep(i, M) toint[i] = firstflg[i] = 0;
  rep(i, 10) used[i] = 0;

  int n;
  cin >> n;
  if (n == 0)
    return 0;
  map<char, int> has;
  vector<char> mozi;
  vector<string> input;
  rep(i, n) {
    string str;
    cin >> str;
    if (str.size() != 1)
      firstflg[str[0]] = 1;
    input.push_back(str);
    rep(j, str.size()) {
      if (has[str[j]] == 0)
        mozi.push_back(str[j]);
      has[str[j]] = 1;
    }
  }

  int ans = wariate(mozi, input, 0);
  printf("%d\\n", ans);
  goto A;
}