【DFS填数】2017计算机学科夏令营上机考试 D:Safecracker
2018-03-31 18:52
429 查看
总时间限制: 1000ms内存限制: 65536kB 题目链接——>点击打开链接 提交链接——>描述"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 le
d49e
tter 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 - w2+ x3- y4+ z5= target
"For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6 - 92+ 53- 34+ 25= 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."
"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 unique Klein combination, or 'no solution' if there is no correct combination. Use the exact format shown below."样例输入
题意 Klein语句通常包含5到12个不同的大写字母,通常在句子的开头,并提及一个或多个数字。五个大写字母形成打开保险箱的组合。通过以适当的方式组合来自所有数字的数字,您将获得一个数字目标。 (构建目标号码的细节是分类的。)要找到组合,您必须选择满足以下等式的五个字母v,w,x,y和z,其中每个字母由其在字母表中的顺序位置替换A = 1,B = 2,...,Z = 26)。这个组合就是vwxyz。如果有多个解决方案,那么这个组合就是词典上最大的那个,也就是最后出现在字典中的那个。“
题型/思路 标准的DFS题,通过深搜暴力试出最优解。试数题(每个数字只能使用一次),一般思路都是DFS+暴力。因为满足等式条件的解可能有多个,题目要求输出字典序最大的那个。所以输入的字符转换成数字后,要从大到小降序排列。需要自定义一个比较算子cmp,其中return a>b;sort中使用该cmp算子。
AC代码
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
char s[26];
char num[26];
int vis[26];
int tmp[5];
int ans[26];
int flag;
int len;
long long target;
bool cmp(int a,int b)
{
return a>b;//降序a>b ;升序a<b
}
void dfs(long long n){//层数从0开始
if(n>=5){//凑齐5个数
if(flag==0&&(tmp[0]-pow(tmp[1],2)+pow(tmp[2],3)-pow(tmp[3],4)+pow(tmp[4],5)==target)){
for(int i=0;i<5;i++){
ans[i]=tmp[i];
//flag=1;
}
flag=1;
}
}
else{//不足5个数
for(int i=0;i<len;i++){//这5层要试12个数啊
if(vis[i]==0){//若这个数字每用过
tmp
=num[i];
vis[i]=1;
dfs(n+1);
vis[i]=0;
}
}
}
}
int main(int argc, char** argv) {
while(scanf("%lld %s",&target,s)==2){
if(target==0&&strcmp(s,"END")==0){//判断结束
break;
}
memset(num,0,sizeof(num));
memset(ans,0,sizeof(ans));
memset(tmp,0,sizeof(tmp));
memset(vis,0,sizeof(vis));
flag=0;
len=strlen(s);
for(int i=0;i<len;i++){//将字符转换为数字
num[i]=s[i]-'A'+1;
}
sort(num,num+len,cmp);
dfs(0);
if(flag==1){
for(int i=0;i<5;i++){
printf("%c",ans[i]-1+'A') ;
}
printf("\n");
}
else{
printf("no solution\n");
}
}
return 0;
}
d49e
tter 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 - w2+ x3- y4+ z5= target
"For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6 - 92+ 53- 34+ 25= 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."
"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 unique Klein combination, or 'no solution' if there is no correct combination. Use the exact format shown below."样例输入
1 ABCDEFGHIJKL 11700519 ZAYEXIWOVU 3072997 SOUGHT 1234567 THEQUICKFROG 0 END样例输出
LKEBA YOXUZ GHOST no solution来源Mid-Central USA 2002
题意 Klein语句通常包含5到12个不同的大写字母,通常在句子的开头,并提及一个或多个数字。五个大写字母形成打开保险箱的组合。通过以适当的方式组合来自所有数字的数字,您将获得一个数字目标。 (构建目标号码的细节是分类的。)要找到组合,您必须选择满足以下等式的五个字母v,w,x,y和z,其中每个字母由其在字母表中的顺序位置替换A = 1,B = 2,...,Z = 26)。这个组合就是vwxyz。如果有多个解决方案,那么这个组合就是词典上最大的那个,也就是最后出现在字典中的那个。“
题型/思路 标准的DFS题,通过深搜暴力试出最优解。试数题(每个数字只能使用一次),一般思路都是DFS+暴力。因为满足等式条件的解可能有多个,题目要求输出字典序最大的那个。所以输入的字符转换成数字后,要从大到小降序排列。需要自定义一个比较算子cmp,其中return a>b;sort中使用该cmp算子。
AC代码
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
char s[26];
char num[26];
int vis[26];
int tmp[5];
int ans[26];
int flag;
int len;
long long target;
bool cmp(int a,int b)
{
return a>b;//降序a>b ;升序a<b
}
void dfs(long long n){//层数从0开始
if(n>=5){//凑齐5个数
if(flag==0&&(tmp[0]-pow(tmp[1],2)+pow(tmp[2],3)-pow(tmp[3],4)+pow(tmp[4],5)==target)){
for(int i=0;i<5;i++){
ans[i]=tmp[i];
//flag=1;
}
flag=1;
}
}
else{//不足5个数
for(int i=0;i<len;i++){//这5层要试12个数啊
if(vis[i]==0){//若这个数字每用过
tmp
=num[i];
vis[i]=1;
dfs(n+1);
vis[i]=0;
}
}
}
}
int main(int argc, char** argv) {
while(scanf("%lld %s",&target,s)==2){
if(target==0&&strcmp(s,"END")==0){//判断结束
break;
}
memset(num,0,sizeof(num));
memset(ans,0,sizeof(ans));
memset(tmp,0,sizeof(tmp));
memset(vis,0,sizeof(vis));
flag=0;
len=strlen(s);
for(int i=0;i<len;i++){//将字符转换为数字
num[i]=s[i]-'A'+1;
}
sort(num,num+len,cmp);
dfs(0);
if(flag==1){
for(int i=0;i<5;i++){
printf("%c",ans[i]-1+'A') ;
}
printf("\n");
}
else{
printf("no solution\n");
}
}
return 0;
}
相关文章推荐
- 2017计算机学科夏令营上机考试 A:判决素数个数
- 2017计算机学科夏令营上机考试 C:岛屿周长
- 【LIS】2017计算机学科夏令营上机考试 E:怪盗基德的滑翔翼
- 2017计算机学科夏令营上机考试-B题 编码字符串(简单题)
- 【优先队列】2017计算机学科夏令营上机考试 G:实现堆结构
- 2012年北京大学计算机系夏令营上机考试
- 2015年北京大学软件工程学科优秀大学生夏令营上机考试---B:整数奇偶排序
- 2015年北京大学软件工程学科优秀大学生夏令营上机考试---A:整数序列的元素最大跨度值
- 百练 / 2017研究生推免上机考试 F:Prime Path
- 北京大学直博申请夏令营上机考试题目分析
- 北京大学计算机系2009应试硕士生上机考试(A-C)
- 北京大学计算机系2009应试硕士生上机考试(D-F)
- 百练 / 2017研究生推免上机考试 G:Eqs
- 2012下半年版无忧全国计算机等级考试超级模拟软件DVD合集三级PC技术部分上机练习第33套题汇编运行结果
- 中国科学院大学 2015 年招收攻读硕士学位研究生入学统一考试试题科目名称:计算机学科综合(专业)
- 【质因子分解】2017研究生推免上机考试 A:因子分解
- 百练 / 2017研究生推免上机考试 C:肿瘤检测
- 百练 / 2017研究生推免上机考试 I:月度开销
- 第45次全国计算机等级考试上机考试黄金校区安排表
- 2016河南大学计算机硕士研究生复试上机考试第2题