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");
}
}
}
}

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

  1. HDOJ(HDU).1015 Safecracker (DFS)

    HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...

  2. HDU 1015.Safecracker【暴力枚举】【8月17】

    Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===  "The item is lo ...

  3. ZOJ 1403&&HDU 1015 Safecracker【暴力】

    Safecracker Time Limit: 2 Seconds      Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 ...

  4. HDOJ/HDU 1015 Safecracker(深搜)

    Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...

  5. hdu 1015 Safecracker 水题一枚

    题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein s ...

  6. HDU 1015 Safecracker 解决问题的方法

    Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===  "The item is locked in a Kl ...

  7. HDU 1015 Safecracker【数值型DFS】

    Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. HDU 1015 Safecracker(第一次用了搜索去遍历超时,第二次用for循环能够了,思路一样的)

    Safecracker Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total S ...

  9. HDU 1015 Safecracker

    解题思路:这题相当诡异,样例没过,交了,A了,呵呵,因为理论上是可以通过的,所以 我交了一发,然后就神奇的过了.首先要看懂题目. #include<cstdio> #include< ...

随机推荐

  1. 暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  2. 九度OJ 1019 简单计算器 -- 2006年浙江大学计算机及软件工程研究生机试真题

    题目地址:http://ac.jobdu.com/problem.php?pid=1019 题目描述:     读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 输入: ...

  3. ISO 学习笔记 2015-03-15

    Objective--C 一 关键字 @property 定义变量函数 @synthesize 实现变量函数 二 函数 alloc 分配内存 init 初始化 new 替代上面两个函数 分配内存,并且 ...

  4. 深度优化LNMP之Nginx [2]

    深度优化LNMP之Nginx [2]   配置Nginx gzip 压缩实现性能优化 1.Nginx gzip压缩功能介绍        Nginx gzuo压缩模块提供了压缩文件内容的功能,用户请求 ...

  5. extern “C”的作用

    1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同.作为一种欲与C兼容的语言,C++保留了一部分过程 式 ...

  6. nginx 中出现nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

    有其他的程序占用的80端口.只需把相关程序关闭,fuser -k 80/tcp 然后再次 /usr/local/nginx/sbin/nginx,就能开启nginx服务了

  7. 快速理解webStroage

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. sonar-maven-plugin问题

    问题: jenkins本地构建时sonar报错 StackOverflow问题 [ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven ...

  9. 尝试使用Java6API读取java代码

    主要类:JavaCompiler  FileManager JavaCompiler .CompilationTaskAbstractProcessor参考代码https://today.java.n ...

  10. js截取小数点后几位的写法

    截取小数点后几位的方法有很多,下面为大家介绍下使用js是如何实现的 如果${showInfo.tt}的值为20,要要它除以10以后精确到小数点后2位,那么js代码中可作如下写法:  复制代码 代码如下 ...