#include<bits/stdc++.h>
using namespace std ;
int visit[10] , asgn[10] ;
map<char , int> key;
char text1[10] = "send" , text2[10] = "more" , ans[11] = "money" ;
int solve_puzzle(char *letters , int index) {
if(index == 9) {
int n1 = 0 , n2 = 0 , n3 = 0 ;
for(int i = 0 ; i < strlen(text1) ; i++)
n1 = n1*10 + asgn[key.find(text1[i])->second] ;
for(int i = 0 ; i < strlen(text2) ; i++)
n2 = n2*10 + asgn[key.find(text2[i])->second] ;
for(int i = 0 ; i < strlen(ans) ; i++)
n3 = n3*10 + asgn[key.find(ans[i])->second] ;
if(n1+n2 == n3) {
cout << n1 << " + " << n2 << " = " << n3 << endl ;
//return 1;
}
return 0 ;
}
for(int i = 0 ; i < 10 ; i ++) {
if(visit[i] == 0) {
asgn[index] = i ;
visit[i] = 1 ;
//int flag =
solve_puzzle(letters , index+1) ;
// if(flag) return 1 ;
visit[i] = 0 ;
}
}
return 0 ;
}
int main()
{
char letters[] = {'\0','s','e','n','d','m','o','r','y'};
for(int i = 1 ; i < 10 ; i ++)
key.insert( pair<char , int>(letters[i] , i ) );
if(!solve_puzzle(letters , 1))
cout << "Not Possible";
}
/*
SEND
+ MORE
-------
MONEY
9567
+ 1085
-------
10652
NOTE : For single solution remove the comments...
*/
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZCA7CgppbnQgdmlzaXRbMTBdICwgYXNnblsxMF0gOwptYXA8Y2hhciAsIGludD4ga2V5OwpjaGFyIHRleHQxWzEwXSA9ICJzZW5kIiAsIHRleHQyWzEwXSA9ICJtb3JlIiAsIGFuc1sxMV0gPSAibW9uZXkiIDsKCgppbnQgc29sdmVfcHV6emxlKGNoYXIgKmxldHRlcnMgLCBpbnQgaW5kZXgpIHsKCiAgICBpZihpbmRleCA9PSA5KSB7CiAgICAgICAgaW50IG4xID0gMCAsIG4yID0gMCAsIG4zID0gMCA7CgogICAgICAgIGZvcihpbnQgaSA9IDAgOyBpIDwgc3RybGVuKHRleHQxKSA7IGkrKykKICAgICAgICAgICAgbjEgPSBuMSoxMCArIGFzZ25ba2V5LmZpbmQodGV4dDFbaV0pLT5zZWNvbmRdIDsKCiAgICAgICAgZm9yKGludCBpID0gMCA7IGkgPCBzdHJsZW4odGV4dDIpIDsgaSsrKQogICAgICAgICAgICBuMiA9IG4yKjEwICsgYXNnbltrZXkuZmluZCh0ZXh0MltpXSktPnNlY29uZF0gOwoKCiAgICAgICAgZm9yKGludCBpID0gMCA7IGkgPCBzdHJsZW4oYW5zKSA7IGkrKykKICAgICAgICAgICAgbjMgPSBuMyoxMCArIGFzZ25ba2V5LmZpbmQoYW5zW2ldKS0+c2Vjb25kXSA7CgogICAgICAgIGlmKG4xK24yID09IG4zKSB7CiAgICAgICAgICAgIGNvdXQgPDwgbjEgPDwgIiArICIgPDwgbjIgPDwgIiA9ICIgPDwgbjMgPDwgZW5kbCA7CiAgICAgICAgICAgIC8vcmV0dXJuIDE7CiAgICAgICAgfQogICAgICAgIHJldHVybiAwIDsKICAgIH0KCiAgICBmb3IoaW50IGkgPSAwIDsgaSA8IDEwIDsgaSArKykgewogICAgICAgIGlmKHZpc2l0W2ldID09IDApIHsKICAgICAgICAgICAgYXNnbltpbmRleF0gPSBpIDsKICAgICAgICAgICAgdmlzaXRbaV0gPSAxIDsKICAgICAgICAgICAgLy9pbnQgZmxhZyA9CiAgICAgICAgICAgIHNvbHZlX3B1enpsZShsZXR0ZXJzICwgaW5kZXgrMSkgOwogICAgICAgICAgICAvLyBpZihmbGFnKSByZXR1cm4gMSA7CiAgICAgICAgICAgIHZpc2l0W2ldID0gMCA7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDAgOwp9CgppbnQgbWFpbigpCnsKICAgIGNoYXIgbGV0dGVyc1tdID0geydcMCcsJ3MnLCdlJywnbicsJ2QnLCdtJywnbycsJ3InLCd5J307CiAgICBmb3IoaW50IGkgPSAxIDsgaSA8IDEwIDsgaSArKykKICAgICAgICBrZXkuaW5zZXJ0KCBwYWlyPGNoYXIgLCBpbnQ+KGxldHRlcnNbaV0gLCBpICkgKTsKCiAgICBpZighc29sdmVfcHV6emxlKGxldHRlcnMgLCAxKSkKICAgICAgICBjb3V0IDw8ICJOb3QgUG9zc2libGUiOwp9CgovKgogIFNFTkQKKyBNT1JFCi0tLS0tLS0KIE1PTkVZCgogIDk1NjcKKyAxMDg1Ci0tLS0tLS0KIDEwNjUyCgpOT1RFIDogRm9yIHNpbmdsZSBzb2x1dGlvbiByZW1vdmUgdGhlIGNvbW1lbnRzLi4uCgoqLwo=