md.etemad
BAN USER#include <iostream>
using namespace std;
char Codes[27];
void Print(const string& code, string result, int pos)
{
if(pos == code.length())
{
cout << result << endl;
return;
}
Print(code, result + Codes[code[pos] - '0'], pos+1);
if(pos == (code.length()-1))
return;
short index = (code[pos]-'0')*10+code[pos+1]-'0';
if(index <= 26)
Print(code, result + Codes[index], pos+2);
}
int main()
{
for (short i = 1; i <= 26 ; i++)
Codes[i] = 'A' + i - 1;
string code = "121345161718";
Print(code, "", 0);
return 0;
}
int main()
{
int firstK = 0, secondK = 0, thirdK= 0, fourthK = 0, tryNo = 0;
clock_t start = clock();
for(int h = 0 ; h < 10 ; h++)
{
for(int t = 0 ; t < 10 ; t++)
{
if(t==h || ((2*h + 2*t) % 10) != h)
continue;
firstK = (2*h + 2*t) / 10;
for(int s = 0 ; s < 10 ; s++)
{
if(s==t || s==h)
continue;
if(((2*s + 2*t + firstK) % 10) != t)
continue;
secondK = (2*s + 2*t + firstK) / 10;
for(int r = 0 ; r < 10 ; r++)
{
if(r==t || r==h || r==s)
continue;
for(int u = 0 ; u < 10 ; u++)
{
if(u==t || u==h || u==s || u==r)
continue;
for(int a = 0 ; a < 10 ; a++)
{
if(a==t || a==h || a==s || a==r || a==u)
continue;
for(int e = 0 ; e < 10 ; e++)
{
if(e==t || e==h || e==s || e==r || e==u || e==a)
continue;
if(((r+u+a+e+secondK) % 10) != r)
continue;
thirdK = (r+u+a+e+secondK) / 10;
for(int o = 0 ; o < 10 ; o++)
{
if(o==t || o==h || o==s || o==r || o==u || o==a || o==e)
continue;
for(int w = 0 ; w < 10 ; w++)
{
if(w==t || w==h || w==s || w==r || w==u || w==a || w==e || w==o)
continue;
if(((2*o + e + w + thirdK) % 10) != a)
continue;
fourthK = (2*o + e + w + thirdK) / 10;
for(int n = 0 ; n < 10 ; n++)
{
if(n==t || n==h || n==s || n==r || n==u || n==a || n==e || n==o || n==w)
continue;
if((n+s+fourthK)!= e)
continue;
cout << "Solution: " << tryNo++ << endl;
cout << n << o << r << t << h << endl;
cout << s << o << u << t << h << endl;
cout << " " << e << a << s << t << endl;
cout << " " << w << e << s << t << endl;
cout << e << a << r << t << h << endl << endl;
}
}
}
}
}
}
}
}
}
}
clock_t end = clock();
cout << "time: " << double(end-start)/CLOCKS_PER_SEC;
return 0;
}
We can put the accessed items in a hash table for fast access. The total cost would be O(n log n).
- md.etemad October 13, 2017