#include <stdio.h>#include <string.h>#include <algorithm>using namespace STD;intuse[ -],a[ -],flag,b[ -],m,p[ -];Chars[1024x768];voidDfsintx) {inti,a1,a2,a3,t,l;if(flag)return;if(x==m) {l=strlen(s); a1=0; A2=0; a3=0; for(i=0; i<l; i++) {if(s[i]==' + ') {t=1; Break; }Else if(s[i]=='-') {t=-1; Break; }if(s[i]>=' 0 '&&s[i]<=' 9 ') {a1=a1*Ten+s[i]-' 0 '; }Else if(s[i]<=' Z '&&s[i]>=' A ') {a1=a1*Ten+a[s[i]-' A ']; } } for(i=i+1; s[i]!=' = '; i++) {if(s[i]>=' 0 '&&s[i]<=' 9 ') {a2=a2*Ten+s[i]-' 0 '; }Else if(s[i]<=' Z '&&s[i]>=' A ') {a2=a2*Ten+a[s[i]-' A ']; } } for(i=i+1; i<l; i++) {if(s[i]>=' 0 '&&s[i]<=' 9 ') {a3=a3*Ten+s[i]-' 0 '; }Else if(s[i]<=' Z '&&s[i]>=' A ') {a3=a3*Ten+a[s[i]-' A ']; } }if(t==1&&A1+A2==A3) flag=1;Else if(t==-1&&A1-A2==A3) flag=1;return; } for(i=0; i<=9; i++) {if(p[i]==1)Continue; p[i]=1; A[b[x]]=i; DFS (x+1);if(flag)return; p[i]=0; }return;}intMain () {intI,j; while(~scanf('%s ', s)) {intlen=strlen(s);memset(Use,0,sizeof(use));memset(P,0,sizeof(p)); flag=0; for(i=0; i<len; i++) {if(s[i]<=' Z '&&s[i]>=' A ') {use[s[i]-' A ']=1; } } for(i=0, m=0;i< -; i++)if(use[i]==1) B[m++]=i; Dfs0); for(i=0; i<m; i++)printf("%c%d\n", b[i]+' A ', A[b[i]]); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3548 Restoring the digits (DFS)