HDU 1015 Safecracker(回溯)
来源:互联网 发布:新浪历史分笔数据接口 编辑:程序博客网 时间:2024/05/07 16:46
=== 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."
"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 ABCDEFGHIJKL11700519 ZAYEXIWOVU3072997 SOUGHT1234567 THEQUICKFROG0 END
Sample Output
LKEBAYOXUZGHOSTno solution
用dfs可做,用5个for循环也可暴力ac,而这题主要以训练搜索为主,所以建议用bfs。
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int s[100],flag[100],f[5];#define res(v,w,x,y,z) v-w*w+x*x*x-y*y*y*y+z*z*z*z*z int dfs(int step,int len ,int n){if(step==5){if(res(f[0],f[1],f[2],f[3],f[4])==n)return 1;return 0;}for(int i=len;i>=0;i--){if(flag[i]) continue;flag[i]=1;f[step]=s[i];if(dfs(step+1,len,n)) return 1;flag[i]=0;}return 0;}int main(){int i,j,n;char str[50];while(~scanf("%d %s",&n,str)){if(n==0&&strcmp(str,"END")==0) break;int len=0;while(str[len]){s[len]=str[len]-'A'+1;flag[len]=0;len++;}sort(s,s+len);if(dfs(0,len-1,n)) {for(i=0;i<5;i++) printf("%c",f[i]+'A'-1); cout<<endl; }else puts("no solution");}return 0;}暴力ac
#include<iostream>#include<string.h>using namespace std;#define res(v,w,x,y,z) v-w*w+x*x*x-y*y*y*y+z*z*z*z*z int cmp(int a,int b){return a>b;}int main(){int n;char str[50],s[50];while(~scanf("%d %s",&n,str)){if(n==0&&strcmp(str,"END")==0) break;int len=0;while(str[len]){s[len]=str[len]-'A'+1;len++;}int temp=0;sort(s,s+len,cmp);for(int i=0;i<len;i++){for(int j=0;j<len;j++){if(i==j) continue;for(int k=0;k<len;k++){if(i==k||k==j) continue;for(int x=0;x<len;x++){if(x==i||x==j||x==k) continue;for(int y=0;y<len;y++){if(y==i||y==j||y==k||y==x) continue;if(res(s[i],s[j],s[k],s[x],s[y])==n){ printf("%c%c%c%c%c\n",s[i]+'A'-1,s[j]+'A'-1,s[k]+'A'-1,s[x]+'A'-1,s[y]+'A'-1); temp=1; break; }}if(temp==1) break;}if(temp==1) break;}if(temp==1) break;}if(temp==1) break;}if(temp==0)puts("no solution");}return 0;}
- HDU 1015 Safecracker(回溯)
- hdu Safecracker(回溯)
- HDU 1015 Safecracker 搜素回溯
- hdu(1015)Safecracker
- HDU-1015 Safecracker(DFS+暴力回溯 / 五重循环)
- HDU 1015 Safecracker (DFS)
- HDU 1015 Safecracker(DFS)
- HDOJ1015 Safecracker(DFS,回溯)
- hdu 1015(Safecracker)(深搜)
- POJ 1248 && HDU 1015 Safecracker(dfs)
- HDU - 1015 Safecracker
- HDU 1015 Safecracker
- HDU 1015 Safecracker
- Hdu 1015 Safecracker
- hdu 1015 Safecracker
- hdu 1015 Safecracker(枚举)
- hdu 1015 Safecracker
- hdu 1015 Safecracker
- 【hdu 1712】ACboy needs your help (分组背包)
- Hibernate泛型DAO及使用方法
- 通讯录 乱码的处理解决方案
- Ubuntu U盘启动盘修复更新后无法启动系统的bug
- 小明的家乡II 最小生成树之prim算法
- HDU 1015 Safecracker(回溯)
- Sql 关联查询统计案例
- 数据预处理(2)
- 如何高速下载国外资源
- ACM竞赛中提交题目时常见的一些结果
- 使用NSTimer和gcb实现倒计时
- android延时加载的几种做法
- 获得通讯录中联系人的所有属性
- linux服务简介-关闭不需要的Linux服务 (转)