hdu 1015 Safecracker 水题一枚

题目链接:HDU - 1015

=== 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."

题意描述:给出一个目标值和一串仅包含大写字母的长度在5~12的字符串,字符串中A的值为1,B的值为2,,,Z为26,要求在这个字符串中找出5个不同的字符满足v-w*w+x*x*x-y*y*y*y+z*z*z*z*z等于目标值的同时,求出字典序最大的这样5个字符组成的字符串。

算法分析:源字符串只有12的长度,找出5个来满足条件,暴力求解即可了,注意一点:看到式子中z*z*z*z*z这样的就应该想到用long long 来解决。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10
11 char str[15];
12 LL an[27]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
13     21,22,23,24,25,26};
14 LL target;
15
16 int cmp_char(const void *_a,const void *_b)
17 {
18     char *a=(char *)_a;
19     char *b=(char *)_b;
20     return *a - *b;
21 }
22
23 int main()
24 {
25     while (scanf("%I64d%s",&target,str)!=EOF)
26     {
27         if (target==0 && strcmp(str,"END")==0) break;
28         int len=strlen(str);
29         qsort(str,len,sizeof(char),cmp_char);
30         int flag=0;
31         for (int i=len-1 ;i>=0 ;i--)
32         {
33             for (int j=len-1 ;j>=0 ;j--) if (j!=i)
34             {
35                 for (int k=len-1 ;k>=0 ;k--) if (k!=i && k!=j)
36                 {
37                     for (int u=len-1 ;u>=0 ;u--) if (u!=i&&u!=j&&u!=k)
38                     {
39                         for (int v=len-1 ;v>=0 ;v--) if (v!=i&&v!=j&&v!=k&&v!=u)
40                         {
41                             LL value=an[str[i]-‘A‘]-an[str[j]-‘A‘]*an[str[j]-‘A‘]+an[str[k]-‘A‘]*
42                                 an[str[k]-‘A‘]*an[str[k]-‘A‘]-an[str[u]-‘A‘]*an[str[u]-‘A‘]*an[str[u]-‘A‘]
43                                 *an[str[u]-‘A‘]+an[str[v]-‘A‘]*an[str[v]-‘A‘]*an[str[v]-‘A‘]*an[str[v]-‘A‘]*an[str[v]-‘A‘];
44                             if (value==target)
45                             {
46                                 flag=1;
47                                 cout<<str[i]<<str[j]<<str[k]<<str[u]<<str[v]<<endl;
48                                 break;
49                             }
50                         }
51                         if (flag) break;
52                     }
53                     if (flag) break;
54                 }
55                 if (flag) break;
56             }
57             if (flag) break;
58         }
59         if (!flag) printf("no solution\n");
60     }
61     return 0;
62 }
时间: 04-17

hdu 1015 Safecracker 水题一枚的相关文章

hdu 2053 Switch Game 水题一枚,鉴定完毕

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10200    Accepted Submission(s): 6175 Problem Description There are many lamps in a line. All of them are off at first. A series of op

HDU 1015 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 are extremely rare; most of them, along with Klein and his factory, were destroyed in Wo

HDU 2092 整数解 --- 水题

x+y = n, x*y = m; y = n - x; x * ( n - x) = m nx - x^2 = m; x^2 - nx + m = 0; △ = sqrt(n^2 - 4m) 要有整数解即△需要为可开方数即可. /* HDU 2092 整数解 --- 水题 */ #include <cstdio> #include <cmath> int main() { double n, m; while (scanf("%lf%lf", &n,

HDU 1015 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 are extremely rare; most of them, along with Klein and his factory, were destroyed in Wo

hdu 5210 delete 水题

Delete Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5210 Description wld有n个数(a1,a2,...,an),他希望进行k次删除一个数的操作,使得最后剩下的n−k个数中有最多的不同的数,保证1≤n≤100,0≤k<n,1≤ai≤n(对于任意1≤i≤n) Input 多组数据(最多100组)对于每组数据:第一行:一个数n表示数的个数接下来一行:

hdu 2682 Tree 最小生成树~~~~水题一枚,,用到了筛法求素数,我竟然在格式上面PE了两次!!

Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1754    Accepted Submission(s): 509 Problem Description There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities

HDU 5703 Desert 水题 找规律

已知有n个单位的水,问有几种方式把这些水喝完,每天至少喝1个单位的水,而且每天喝的水的单位为整数.看上去挺复杂要跑循环,但其实上,列举几种情况之后就会发现是找规律的题了= =都是2的n-1次方,而且这题输出二进制数就行了......那就更简单了,直接输出1,然后后面跟n-1个0就行了╮(╯_╰)╭ 下面AC代码 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>

hdu 4802 GPA 水题

GPA Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4802 Description In college, a student may take several courses. for each course i, he earns a certain credit (ci), and a mark ranging from A to F, which is c

HDU 1015 Safecracker【数值型DFS】

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3966    Accepted Submission(s): 2028 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein