Title Source: http://noi.openjudge.cn/ch0201/250/ d:safecracker
Total time limit: 1000ms memory limit: 65536kB
Description
"The item islocked in a Klein safe behind a painting in the Second-floor library. Kleinsafes is extremely rare; Most of the them, along with Klein and he factory, weredestroyed in world War ii. Fortunately old Brumbaugh from the knew Klein ' ssecrets and wrote them down before he died. A Klein Safe has both distinguishingfeatures:a combination lock that uses letters instead of numbers, and anengraved quota tion on the door. A Klein Quotation always contains between Fiveand twelve distinct uppercase letters, usually at the beginning of sentences , and mentions one or more numbers. Five of the uppercase letters form thecombination that opens the safe. By combining the digits from all the numbersin the appropriate the-a-to-get a numeric target. (The details of constructingthe target number are classified.) To find the combination-must select Fiveletters V, W, X, Y, and z that satisfy the following equation, where Eachlette R is replaced by it ordinal position in the alphabet (A=1, b=2,..., z=26). The combination is then vwxyz. If there is more than one solution thenthe combination are the one that's lexicographically greatest, i.e., the Onethat wo Uld appear last in a dictionary. "
v-w2+ x3-y4+ z5= Target
"For example, given target 1 and letter set ABCDEFGHIJKL, one possiblesolution is FIECB, since 6-92+ 53-34+ 25=1. There is actually several solutions in this case, and the combination turnsout to be lkeba. Klein thought it is safe to encode the combination within theengraving, because it could take months of effort to try all The Possibilitieseven if you knew the secret. But of course computers didn ' t existthen. "
"Develop a program to find Klein combinations in preparation for fielddeployment. Use standard test methodology as per Departmentalregulations.
input
Input consists of one or more lines containing a positiveinteger target less than twelve million, a space, then at least F Ive and atmost twelve distinct uppercase letters. The last line would contain a target Ofzero and the letters END; This signals the end of the input.
Output
For each line output of the unique Klein combination, or ' nosolution ' if there is no correct combination. Use the exact format Shownbelow. "
Sample Input
1 ABCDEFGHIJKL
11700519 Zayexiwovu
3072997 sought
1234567 Thequickfrog
0 END
Sample Output
Lkeba
Yoxuz
GHOST
No solution
-----------------------------------------------------
Thinking of solving problems
Brute Force enumeration
-----------------------------------------------------
Code
#include <iostream> #include <vector> #include <string> #include <fstream> #include <
Algorithm> using namespace std;
int main () {#ifndef Online_judge int n, I, Len;
int oper[5] = {0};
Char Tmp[6] = {0};
int ind[5] = {0};
string S;
BOOL Has_solution;
Vector<int> VEC;
Ifstream fin ("xly2017D.txt");
Fin >> n >> S;
while (s!= "END") {vec.clear ();
Has_solution = false;
Len = S.length ();
for (i=0; i<len; i++) {vec.push_back ((int) (s.at (i)-' A ' + 1));
} sort (Vec.begin (), Vec.end ()); for (ind[0] = len-1; Ind[0] >= 0, Ind[0]--) {for (ind[1] = len-1; ind[1] >= 0; ind[1]--) {if (ind[1] ! = Ind[0]) {for (ind[2] = len-1; ind[2] >= 0; ind[2]-) {if (ind[2]! = ind[1] && ind[2] ! = Ind[0]) {for (ind[3] = len-1; ind[3] >=0; ind[3]--) {if (ind[3]!=ind[2] && IND [3]!=ind[1] && ind[3]!=ind[0]) {for (ind[4] = len-1; ind[4]>=0; ind[4]--) {if (ind[4]!=ind[3] && ind[4]!=ind[2] && ind[4]!=ind[1] && ind[4]!=in
D[0]) {for (i=0; i<5; i++) {oper[i] = vec[ind[i]]; } if (Oper[0]-oper[1]*oper[1]+oper[2]*oper[2]*oper[2]-oper[3]*oper[3]*oper[3]*oper[3] + Oper[4]*oper[4]*oper[4]*oper[4]*oper[4]==n) {for (i=0; i<5; i++) {tmp
[I] = (char) (Oper[i] + ' A '-1);
} string ans (TMP);
cout << ans << endl;
Has_solution = true;
Goto Mark; }}}}}}}} mark:if (!has_solution) {cout &
lt;< "no solution" << Endl;
} fin >> n >> S;
} fin.close ();
#endif #ifdef Online_judge int n, I, Len;
int oper[5] = {0};
Char Tmp[6] = {0};
int ind[5] = {0}; String S
BOOL Has_solution;
Vector<int> VEC;
CIN >> N >> S;
while (s!= "END") {vec.clear ();
Has_solution = false;
Len = S.length ();
for (i=0; i<len; i++) {vec.push_back ((int) (s.at (i)-' A ' + 1));
} sort (Vec.begin (), Vec.end ()); for (ind[0] = len-1; Ind[0] >= 0, Ind[0]--) {for (ind[1] = len-1; ind[1] >= 0; ind[1]--) {if (ind[1] ! = Ind[0]) {for (ind[2] = len-1; ind[2] >= 0; ind[2]-) {if (ind[2]! = ind[1] && ind[2] ! = Ind[0]) {for (ind[3] = len-1; ind[3] >=0; ind[3]--) {if (ind[3]!=ind[2] && IND
[3]!=ind[1] && ind[3]!=ind[0]) {for (ind[4] = len-1; ind[4]>=0; ind[4]--) {
if (Ind[4]!=ind[3] && ind[4]!=ind[2] && ind[4]!=ind[1] && ind[4]!=ind[0]) {
for (i=0; i<5; i++) {oper[i] = vec[ind[i]]; } if (Oper[0]-oper[1]*oper[1]+OPER[2]*OPER[2]*OPER[2]-oper[3]*oper[3]*oper[3]*oper[3] +oper[4]*oper[4]*oper[4]*oper[4]*oper[4]==
N) {for (i=0; i<5; i++) {Tmp[i] = (char) (Oper[i] + ' A '-1);
} string ans (TMP);
cout << ans << endl;
Has_solution = true;
Goto Mark; }}}}}}}} mark:if (!has_solution) {cout &
lt;< "no solution" << Endl;
} CIN >> n >> S; } #endif}