HDU 1015 Safecracker

12 篇文章 0 订阅
9 篇文章 0 订阅

题目

hduoj1015

Safecracker

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

一、题目大意

=Op技术简报,2002/11/02 06:42 CST=
“物品被锁在二楼图书馆一幅画后面的克莱恩保险柜里。克莱因的保险柜极为罕见,其中大部分连同克莱因和他的工厂一起在二战中被毁。幸运的是,研究所的老布鲁博知道克莱恩的秘密,并在他死前把秘密写了下来。克莱恩保险柜有两个显著的特点:一个是用字母代替数字的组合锁,另一个是门上刻着引号。克莱因语录通常包含五到十二个不同的大写字母,通常在句首,并提到一个或多个数字。五个大写字母组成了打开保险箱的组合。通过以适当的方式组合所有数字中的数字,可以得到一个数字目标。(构建目标编号的详细信息已分类。)要找到组合,必须选择满足以下等式的五个字母v、w、x、y和z,其中每个字母都替换为字母表中的顺序位置(A=1,B=2,…,z=26)。然后组合为vwxyz。如果有一个以上的解决方案,那么这个组合就是词典中最伟大的一个,即在词典中最后出现的一个。”
v-w2+x3-y4+z5=目标
“例如,给定目标1和字母集ABCDEFGHIJKL,一个可能的解决方案是FIECB,因为6-92+53-34+25=1。在这种情况下,实际上有几种解决方案,而组合结果是LKEBA。克莱恩认为在雕刻中对这种组合进行编码是安全的,因为即使你知道这个秘密,也要花上几个月的时间来尝试所有的可能性。当然,那时计算机还不存在。”
=Op技术指令,计算机部,2002/11/02 12:30 CST=
“制定一个计划,寻找克莱因组合,为实地部署做准备。按照部门规定使用标准测试方法。输入由一行或多行组成,其中包含一个小于1200万的正整数目标、一个空格、至少五个且最多十二个不同的大写字母。最后一行将包含一个目标零和字母END;这表示输入结束。对于每一行输出Klein组合,按字典顺序断开连接,如果没有正确的组合,则“无解决方案”。使用下面显示的精确格式。”

二、代码

总的来说就是要用所给出的字符串按(A=1, B=2, …, Z=26)的规则通过固定的公式去实现所给的目标值,现在要你找到哪几个字母可以实现。

代码如下(示例):

#include <stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int cmp(char a,char b)
{
    return a>b;
}
char map[300];
int chun[300];//用于共式计算的数组
int visit[300];//标记数组
int len,ans;
bool judge;
//judge>0
//num==5,符合公式
//深度搜索长度为len

void dfs(int num)//num是数组chun的下标
{
if(judge) return ;
if(num==5)
{
   if(ans == chun[0] - chun[1]*chun[1] + chun[2]*chun[2]*chun[2] - chun[3]*chun[3]*chun[3]*chun[3] + chun[4]*chun[4]*chun[4]*chun[4]*chun[4])
   {
       judge=1;
   }
   return ;
}
for(int i=0;i<len;i++)
{
    if(visit[i]==0&&!judge)
    {
    chun[num]=map[i];
    visit[i]=1;
    dfs(num+1);
    visit[i]=0;
    }
}
    return ;
}
int main(int argc, char *argv[])
{
    while(scanf("%d %s",&ans,map)!=EOF)
    {
        int i;
        if(ans==0&&strcmp("END",map)==0)
        {
            break;
        }
        len=strlen(map);
        sort(map,map+len,cmp);//字母按ascll码降序排序
        memset(visit,0,sizeof(visit));
        for(i=0;i<len;i++)
        {
            map[i]=map[i]-'A'+1;//字符转数字
        }
        judge=false;
        dfs(0);
        if(judge)
        {
            for(i=0;i<5;i++)
            printf("%c",chun[i]-1+'A');
        }
        else
        printf("no solution");
        printf("\n");

    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值