HDOJ-1015Safecracker(DFS)
来源:互联网 发布:淘宝产品质检报告 编辑:程序博客网 时间:2024/05/20 09:07
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 Input1 ABCDEFGHIJKL11700519 ZAYEXIWOVU3072997 SOUGHT1234567 THEQUICKFROG0 ENDSample OutputLKEBAYOXUZGHOSTno solution题意:给你一个数,再给一个全部是大写字构成的字符串,从里面选5个字母V-m^2+x^3-y^3+z^5是否等于目标数,选出来的方案可能有很多种,那么你应该选择字典序最大的那种import java.util.Arrays;import java.util.Scanner;public class Main {private static char handle[] = new char[6];private 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'};private static char chs[];private static int target;private static String str;private static boolean map[]; //标识字母是否已经用了public static void main(String[] args) { Scanner sc = new Scanner(System.in);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--){//chs[i]=chs[i]^chs[j];//chs[j]=chs[i]^chs[j]; //位运算只能用于整数//chs[i]=chs[i]^chs[j];char temp = chs[i];chs[i] = chs[j];chs[j] = temp;}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;}}elseif(j==1){if(b==at[i]){ap[1]=i;break;}}else if(j==2){if(c==at[i]){ap[2]=i;break;}}elseif(j==3){if(d==at[i]){ap[3]=i;break;}}elseif(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;}}}
0 0
- hdoj 1015 Safecracker 【dfs】
- HDOJ 1015 Safecracker-DFS
- hdoj 1015 Safecracker【DFS】
- hdoj 1015 Safecracker 【DFS】
- HDOJ-1015Safecracker(DFS)
- HDOJ 1015 Safecracker(DFS)
- HDOJ(HDU).1015 Safecracker (DFS)
- HDOJ 题目1015 Safecracker(dfs)
- hdoj 1015 Safecracker(窃贼) Dfs
- HDOJ 1015 Safecracker
- HDOJ 1015 Safecracker
- hdu/hdoj 1015 Safecracker
- HDOJ 1015 Safecracker
- HDOJ 1015 Safecracker
- hdoj 1015 Safecracker
- HDOJ HDU 1015 Safecracker
- hdoj 1015 Safecracker
- HDOJ 1015 Safecracker
- 图的储存结构之邻接表
- TCP三次握手和四次握手
- hello.c一个生命周期经历的四个过程详解
- js中slice(),substr(),substring()的区别
- odroid上mavros指定版本安装
- HDOJ-1015Safecracker(DFS)
- Windows Data Type
- win7 下解决python 集成编译环境IDLE无法打开的问题。
- Ajax技术之解决中文乱码问题
- floyd求最小环
- 代码混淆 GSON完满解决
- Uva
- Java System.out.println()的解释
- 条件随机场学习
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
拍拍贷账号注销了怎么办
我在拍拍贷注销了怎么办
快贷逾期一年了怎么办
广州车牌买新车旧车怎么办
高尔夫旅行款被锁在车内怎么办
英雄联盟误删文件怎么办
拍拍贷换了号码怎么办
手机打开显示无法连接服务器怎么办
剑灵画面卡顿怎么办
cf被永久禁赛了怎么办
微信没有微游戏商店怎么办
游侠云盒下载慢怎么办2018
安卓手机玩网页游戏卡怎么办
safari点开什么都没有怎么办
康佳电视全网搜索打不开怎么办
脚被图钉扎了怎么办
电脑中毒了打不开软件怎么办
剑三程序不兼容怎么办
玩无主之地卡怎么办
平台老板跑路了怎么办
qq在苹果下载不了怎么办
下载速度快上传速度慢怎么办
苹果7开网页慢怎么办
为什么浏览器下载视频速度慢怎么办
机连WLAN网速慢怎么办
会声会影卸载后无法重新安装怎么办
电视空间不足无法卸载怎么办
堡垒之夜下载慢怎么办
手机网盘下载速度慢怎么办插件
笔记本电脑显示连接不可用怎么办
蓝魔手机充电慢怎么办
360f4手机充电慢怎么办
vivo卡了怎么办小窍门
白色衣服用84泡后变黄怎么办
用祛斑霜脱皮了怎么办
用祛斑霜脸一直蜕皮怎么办
吃热的就流鼻涕怎么办
键盘qaz失灵其他都没事怎么办
时时彩代理抓了怎么办
6p升级ios11卡顿怎么办
微信弄没了又换号了怎么办