#include <stdio.h>
#include <ctype.h>
#include <string.h>
int used = 0;
char str[32];
void eval()
{
char *p, *q;
int a,b,c;
if(p) {
if(p[1] == '0' || q[1] == '0') return;
if((a + b) == c){
}
}
if(p) {
if(p[1] == '0' || q[1] == '0') return;
if((a - b) == c){
}
}
}
void recur(int offs)
{
int c,n,i,old_used=used;
char old_str[32];
if((c = str[offs]) == 0) {
eval();
return;
}
recur(offs + 1);
return;
}
for(n=((offs == 0)?1:0); n<10; n++) {
if(!(used & (1 << n))) {
used |= (1 << n);
for(i=0; str[i]; i++) {
if(str[i] == c) str[i] = '0' + n;
}
recur(offs + 1);
used = old_used;
}
}
}
main()
{
strcpy(str
, "SEND+MORE=MONEY"); recur(0);
strcpy(str
, "WWWDOT-GOOGLE=DOTCOM"); recur(0);
return 0;
}
CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8Y3R5cGUuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKaW50IHVzZWQgPSAwOwpjaGFyIHN0clszMl07CnZvaWQgZXZhbCgpCnsKCWNoYXIJKnAsICpxOwoJaW50CWEsYixjOwoJYSA9IHN0cnRvbChzdHIsIDAsIDEwKTsKCXEgPSBzdHJjaHIoc3RyLCAnPScpOwljID0gc3RydG9sKHEgKyAxLCAwLCAxMCk7CglwID0gc3RyY2hyKHN0ciwgJysnKTsKCWlmKHApIHsKCQliID0gc3RydG9sKHAgKyAxLCAwLCAxMCk7CgkJaWYocFsxXSA9PSAnMCcgfHwgcVsxXSA9PSAnMCcpIHJldHVybjsKCQlpZigoYSArIGIpID09IGMpewoJCQlwcmludGYoIiAgJXNcbiIsIHN0cik7CgkJfQoJfQoKCXAgPSBzdHJjaHIoc3RyLCAnLScpOwoJaWYocCkgewoJCWIgPSBzdHJ0b2wocCArIDEsIDAsIDEwKTsKCQlpZihwWzFdID09ICcwJyB8fCBxWzFdID09ICcwJykgcmV0dXJuOwoJCWlmKChhIC0gYikgPT0gYyl7CgkJCXByaW50ZigiICAlc1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