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."
译文:=== Op科技简报,2002/11/02 06:42 CST === 
“该物品被锁在二楼图书馆一幅油画后面的克莱恩保险柜中,克莱因保险柜非常罕见,其中大部分与克莱恩及其工厂一起在二战时被摧毁。幸运的是,老布鲁博从研究中知道克莱因在他去世前写下他们的秘密,一个克莱因保险箱有两个显着特点:一个使用字母而不是数字的组合锁,以及一扇刻在门上的刻字引号Klein引用总是包含5到12个不同的大写字母,通常在句子的开头,并提到一个或多个数字。大写字母组成打开的安全相结合,通过数字从所有的数字中,你得到一个数字目标适当的方式合并。(建设目标的细节五号码被分类。)要找到组合,您必须选择满足以下等式的五个字母v,w,x,y和z,其中每个字母由其在字母表中的顺序位置替换(A = 1,B = 2,...,Z = 26)。这个组合就是vwxyz。如果有多个解决方案,那么这个组合就是词典上最大的那个,也就是最后出现在字典中的那个。“v - w ^ 2 + x ^ 3 - y ^ 4 + z ^ 5 =目标”例如,给定目标1和字母集ABCDEFGHIJKL,一个可能的解决方案是FIECB,因为6 - 9 ^ 2 + 5 ^ 3 - 3 ^ 4 + 2 ^ 5 = 1。在这种情况下实际上有几个解决方案, Klein认为在雕刻中对组合进行编码是安全的,因为即使你知道秘密也需要数月的努力来尝试所有的可能性,但当然电脑当时并不存在。”
=== Op技术指令,计算机部门,2002/11/02 12:30 CST ===
“制定一个程序,以发现Klein组合,为野外部署做准备,按照部门规定使用标准测试方法,输入包含一个或多个行,其中包含一个不超过1200万的正整数,一个空格,然后至少5个,最多 十二个不同的大写字母,最后一行将包含零和字母END的目标;这表示输入结束,对于每行输出的克莱因组合,断开与字典顺序的关系,或者如果没有 正确的组合,请使用下面显示的确切格式。“
Sample Input
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END
Sample Output
LKEBA
YOXUZ
GHOST
no solution
解题思路:题目的意思就是给你一个字母字符串,从中找出5个字母组成最大字典序,并且满足那个表达式,深搜算法即可解决。注意慎用pow函数会有误差,此处用了一个函数单独判断是否满足表达式,当表达式满足后还要判断当前的字符串t是否大于字符串g,是的话就拷贝覆盖字符串g,g就成为最大的字典序字符串。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
char s[],t[],g[];//s是输入的一串,t是临时保存字母,g是存放最终的5个字母(最大字典序)
int target,len,a[];//a数组来保存字母对应的数字
bool vis[];
bool isobj(int v,int w,int x,int y,int z,int tar){
if(v - w*w + x*x*x - y*y*y*y + z*z*z*z*z == tar) return true;
else return false;
}
void dfs(int k)
{
if(k==){//边界条件
if(isobj(a[t[]-'A'],a[t[]-'A'],a[t[]-'A'],a[t[]-'A'],a[t[]-'A'],target) && strcmp(t,g)>)
strcpy(g,t);//与目标相等时,还要按照字典序较大的输出
return;//回溯,返回上一层
}
for(int i=;i<len;i++){//遍历字符串
if(!vis[s[i]-'A']){//如果当前字母没有访问
t[k]=s[i];//先存到t数组中
vis[s[i]-'A']=true;//并标记为暂时的访问过
dfs(k+);//从每个当前位置进行深搜
vis[s[i]-'A']=false;//不满足条件的把原来标记的记为未访问过(回溯)
}
}
}
int main()
{
for(int i=;i<;i++)a[i]=i+;//对26个字母进行编号赋值
while(cin>>target>>s && (target||strcmp(s,"END"))){
memset(vis,false,sizeof(vis));//初始化为假值,表示都未访问过
memset(g,'\0',sizeof(g));//全部赋为'\0',便于拷贝和整体输出
memset(t,'\0',sizeof(t));
len=strlen(s);//目标字符串的长度
dfs();//从第0个目标字符开始深搜
if(strlen(g)==)cout<<"no solution"<<endl;//如果g的长度仍是0,则没有解
else cout<<g<<endl;//否则输出g字符串
}
return ;
}

以上是深搜目标字符串,找出其中最大字典序字符串,但其实我们只需要将目标字符串中的字母进行降序排列,然后深搜找到第一个满足表达式的字符串,即为最大字典序字符串。AC解法二代码:

 #include<bits/stdc++.h>
using namespace std;
char s[],t[];//s是输入的一串,t是临时保存字母
int target,len,a[];//a数组来保存字母对应的数字
bool flag,vis[];//flag标记是否找到
bool isobj(int v,int w,int x,int y,int z,int tar){
if(v - w*w + x*x*x - y*y*y*y + z*z*z*z*z == tar) return true;
else return false;
}
void dfs(int k)
{
if(k==){//边界条件
if(isobj(a[t[]-'A'],a[t[]-'A'],a[t[]-'A'],a[t[]-'A'],a[t[]-'A'],target) && !flag){
flag=true;cout<<t<<endl;//找到第一个字符串直接输出即可,此时就是最大字典序
}
return;//直接返回
}
for(int i=len-;i>=;--i){//从后往前遍历字符串
if(!vis[s[i]-'A']){//如果当前字母没有访问
t[k]=s[i];//先存到t数组中
vis[s[i]-'A']=true;//并标记为暂时的访问过
dfs(k+);//从每个当前位置进行深搜
vis[s[i]-'A']=false;//不满足条件的把原来标记的记为未访问过(回溯)
}
}
}
int main()
{
for(int i=;i<;i++)a[i]=i+;//对26个字母进行编号赋值
while(cin>>target>>s && (target||strcmp(s,"END"))){
memset(vis,false,sizeof(vis));//初始化为假值,表示都未访问过
memset(t,'\0',sizeof(t));
len=strlen(s);//目标字符串的长度
flag=false;sort(s,s+len);//排序
dfs();//从第0个目标字符开始深搜
if(!flag)cout<<"no solution"<<endl;//找不到
}
return ;
}

题解报告: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 水题一枚

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

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

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

  4. HDU 1015 Safecracker【数值型DFS】

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

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

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

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

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

  7. HDU 1015 Safecracker

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

  8. HDOJ/HDU 1015 Safecracker(深搜)

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

  9. HDOJ/HDU 1015 Safecracker(枚举、暴力)

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

随机推荐

  1. Frequent values(poj 3368)

    题意:给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之间连续出现次数最多的次数. 代码: /* rmq算法 当询问到x,y时,设在x之后并且与a[x]相同的最后一个数编号为t,那么x到t之 ...

  2. SQL SERVER 2012 第三章 T-SQL 基本SELECT语句用法,Where子句详细用法

    select [all|distinct] [top (<expression>) [Percent] [with ties]] <column list> [from < ...

  3. Kerberos认证浅析

    1 引言 在希腊神话中Kerberos是守护地狱之门的一条凶猛的三头神犬,而我们在本文中所要介绍的Kerberos认证协议是由美国麻省理工学院(MIT)首先提出并实现的,是该校雅典娜计划的一部分.这个 ...

  4. 笔记:Javac编译器

    Javac编译器是把 *.java 文件转换为 *.class 文件,是一个前端编译器:对应着有一种把字节码转变为机器码的编译器,称为JIT编译器(Just In Time Compiler),比如 ...

  5. 【结果发布】第六届SeedCoder编程大赛初赛结果发布

    微软俱乐部科技文化月seedcoder2014编程大赛已经初审完成. 评审小组选出最棒的作品进入决赛(现场答辩+陈述环节,由评委现场打分).终于排名由"初赛分数+现场答辩分"决定. ...

  6. Cocos2d-x 脚本语言Lua基本数据结构-表(table)

    Cocos2d-x 脚本语言Lua基本数据结构-表(table) table是Lua中唯一的数据结构.其它语言所提供的数据结构,如:arrays.records.lists.queues.sets等. ...

  7. 关于mysql建立索引 复合索引 索引类型

    这两天有个非常强烈的感觉就是自己在一些特别的情况下还是hold不住,脑子easy放空或者说一下子不知道怎么去分析问题了,比方,问"hash和btree索引的差别",这非常难吗.仅仅 ...

  8. [办公应用]如何制作二Y轴图(excel)

    有时候我们会遇到一种图表,就是X轴一致,可是Y轴的数据相差很大.如下图中,年龄和收入就不是一个数量级,在图表中显示的时候,“年龄”的曲线根本看不到(表中数据仅供举例): 解决的方法就是使用双Y轴显示, ...

  9. convex hull

    1 什么是convex hull 就是凸包,是计算几何中的一个概念,计算几何是计算机图形学的基础之一. 对于二维平面来说是这样的:对于二维平面上的点集,凸包是位于最外层的点构成的包围其它所有的点的凸多 ...

  10. python2.x里unicode错误问题

    import sys reload(sys) sys.setdefaultencoding('utf8')