Safecracker
Problem description=== Op Tech Briefing, 2002/11/02 06:42 CST = = =
"The item is locked in a Klein safe behind a painting in the Second-floor library. Klein safes is extremely rare; Most of the them, along with Klein and he factory, were destroyed in world War Ii. Fortunately old Brumbaugh from knew Klein's secrets and wrote them down before he died. A Klein Safe has both distinguishing features:a combination lock that uses letters instead of numbers, and an engraved quo Tation on the door. A Klein Quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentence S, 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 to get a numeric target. (The details of the constructing the target number are classified.) To find the combination you must select five Letters V, W, X, y, and z so satisfy the following equation, where each let TER 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 then the combination are the one that's lexicographically greatest, i.e., the one that Would 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 is actually several solutions in this case, and the combination turns off to be lkeba. Klein thought it is safe to encode the combination within the engraving, because it could take months of effort to try Al L The possibilities even if you knew the secret. But the course computers didn ' t exist then. "
= = = Op Tech Directive, computer division, 2002/11/02 12:30pm CST = = =
"Develop a program to find Klein combinations on preparation for field deployment. Use standard test methodology as per departmental regulations. Input consists of one or more lines containing a positive an integer target less than twelve million, a space, then at least Five and at the most twelve distinct uppercase letters. The last line would contain 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 C Ombination. Use the exact format shown below. "
Sample Input
1 ABCDEFGHIJKL11700519 ZAYEXIWOVU3072997 SOUGHT1234567 THEQUICKFROG0 END
Sample Output
Lkebayoxuzghostno Solution
a=1,b=2 Z=26. Given target, find 5 characters from known characters so that v-w^2 + x^3-y^4 + z^5 = target is established. Multi-group solution outputs the largest dictionary order. Violent enumeration, never thought could be too. 5-Layer loop:
#include <cstdio> #include <cstring>int f[30],target;int main () {char s[30]; while (scanf ("%d%s", &target,s)!=eof&&target) {memset (f,0,sizeof (f)); if (strlen (s) <5) {printf ("no solution"); Break } bool Key=false; for (int i=0;i<strlen (s); i++) f[s[i]-' @ ']=1; for (int a=26;a>0&&!key;a--) for (int. b=26;b>0&&!key;b--) {if (b!=a) for (i NT c=26;c>0&&!key;c--) {if (c!=b&&c!=a) for (int d=26;d>0&&!key; d--) {if (d!=c&&d!=b&&d!=a) for (int e=26;e>0&&!key;e--) { if (e!=d&&e!=c&&e!=b&&e!=a) if (f[a]&&f[b]& &f[c]&&f[d]&&f[e]) if (a-b*b+c*c*c-d*d*d*d+e*e*e*e*e==target) { Key=true; printf ("%c%c%c%c%c\n", (char) a+64, (char) b+64, (char) c+64, (char) d+64, (char) e+64); }}}}} if (!key) printf ("No solution\n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1015.Safecracker "Brute Force Enumeration" "August 17"