#include #include int inputSize; bool solutionPrinted; int input[1000]; int output[1000]; int minimumSolution; enum commands {add, sub, dup, mul, div}; int solutionN; struct solution { int depth; commands sol[11]; }; solution solutions[10000]; char *getString(int len, commands cmds[]) { char buf[1000]; char *off =buf; for(int i=0; i 30000 || simstack[stackptr] < -30000 ) { wrong = true; continuebacktrack = false; } } if( stackptr != 0 || output[i] != simstack[0] ) wrong = true; } if(!wrong) { // a correct solution if(depth==0) { solutionPrinted = true; cout << "Empty sequence" << endl << endl; } else { // merken if( minimumSolution >= depth) { solutions[solutionN].depth = depth; for(int i=0; i 1) { cmds[depth] = add; backtrack(cmds, inpStackLen-1, depth+1); cmds[depth] = sub; backtrack(cmds, inpStackLen-1, depth+1); cmds[depth] = mul; backtrack(cmds, inpStackLen-1, depth+1); cmds[depth] = div; backtrack(cmds, inpStackLen-1, depth+1); } } } } int main() { ifstream in("optimal.in"); int kase=1; in >> inputSize; while( inputSize > 0 ) { solutionPrinted = false; for(int i=0; i> input[i]; for(int i=0; i> output[i]; cout << "Program " << kase << endl; commands cmds[100]; solutionN = 0; minimumSolution = 100; backtrack(cmds, 1, 0); if(!solutionPrinted) { if(solutionN == 0) cout << "Impossible" << endl << endl; else { // more than 1 sol // cout<<"soltu9ijo " << solutionN << endl; int solN = 0; char *solstr[10000]; char *minstr; for(int i=0; i< solutionN ; i++) { if( solutions[i].depth == minimumSolution ) { solstr[solN] = getString(solutions[i].depth, solutions[i].sol); solN++; } // cout << "depth " << solutions[i].depth << " " << minimumSolution << endl; } minstr=solstr[0]; for(int i=0; i< solN; i++) { if( strcmp(minstr, solstr[i]) >= 0) minstr=solstr[i]; } cout << minstr << endl << endl; } } in >> inputSize; kase++; } return 0; }