Problem Description
=== Op tech briefing, 2002/11/02 CST ===
"The item is locked in a Klein safe behind a painting in the second-floor library. klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World War II. fortunately old Brumbaugh from research knew Klein's secrets and wrote them down before he died. A Klein safe has two distinguishing features: a combination lock that uses letters instead of numbers, and an engraved quotation on the door. A Klein quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentences, and mentions one or more numbers. five of the uppercase letters form the combination that opens the safe. by combining the digits from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified .) to find the combination you must select five letters v, w, x, y, and z that satisfy the following equation, where each letter is replaced by its ordinal position in the alphabet (A = 1, B = 2 ,..., Z = 26 ). the combination is then vwxyz. if there is more than one solution then the combination is the one that is lexicographically greatest, I. e ., the one that wowould appear last in a dictionary."
V-w ^ 2 + x ^ 3-y ^ 4 + z ^ 5 = target
"For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6-9 ^ 2 + 5 ^ 3-3 ^ 4 + 2 ^ 5 = 1. there are actually several solutions in this case, and the combination turns out to be LKEBA. klein thought it was safe to encode the combination within the engraving, because it cocould take months of effort to try all the possibilities even if you knew the secret. but of course computers didn't exist then."
=== Op tech directive, computer division, 2002/11/02 CST ====
"Develop a program to find Klein combinations in preparation for field deployment. use standard test methodology as per departmental regulations. input consists of one or more lines containing a positive integer target less than twelve million, a space, then at least five and at most twelve distinct uppercase letters. the last line will contain in a target of zero and the letters END; this signals the end of the input. for each line output the Klein combination, break ties with lexicographic order, or 'no solution' if there is no correct combination. use the exact format shown below."
Sample Input
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END
Sample Output
LKEBA
YOXUZ
GHOST
No solution
# Include <stdio. h> # include <string. h> # include <math. h> int vist [1000], flog, len, target, s [10], top, loction [10]; int m [1000]; int cmp () {int v, w, x, y, z; // The precision of converting to int Type v = s [1]; w = (int) (pow (s [2] * 1.0, 2.0) + 0.5); x = (int) (pow (s [3] * 1.0, 3.0) + 0.5 ); y = (int) (pow (s [4] * 1.0, 4.0) + 0.5); z = (int) (pow (s [5] * 1.0, 5.0) + 0.5); if (v-w + x-y + z = target) return 1; // returns 1 return 0;} void DFS (int n) {int I; s [++ top] = m [n]; // stack loction [top] = n; // Note the location vist [n] of the green m [n] = 1; // indicates that the n position is accessed if (top = 5) // if they are equal, you can compare flog = cmp (); if (flog) // true, you do not need to return; if (top <5) // The number can be placed only when the number of stacks is smaller than 5. {for (I = 1; I <= len; I ++) if (vist [I] = 0) {DFS (I); if (flog) // used to end the DFS return;} vist [n] = 0; top --; // return} int main () {char str [1000], tem; int I, j, e; while (scanf ("% d % s", & target, str)> 0) {if (target = 0 & strcmp (str, "END") = 0) break; flog = 0; len = strlen (str); for (I = 0; I <len; I ++) // In the lexicographically ascending order {e = I; for (j = I + 1; j <len; j ++) if (str [e] <str [j]) e = j; tem = str [I]; str [I] = str [e]; str [e] = tem;} for (I = 0; I <len; I ++) // change to the number m [I + 1] = str [I]-'A' + 1; for (I = 1; I <= len; I ++) // starts with the number of I: {memset (vist, 0, sizeof (vist); top = 0; DFS (I); if (flog) break ;} if (flog) {for (I = 1; I <= top; I ++) printf ("% c", str [loction [I]-1]);} else printf ("no solution"); printf ("\ n") ;}#include <stdio. h> # include <string. h> # include <math. h> int vist [1000], flog, len, target, s [10], top, loction [10]; int m [1000]; int cmp () {int v, w, x, y, z; // The precision of converting to int Type v = s [1]; w = (int) (pow (s [2] * 1.0, 2.0) + 0.5); x = (int) (pow (s [3] * 1.0, 3.0) + 0.5 ); y = (int) (pow (s [4] * 1.0, 4.0) + 0.5); z = (int) (pow (s [5] * 1.0, 5.0) + 0.5); if (v-w + x-y + z = target) return 1; // returns 1 return 0;} void DFS (int n) {int I; s [++ top] = m [n]; // stack loction [top] = n; // Note the location vist [n] of the green m [n] = 1; // indicates that the n position is accessed if (top = 5) // if they are equal, you can compare flog = cmp (); if (flog) // true, you do not need to return; if (top <5) // The number can be placed only when the number of stacks is smaller than 5. {for (I = 1; I <= len; I ++) if (vist [I] = 0) {DFS (I); if (flog) // used to end the DFS return;} vist [n] = 0; top --; // return} int main () {char str [1000], tem; int I, j, e; while (scanf ("% d % s", & target, str)> 0) {if (target = 0 & strcmp (str, "END") = 0) break; flog = 0; len = strlen (str); for (I = 0; I <len; I ++) // In the lexicographically ascending order {e = I; for (j = I + 1; j <len; j ++) if (str [e] <str [j]) e = j; tem = str [I]; str [I] = str [e]; str [e] = tem;} for (I = 0; I <len; I ++) // change to the number m [I + 1] = str [I]-'A' + 1; for (I = 1; I <= len; I ++) // starts with the number of I: {memset (vist, 0, sizeof (vist); top = 0; DFS (I); if (flog) break ;} if (flog) {for (I = 1; I <= top; I ++) printf ("% c", str [loction [I]-1]);} else printf ("no solution"); printf ("\ n ");}}