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
上个我用枚举做了,感觉不怎么好,毕竟是练算法的,就试试了深搜。
题意:
给你一个数,再给一个全部是大写字母构成的字符串。
从里面选5个字母v,m,x,y,z(不重复),计算v-m^2+x^3-y^4+z^4是否等于目标值
选出来的方案可能有很多种,那么你应该选择字典序最大的那种。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static char handle[] = new char[6];
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'};
static char chs[];
static int target;
static String str;
static boolean map[];//标识是否已经用了
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()){
target = sc.nextInt();
str = sc.next();
if(target==0&&str.equals("END")){
return;
}
map = new boolean[str.length()];
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;
}
if(dfs(0)){
for(int i=0;i<5;i++){
System.out.print(handle[i]);
}
System.out.println();
}else{
System.out.println("no solution");
}
}
}
private static boolean dfs(int m) {
if(m==5){
if( res(handle[0],handle[1],handle[2],handle[3],handle[4]) ){
return true;
}
return false;
}else{
for(int i=0;i<str.length();i++){
if(!map[i]){
map[i]=true;
handle[m]=chs[i];
if(dfs(m+1)){
return true;
}
map[i]=false;
}
}
}
return false;
}
private static boolean res(char a, char b, char c, char d, char e) {
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(a==at[i]){
ap[0]=i;
break;
}
}else
if(j==1){
if(b==at[i]){
ap[1]=i;
break;
}
}else
if(j==2){
if(c==at[i]){
ap[2]=i;
break;
}
}else
if(j==3){
if(d==at[i]){
ap[3]=i;
break;
}
}else
if(j==4){
if(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){
return true;
}else{
return false;
}
}
}
HDOJ/HDU 1015 Safecracker(深搜)的更多相关文章
- HDOJ(HDU).1015 Safecracker (DFS)
HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...
- HDOJ/HDU 1015 Safecracker(枚举、暴力)
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...
- hdu 1015 Safecracker 水题一枚
题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein s ...
- 题解报告:hdu 1015 Safecracker
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kl ...
- hdu 1518 Square 深搜,,,,花样剪枝啊!!!
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- Block Breaker HDU - 6699(深搜,水,写下涨涨记性)
Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...
- HDU 1015 Safecracker
解题思路:这题相当诡异,样例没过,交了,A了,呵呵,因为理论上是可以通过的,所以 我交了一发,然后就神奇的过了.首先要看懂题目. #include<cstdio> #include< ...
- ZOJ 1403&&HDU 1015 Safecracker【暴力】
Safecracker Time Limit: 2 Seconds Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 ...
- HDU 1015 Safecracker【数值型DFS】
Safecracker Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- UVA 11384 Help is needed for Dexter(问题转化 递归)
Help is needed for Dexter Time Limit: 3 Second Dexter is tired of Dee Dee. So he decided to keep Dee ...
- 九度OJ 1516 调整数组顺序使奇数位于偶数前面 -- 归并排序
题目地址:http://ac.jobdu.com/problem.php?pid=1516 题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的 ...
- ubuntu 安装 桌面 awesome
受了ubuntu 12.04自带的桌面,运行太卡了 http://www.linuxzen.com/awesometmuxgnomedoda-zao-gao-xiao-linuxzhuo-mian-h ...
- 《APUE》第三章笔记(1)
以下内容是我看<APUE>第二版第三章的笔记,有错还希望指出来,谢谢. unbuffered I/O,跟buffered I/O相对,buffered I/O就是 ISO C标准下的标准输 ...
- Extjs发票管理系统
技术特点:Extjs框架,三层架构,Ajax,json 1.仿office2007菜单.介面美观大方,可动态更改皮肤保存至cookie. 2,json数据源与实体类的相互转换. 3.可下载桌面版登录方 ...
- [python]类与类中的列表
最近在用类中的列表时出现一件怪事 实例2中的列表,竟然有实例1中的数据. 查了半天发现是list的append方法的问题. 将全部的list.append(value) 换成 list = list ...
- ~/.vimrc config
runtime! debian.vim "设置编码 set encoding=utf- set fencs=utf-,ucs-bom,shift-jis,gb18030,gbk,gb2312 ...
- CSS3 学习
border-radius: 半径,不用学了,用得很熟了,但要记得它的某一个角的写法是border-top/bottom-left/right-radius: ,参数中的两个值为先左右后上下,支持百分 ...
- 解决CocoaPods在OS X 10.11出现问题-b
最近把mac系统升级到10.11系统,但是在用pod install命令的时候,却提示command not found.后来上网查了下才知道,Cocoapods在10.11系统上发生了变化. 在st ...
- 【poj3070】矩阵乘法求斐波那契数列
[题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...