HDU-1015 Safecracker(暴力枚举)

题目回顾(HDU-1015

Safecracker

Problem Description

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

"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-12个大写字母表示的字符串,字母的值分别是A=1,B=2,...,Z=26。从中找出5个字母,满足v - w^2 + x^3 - y^4 + z^5 = target。并输出满足条件的字典序最大的情况。

  容易分析出计算的情况只有5*4*3*2*1 ~ 12*11*10*9*8种,因此通过暴力枚举的方法应该不会超时。(此题也可以使用DFS)

  在编码中出现过一个小情况,使用sort快排的时候,sort(x,x+len,compare)写成了sort(x,x+len-1,compare)导致出错。

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 bool compare(int a,int b){
 7     return a>b;
 8 }
 9
10 int main(){
11     int n;
12     string s;
13     int x[13];
14     while(cin>>n>>s&&(n!=0&&s!="END")){
15         int len=s.length();
16         for(int i=0;i<len;i++){
17             x[i]=s[i]-‘A‘+1;
18         }
19         sort(x,x+len,compare);
20         bool flag=false;
21         int a,b,c,d,e;
22         for(a=0;a<len;a++){
23             for(b=0;b<len;b++){
24                 if(b==a){
25                     continue;
26                 }
27                 for(c=0;c<len;c++){
28                     if(c==a||c==b){
29                         continue;
30                     }
31                     for(d=0;d<len;d++){
32                         if(d==a||d==b||d==c){
33                             continue;
34                         }
35                         for(e=0;e<len;e++){
36                             if(e==a||e==b||e==c||e==d){
37                                 continue;
38                             }
39                             if(x[a]-x[b]*x[b]+x[c]*x[c]*x[c]-x[d]*x[d]*x[d]*x[d]+x[e]*x[e]*x[e]*x[e]*x[e]==n){
40                                 flag=true;
41                                 break;
42                             }
43                         }
44                         if(flag){
45                             break;
46                         }
47                     }
48                     if(flag){
49                         break;
50                     }
51                 }
52                 if(flag){
53                     break;
54                 }
55             }
56             if(flag){
57                 break;
58             }
59         }
60         if(flag){
61             char ans[5];
62             ans[0]=‘A‘+(x[a]-1);
63             ans[1]=‘A‘+(x[b]-1);
64             ans[2]=‘A‘+(x[c]-1);
65             ans[3]=‘A‘+(x[d]-1);
66             ans[4]=‘A‘+(x[e]-1);
67             for(int i=0;i<5;i++){
68                 cout<<ans[i];
69             }
70             cout<<endl;
71         }else{
72             cout<<"no solution"<<endl;
73         }
74     }
75     return 0;
76 } 

原文地址:https://www.cnblogs.com/orangecyh/p/9769836.html

时间: 2024-10-09 16:44:40

HDU-1015 Safecracker(暴力枚举)的相关文章

hdu 1015 Safecracker (纯暴力)

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

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 1015.Safecracker【暴力枚举】【8月17】

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 des

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

hdu 1015 Safecracker DFS解法 ,简单明了

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

BestCoder Round #50 (div.1) 1002 Run (HDU OJ 5365) 暴力枚举+正多边形判定

题目:Click here 题意:给你n个点,有多少个正多边形(3,4,5,6). 分析:整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正四边形即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define power(x) ((x)*(x))

HDU ACM 1015 Safecracker 暴力DFS

分析:暴搜,复杂度一次最高也才12^5. #include<iostream> #include<cmath> using namespace std; #define N 30 char s[15],ts[10],ans[10]; int tar,index[N]; bool vis[N]; void dfs(int d,int n) { int i,tmp; if(d==5) { tmp=index[ts[0]-'A']-pow(index[ts[1]-'A'],2)+pow(

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 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

hdu 1407 1248 暴力枚举

背景:老了老了,做了这么久题竟然忘了EOF来文件结束了. 心得:一定要自己调试,直到自己觉得所有情况都考虑完了还是wa再借助网络. 学习:1.此题为缩减时间可对所有m打表,然后查表即可. #include<stdio.h> int main(void) { int num, x, y, z; while(scanf("%d", &num)!=EOF){ for (int i = 1; i*i <= num; i++){ for (int j = i; j*j