HDOJ/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.

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 和一个字符串 s,在字符串 s 找出一个由5个字符组成的最大字符串使得v - w^2 + x^3 - y^4 + z^5 = target ;
分析:枚举所有的5个元素组成的集合,依次去判断
5层循环

import java.util.Arrays;
import java.util.Scanner;

public class Main{
    static char at[]={' ','A','B','C','D','E','F','G','H','I','J'
            ,'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //for(int i='A';i<='Z';i++){
            //char c = (char)i;
            //System.out.print("'"+c+"',");
        //}
        while(sc.hasNext()){
            int target = sc.nextInt();
            String str = sc.next();
            if(target==0&&str.equals("END")){
                return;
            }
            char chs[] = str.toCharArray();
            Arrays.sort(chs);
            for(int i=0,j=chs.length-1;i<chs.length/2;i++,j--){
                char c=chs[i];
                chs[i]=chs[j];
                chs[j]=c;
            }
            boolean haveAnswer = false;

            con: for(int a=0;a<chs.length;a++){

                for(int b=0;b<chs.length;b++){
                    if(a==b){
                        continue;
                    }
                    for(int c=0;c<chs.length;c++){
                        if(a==c||b==c){
                            continue;
                        }
                        for(int d=0;d<chs.length;d++){
                            if(d==a||d==b||d==c){
                                continue;
                            }
                            for(int e=0;e<chs.length;e++){
                                if(e==a||e==b||e==c||e==d){
                                    continue;
                                }
                                int ap[] = new int[5];
                                for(int j=0;j<ap.length;j++){
                                    for(int i=1;i<at.length;i++){
                                        if(j==0){
                                            if(chs[a]==at[i]){
                                                ap[0]=i;
                                                break;
                                            }
                                        }else
                                        if(j==1){
                                            if(chs[b]==at[i]){
                                                ap[1]=i;
                                                break;
                                            }
                                        }else
                                        if(j==2){
                                            if(chs[c]==at[i]){
                                                ap[2]=i;
                                                break;
                                            }
                                        }else
                                        if(j==3){
                                            if(chs[d]==at[i]){
                                                ap[3]=i;
                                                break;
                                            }
                                        }else
                                        if(j==4){
                                            if(chs[e]==at[i]){
                                                ap[4]=i;
                                                break;
                                            }
                                        }
                                    }
                                }

                                int sum=0;
                                for(int i=0;i<ap.length;i++){
                                    if(i%2==0){
                                        sum+=Math.pow(ap[i], i+1);
                                    }else{
                                        sum-=Math.pow(ap[i], i+1);
                                    }
                                }
                                if(sum==target){
                                    String s="";
                                    s+=chs[a];
                                    s+=chs[b];
                                    s+=chs[c];
                                    s+=chs[d];
                                    s+=chs[e];
                                    System.out.println(s);
                                    haveAnswer=true;
                                    break con;
                                }
                            }
                        }
                    }
                }
            }
            if(!haveAnswer){
                System.out.println("no solution");
            }
        }
    }
}
目录
相关文章
|
10月前
|
机器学习/深度学习
【每日一题Day196】LC2106摘水果 | 枚举+前缀和数组 同向双指针+二分查找
【每日一题Day196】LC2106摘水果 | 枚举+前缀和数组 同向双指针+二分查找
71 0
|
10月前
【每日一题Day213】LCP 33. 蓄水 | 枚举+贪心
【每日一题Day213】LCP 33. 蓄水 | 枚举+贪心
55 0
|
9月前
【洛谷 P2089】烤鸡(循环枚举)
烤鸡问题探讨了如何组合10种配料达成特定美味程度。给定正整数$n$代表美味程度,程序需列出所有使得配料总和等于$n$的方案。样例输入11对应10种配料的不同组合,输出显示了10种符合条件的方案。代码通过暴力枚举实现,AC代码展示了如何遍历所有可能的配料质量组合来找到答案。对于100%的数据,$n\leq5000$。
78 0
|
9月前
|
数据安全/隐私保护
【洛谷 P1928】外星密码 题解(递归+字符串)
外星密码挑战涉及解压缩由重复子串压缩的字符串,如`[3FUN]`代表`FUNFUNFUN`。输入是一行压缩过的字符串,输出是解压缩的结果。代码使用递归方法,遇到`[`读取重复次数并解压下一层,遇到`]`返回当前层结果,否则直接添加字符。样例输入`AC[3FUN]`输出`ACFUNFUNFUN`。处理的数据限制为解压后长度在20000内,最多十重压缩。
136 0
|
9月前
|
存储 人工智能 决策智能
每日一题——博弈论(枚举与暴力)
每日一题——博弈论(枚举与暴力)
51 1
每日一题——博弈论(枚举与暴力)
|
9月前
|
索引
力扣每日一题 6/17 枚举+双指针
力扣每日一题 6/17 枚举+双指针
48 1
|
9月前
【洛谷 P1618】三连击(升级版)题解(循环枚举+全排列)
该编程题目要求将数字1到9分为三组,形成三个三位数,使得这三个数成比例A:B:C。输入为A、B、C的值,输出符合条件的三位数组合,按首个数字升序排列。样例输入为1 2 3,输出多组解。代码使用全排列遍历数字,检查比例关系。若无解,则输出&quot;No!!!&quot;。
92 0
【每日一题Day54】LC1781所有子字符串美丽值之和 | 暴力
最大频率取决于当前字母是否会影响最大频率,因此只需一个变量记录前一子字符串s[i,j−1]的最大频率即可
82 0
|
机器学习/深度学习 算法
【递归与递推 4】AcWing95. 费解的开关 、AcWing 93. 递归实现组合型枚举、AcWing 1209. 带分数、AcWing 1208. 翻硬币
【递归与递推 4】AcWing95. 费解的开关 、AcWing 93. 递归实现组合型枚举、AcWing 1209. 带分数、AcWing 1208. 翻硬币
181 0
AcWing 周赛12 3804. 构造字符串(贪心 构造)
AcWing 周赛12 3804. 构造字符串(贪心 构造)
88 0