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. 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");
            }
        }
    }
}
时间: 2024-11-01 20:02:22

HDOJ/HDU 1015 Safecracker(枚举、暴力)的相关文章

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

HDOJ(HDU) 1562 Guess the number(水题,枚举就行)

Problem Description Happy new year to everybody! Now, I want you to guess a minimum number x betwwn 1000 and 9999 to let (1) x % a = 0; (2) (x+1) % b = 0; (3) (x+2) % c = 0; and a, b, c are integers between 1 and 100. Given a,b,c, tell me what is the

HDOJ(HDU) 1407 测试你是否和LTC水平一样高(暴力)

Problem Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数解. Input 输入数据包含多个测试实例,每个实例占一行,仅仅包含一个小于等于10000的正整数num. Output 对于每组测试数据,请按照x,y,z递增的顺序输出它的一个最小正整数解,每个实例的输出占一行,题目保证所有测试数据都有解. Sample Input 3 Sample Output

HDU 4380 预处理枚举

题意:给出n个房子m个矿问从n个房子选三个组成的三角形内部矿数为奇数有多少种选法. 先预处理一下每条线段正上方有多少个点,然后在枚举三条线段就可以了. #include <iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct point { long long x,y; }; int

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 2061 Treasure the new start, freshmen!(水题、)

Problem Description background: A new semester comes , and the HDU also meets its 50th birthday. No matter what's your major, the only thing I want to tell you is:"Treasure the college life and seize the time." Most people thought that the colle

HDOJ(HDU) 2109 Fighting for HDU(简单排序比较)

Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况: 在最初的两年里,HDU发展非常迅速,综合各种ACM算法生成的老鼠药效果奇好,据说该药专对老鼠有效,如果被人误食了,没有任何副作用,甚至有传闻说还有健胃的效果,不过这倒没有得到临床验证.所以,公司的销量逐年递增,利润也是节节攀升,作为股东之一的公主负责财务,最近半年,她实在辛苦,多次因为点钞票造成双手抽筋而住院,现在在她面前你根本不要提到"

HDOJ(HDU) 2107 Founding of HDU(找最大值)

Problem Description 经过慎重的考虑,XHD,8600, LL,Linle以及RPG等ACM队员集体退役,甚至正在酝酿退学. 为什么?要考研?那也不用退学呀- 当然不是!真正的原因是他们想提前创业,想合伙成立一家公司,据说公司的名称都想好了,为了感谢多年的ACM集训队队长XHD,公司就叫海东集团(HaiDong Union),简称HDU.(对于这个公司名称,几个人私下里开玩笑说,外面的人看到HDU,可别以为是"胡捣集团",呵呵) 公司成立了,谁来做老大呢?这对于合伙的