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 Input
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END
 
Sample Output
LKEBA
YOXUZ
GHOST
no solution
 
Source

水题一枚。呵呵,纯粹暴力法过的。

使用C++比C能够大大降低出错的可能哦。

注意:要求按最大的字典序输出。故此要排序。

奉献给个小技巧:

sort(str.rbegin(), str.rend());

这个rbegin是逆序的iterator了,故此这样排序处理就是逆序排序的字典序了。

#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std; __int64 num;
string str;
__int64 a, b, c, d, e; inline __int64 cal()
{
return a - b*b + c*c*c - d*d*d*d + e*e*e*e*e;
} bool getCombination()
{
for (int i = 0; i < (int)str.size(); i++)
{
a = str[i] - 'A' + 1;
for (int j = 0; j < (int)str.size(); j++)
{
if (j == i) continue;
b = str[j] - 'A' + 1;
for (int k = 0; k < (int)str.size(); k++)
{
if (k == i || k == j) continue;
c = str[k] - 'A' + 1;
for (int u = 0; u < (int)str.size(); u++)
{
if (u == k || u == i || u ==j) continue;
d = str[u] - 'A' + 1;
for (int v = 0; v < (int)str.size(); v++)
{
if (v == u || v == k || v == i || v == j) continue;
e = str[v] - 'A' + 1;
if (cal() == num) return true;
}
}
}
}
}
return false;
} int main()
{
while (cin>>num && cin>>str)
{
if (num == 0LL && str == "END") break;
sort(str.rbegin(), str.rend());
if (getCombination())
cout<<char(a+'A'-1)<<char(b+'A'-1)<<char(c+'A'-1)<<char(d+'A'-1)
<<char(e+'A'-1)<<endl;
else cout<<"no solution\n";
}
return 0;
}

版权声明:笔者靖心脏,景空间地址:http://blog.csdn.net/kenden23/,只有经过作者同意转载。

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 (DFS)

    题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...

  8. HDU 1015 Safecracker

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

  9. HDOJ/HDU 1015 Safecracker(深搜)

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

随机推荐

  1. python 下的数据结构与算法---8:哈希一下【dict与set的实现】

    少年,不知道你好记不记得第三篇文章讲python内建数据结构的方法及其时间复杂度时里面关于dict与set的时间复杂度[为何访问元素为O(1)]原理我说后面讲吗?其实就是这篇文章讲啦. 目录: 一:H ...

  2. (转)xml节点和元素的关系 .

    XML节点是什么呢?当我们在处理XML文件的时候必须要明白XML节点的概念,那么从这里的讲述,你将会了解XML节点对于XML文件的意义,希望对你有所帮助. 在我们学习LINQ删除XML节点之前我们先来 ...

  3. poj2236 基础并查集

    题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O ...

  4. decode()与case then 学习与使用

    今天做项目的时候遇到一个oracle数值转换的问题,按需求需要对匹配系统时间进行固定赋值,为了避免增加复杂度并易于维护,尽量不要使用存储过程或触发器,最好是使用oracle 自带函数. 如: SQL& ...

  5. 初始化JQuery方法与(function(){})(para)匿名方法介绍

    一.初始化JQuery对象 DOM加载完成时运行代码 1.$(document).ready(function(){ 全写 // 在这里写你的代码... }); 2.jQuery(function() ...

  6. javascript回调函数

    function $$(f) { if (typeof f == 'function') {//f是一个函数 f(); } else { alert('not a function'); } } $$ ...

  7. 查看文件系统类型的Linux命令

    不需挂载就能查看的命令:   1. file  [root@localhost dev]# file -s /dev/sda1 /dev/sda1: Linux rev 1.0 ext4 filesy ...

  8. var genreModel =storeDB.Genres.Include("Albums").Single(g => g.Name == genre);是什么意思?

    g => g.Name == genre代表一个匿名函数.即这里向Single方法传入了一个方法类型的参数. =>左边的g代表方法的参数,可以有多个,如(g,f) => ...,=& ...

  9. 将日志写入Debug窗口

    定义在NuGet包“Microsoft.Extensions.Logging.Debug”中的DebugLogger会直接调用Debug的WriteLine方法来写入分发给它的日志消息.如果需要使用D ...

  10. 复杂事件处理引擎—Esper入门(第二弹)

    说明: 以下内容,可以参考Esper官方网站<Qucik start & Tutorial >(顺序做了部分调整). PS:因为英语水平有限(大学期间刚过CET4的英语小盲童一枚) ...