题目链接:
http://acm.uestc.edu.cn/#/problem/show/1085
题意:
题解:
因为进位,从低位搜
代码:
http://www.cnblogs.com/qscqesze/p/4489781.html
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;
int n,ma,tmp,ans;
string s[30];
char k[100];
int tp[30],ans1[30],ans2[30],H[30],mp[30],vis[10];
bool check(){
ans1[ma] = 0;
for(int i=ma-1; i>=0; i--){
ans1[i] = 0;
ans2[i] = 0;
for(int j=0; j<n-1; j++){
if(H[s[j][i]-'A'] == -1) return 0;
ans1[i] += H[s[j][i]-'A'];
}
if(H[s[n-1][i]-'A'] == -1) return 0;
ans2[i] = H[s[n-1][i]-'A'];
ans1[i] += (ans1[i+1]/10);
if((ans1[i])%10 != ans2[i]) return 1;
}
if(ans1[0] > 9) return 1;
return 0;
}
void dfs(int x){
if(x==tmp) {
ans++;
return ;
}
for(int i=0; i<=9; i++){
if(i==0 && tp[k[x]-'A']) continue;
if(vis[i]) continue;
vis[i] = 1;
H[k[x]-'A'] = i;
if(!check()) dfs(x+1);
H[k[x]-'A'] = -1;
vis[i] = 0;
}
}
int main(){
while(cin >> n){
for(int i=0;i<30;i++)
mp[i]=tp[i]=H[i]=0;
ma=0;
for(int i=0; i<n; i++){
cin >> s[i];
ma = max(ma,(int)s[i].size());
}
for(int i=0; i<n; i++)
tp[s[i][0]-'A'] = 1;
for(int i=0; i<n; i++){
int len = ma-s[i].size();
for(int j=0; j<len; j++){
s[i] = '[' + s[i];
}
}
tmp=0;
ans=0;
for(int i=ma-1;i>=0;i--){
for(int j=0;j<n;j++){
if(mp[s[j][i]-'A']) continue;
if(s[j][i]=='[') continue;
k[tmp++]=s[j][i];
mp[s[j][i]-'A']=1;
}
}
for(int i=0; i<tmp; i++)
H[k[i]-'A'] = -1;
// H['['-'A'] = 0;
dfs(0);
cout << ans << endl;
}
return 0;
}