2017计算机学科夏令营上机考试 D:Safecracker DFS

总时间限制:
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 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 - 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

题意
Klein语句通常包含5到12个不同的大写字母,通常在句子的开头,并提及一个或多个数字。五个大写字母形成打开保险箱的组合。通过以适当的方式组合来自所有数字的数字,您将获得一个数字目标。 (构建目标号码的细节是分类的。)要找到组合,您必须选择满足以下等式的五个字母v,w,x,y和z,其中每个字母由其在字母表中的顺序位置替换A = 1,B = 2,…,Z = 26)。这个组合就是vwxyz。如果有多个解决方案,那么这个组合就是词典上最大的那个,也就是最后出现在字典中的那个。“

思路
1 试数题(每个数字都不能重复)一般思路都是DFS+暴力
2 DFS的死胡同和岔道口思想
3 DFS visit数组思想
4 sort的用法(起,止,比较)
5 pow(x,y)返回double 型的数值(x的y次方,类似的还有exp(x),e的x次方),如果对结果进行加减需要强制类型转换

代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;
//五个因子,幂分别是12345,多个解按照字典序降序排列
//深度优先搜索
char zimu[27]={'a','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'
,'U','V','W','X','Y','Z'};
char factor[26];

bool visit[26];
int num[26];
int temp[5];
int ans[26];
long long int target;
int len;
bool flag=0;

bool cmp(int a,int b)
{
    return a>b?1:0;
}
void dfs(int n)
{
    if(n>=5)//死胡同
    {
        if((target==int(pow(temp[0],1))-int(pow(temp[1],2))+int(pow(temp[2],3))-int(pow(temp[3],4))+int(pow(temp[4],5)))&&flag==0) //这里flag==0保证是第一个(也就是字典序最大的)
        {//注意pow返回double型,若进行加减运算可能会比正确值+-1
            flag=1;
            for(int i=0;i<5;i++)
            {
                ans[i]=temp[i];
            }
        }
    }
    else//否则就是岔道口
    {
        for(int i=0;i<len;i++)//相当于遍历从进入点引出的边,有len种可能
        {
            if(visit[i]==0)
            {
                visit[i]=1;
                temp[n]=num[i];
                dfs(n+1);
                visit[i]=0;
            }
        }
    }
}

int main()
{
    //freopen("input.txt","r",stdin);
    while(cin>>target>>factor)
    {
        flag=0;
        if(target==0&&strcmp(factor,"END")==0) break; //c中比较字符串是==0为匹配成功
        memset(visit,0,sizeof(visit));
        memset(num,0,sizeof(num));
        memset(temp,0,sizeof(temp));
        memset(ans,0,sizeof(ans));
        len = strlen(factor);
        for(int i=0;i<len;i++)
        {
            num[i]=factor[i]-'A'+1;
        }
        sort(num,num+len,cmp);//sort用法,有三个参数,降序a>b,升序a<b
        dfs(0);
        if(flag)
        {
            for(int i=0;i<5;i++)
            {
                printf("%c",'A'+ans[i]-1);
            }
            cout<<endl;
        }
        else
        {
            cout<<"no solution"<<endl;
        }

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值