Language: C++

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

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

struct Alphabet
{
	int keisu;
	char mozi;
	bool sento;
};
typedef struct Alphabet alphabet;

void create(string str, vector<alphabet>& alp, int sign)
{
	int len = str.size();
	int idx = len-1;
	vector<alphabet>::iterator it;

	for(int i=0; i < len; i++) {
		bool hit = false;
		for(it = alp.begin(); it != alp.end(); it++) {
			if(str[i] == (*it).mozi) {
				hit = true;
				(*it).keisu += (g_keta[idx] * sign);
				break;
			}
		}
		if(!hit) {
			alphabet tmp;
			tmp.keisu = g_keta[idx] * sign;
			tmp.mozi = str[i];
			if(len > 1) tmp.sento = (i)? false : true;
			else tmp.sento = false;
			alp.push_back( tmp );
		}
		idx--;
	}
}

bool g_use[10] = { false };
bool g_lock[26] = { false };
int g_num[26] = { 0 };
int g_cnt;

void search(vector<alphabet>& alp, int cnt, int sum)
{
	if(sum > 0) return;
	if(alp.size() == cnt){
		if(!sum) g_cnt++;
		return;
	}
	int idx = alp[cnt].mozi-'A';
	if(g_lock[idx]) {
		int tmp = alp[cnt].keisu * g_num[idx];
		search(alp, cnt+1, sum+tmp);
		return;
	}
	for(int i=0; i < 10; i++) {
		if(g_use[i]) continue;
		if(!i && alp[cnt].sento) continue;
		g_use[i] = true;
		int tmp = alp[cnt].keisu * i;
		search(alp, cnt+1, sum+tmp);
		g_use[i] = false;
	}
}

vector<alphabet> ans;
bool g_hit;
int g_limit;

void solve(vector<alphabet>& alp, int cnt, int sum)
{
	if(alp.size() == cnt) {
		search(ans, 0, sum);
		return;
	}
	for(int i=0; i < 10; i++) {
		if(g_use[i]) continue;
		if(alp[cnt].sento) {
			if(!i) continue;
			else if(g_hit && i > 4) break;
		}
		int idx = alp[cnt].mozi-'A';
		int tmp = alp[cnt].keisu * i;
		g_use[i] = true;
		g_lock[idx] = true;
		g_num[idx] = i;
		solve(alp, cnt+1, sum+tmp);
		g_lock[idx] = false;
		g_use[i] = false;
	}
}

int main()
{
	int n;
	int maxlen = 0;
	while(cin >> n, n)
	{
		g_cnt = g_limit = 0;
		vector<alphabet> alp; ans.clear();
		bool hit = g_hit = false;
		for(int i=0; i < n; i++) {
			string str;
			cin >> str;
			if(i == n-1) {
				if(str.size() < maxlen) hit = true;
				else if(str.size() > maxlen) {
					g_hit = true;
					for(int i=9; i > 9-n; i--) {
						g_limit += i;
					}
					g_limit /= 10;
				}
				create(str, alp, -1);
			}
			else {
				maxlen = max(maxlen, str.size());
				create(str, ans, 1);
			}
		}
		if(hit) {
			cout << 0 << endl;
			continue;
		}
		solve(alp, 0, 0);
		cout << g_cnt << endl;
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:123:45: error: no matching function for call to 'max(int&, std::__cxx11::basic_string<char>::size_type)'
  123 |                                 maxlen = max(maxlen, str.size());
      |                                          ~~~^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:123:45: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'})
  123 |                                 maxlen = max(maxlen, str.size());
      |                                          ~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:123:45: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  123 |                                 maxlen = max(maxlen, str.size());
      |                                          ~~~^~~~~~~~~~~~~~~~~~~~