HDU-1015(暴力)

Safecracker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11553    Accepted Submission(s): 5952

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 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 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 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 could 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 12:30 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 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
 
题意:给一个方程,给一个target的数值。给一个字符串。选字符串中的5个不同的数。A对应的数值是1,Z对应的数值是26.按照顺序求出结果,如果和target值相等,就为一个解。求所有解中的字典序最大的解。
思路:因为串长最多是12。所以可以直接暴力。5层for循环。就能过。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 int n;
 7 char s[20];
 8 char result[10];
 9 char temp[10];
10 
11 int main()
12 {
13     while(scanf("%d %s", &n, s)){
14         if(n == 0 && !strcmp(s, "END")) break;
15         int len = strlen(s);
16         memset(result, '\0', sizeof(result));
17         memset(temp, '\0', sizeof(temp));
18         bool flag = false;
19         
20         for (int v = 0; v < len; v++){
21             for (int w = 0; w < len; w++){
22                 for (int x = 0; x < len; x++){
23                     for (int y = 0; y < len; y++){
24                         for (int z = 0; z < len; z++){
25                                 
26                             int num01 = s[v] - 'A' + 1;//将字符转换成数值
27                             int num02 = s[w] - 'A' + 1;
28                             int num03 = s[x] - 'A' + 1;
29                             int num04 = s[y] - 'A' + 1;
30                             int num05 = s[z] - 'A' + 1;
31                             //求结果
32                             long long jieguo = num01 -
33                                                num02 * num02 +
34                                                num03 * num03 * num03 -
35                                                num04 * num04 * num04 * num04 +
36                                                num05 * num05 * num05 * num05 * num05;
37                             if(jieguo == n &&
38                                v != w && v != x && v != y && v != z &&
39                                w != x && w != y && w != z &&
40                                x != y && x != z &&
41                                y != z){//v,w,x,y,z这5个数两两不能相同
42                                    
43                                 temp[0] = s[v];
44                                 temp[1] = s[w];
45                                 temp[2] = s[x];
46                                 temp[3] = s[y];
47                                 temp[4] = s[z];
48                                 temp[5] = '\0';
49                                 
50                                 if(strcmp(temp, result) > 0){//如果字典序大,则替换。
51                                     flag = true;
52                                     for (int i = 0; i <= 5; i++){
53                                         result[i] = temp[i];
54                                     }
55                                 }
56                             }
57                         }
58                     }
59                 }
60             }
61         }
62 
63         if(!flag)
64             printf("no solution\n");
65         else{
66             printf("%s\n", result);
67         }
68     }
69     return 0;
70 }
posted @ 2016-02-24 23:46  喷水小火龙  阅读(87)  评论(0)    收藏  举报