题解报告:hdu 1015 Safecracker
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."
“该物品被锁在二楼图书馆一幅油画后面的克莱恩保险柜中,克莱因保险柜非常罕见,其中大部分与克莱恩及其工厂一起在二战时被摧毁。幸运的是,老布鲁博从研究中知道克莱因在他去世前写下他们的秘密,一个克莱因保险箱有两个显着特点:一个使用字母而不是数字的组合锁,以及一扇刻在门上的刻字引号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认为在雕刻中对组合进行编码是安全的,因为即使你知道秘密也需要数月的努力来尝试所有的可能性,但当然电脑当时并不存在。”
#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的更多相关文章
- HDOJ(HDU).1015 Safecracker (DFS)
HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...
- hdu 1015 Safecracker 水题一枚
题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein s ...
- 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 ...
- HDU 1015 Safecracker(第一次用了搜索去遍历超时,第二次用for循环能够了,思路一样的)
Safecracker Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total S ...
- HDU 1015.Safecracker【暴力枚举】【8月17】
Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is lo ...
- HDU 1015 Safecracker
解题思路:这题相当诡异,样例没过,交了,A了,呵呵,因为理论上是可以通过的,所以 我交了一发,然后就神奇的过了.首先要看懂题目. #include<cstdio> #include< ...
- HDOJ/HDU 1015 Safecracker(深搜)
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...
- HDOJ/HDU 1015 Safecracker(枚举、暴力)
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...
随机推荐
- 51Nod——T 1686 第K大区间
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...
- HttpClientUtil 工具类 实现跨域请求数据
package com.xxx.common.util; import java.io.IOException; import java.net.URI; import java.util.Array ...
- JVM定位程序假死或cpu占用高的线程
linux系统: 参考:https://blog.csdn.net/qq_40197576/article/details/80287515 1>使用top命令查看占用cpu进程情况,得到jav ...
- 选择器的使用(first-child和last-child选择器)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- Ionic3 填坑记录 - java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
1 错误:Unable to merge dex 执行打包命令时 ionic cordova build android --prod 报如下错误 2 原因 重复引用了同一个包 如上图所示, com ...
- pycharm支持react
安装nodejs插件 使能node 出现下面的变化,在scope里可以定义使用的范围 创建react项目 使能eslint规则检查功能 配置前端启动脚本: https://www.jetbrains. ...
- Android: Mac无法找到Android SDK问题
通过brew cask install android-sdk后,Intellij Idea中设置Android SDK路径失败,解决方法如下: /usr/local/Caskroom/android ...
- Django学习系列之Form表单结合ajax
Forms结合ajax Forms的验证流程: 定义用户输入规则的类,字段的值必须等于html中name属性的值(pwd= forms.CharField(required=True)=<i ...
- http://www.cnblogs.com/sprinkle/
http://www.cnblogs.com/sprinkle/ http://www.cnblogs.com/sprinkle/
- Linux 内核开发 - 内存管理
1.1什么是内存管理 内存管理是对计算机内存进行分配和使用的技术.内存管理主要存在于多任务的操作系统中,因为内存资源极其有限.须要在不同的任务之间共享内存,内存管理的存在就是要高效.高速的非配内存,并 ...