码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1015 Safecracker 水题一枚

时间:2015-04-18 01:01:46      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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 }

 

hdu 1015 Safecracker 水题一枚

标签:

原文地址:http://www.cnblogs.com/huangxf/p/4436398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!