HDU 1015 Safecracker DFS搜索

来源:互联网 发布:购买域名需要多少钱 编辑:程序博客网 时间:2024/05/17 09:08

Safecracker
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64

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 ABCDEFGHIJKL11700519 ZAYEXIWOVU3072997 SOUGHT1234567 THEQUICKFROG0 END
 

Sample Output

LKEBAYOXUZGHOSTno solution
 


题意:

A-Z分别映射为1-26,然后输入一个数字和一串字符。

然后,在这串字符里找出5个字符,然后用给的公司计算,如果出现等于给的值序列就输出,如果有多个,输出字典序最大的那一个。没有就输出 no solution。


解题思路:

因为要最大的字典序,那就先排个序,安字典序降序排列。然后暴力搜索就可以了。

另外在判断值是否满足条件的计算我自己写了一个快速幂,用math里的是double类型,在不做浮点误差处理的情况下会出现奇怪的错误.


AC代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n,len;const int maxn = 100;char str[maxn],ans[8];int vis[maxn], flag;bool cmp(char a, char b){    return a > b;}int qpow(int a,int b){    int r = 1,base = a;    while(b!=0){        if(b&1) r*=base;        base *= base;        b >>= 1;    }    return r;} //自己写的快速幂void dfs(int k){    for(int i = 0; i < len; i++){        if(vis[i]) continue;  //已经标记过,则搜索下一个        vis[i] = 1;        ans[k] = str[i];          if(k == 4){    //0-4一共五个数。            int tmp;            tmp = (ans[0] - 'A' + 1) - (qpow(ans[1] - 'A' + 1,2)) + (qpow(ans[2] - 'A' + 1,3)) - (qpow(ans[3] - 'A' + 1,4)) + (qpow(ans[4] - 'A' + 1,5));            if(tmp == n){                flag = 1;                return;            }            vis[i] = 0; //别忘了,不满足条件的话,在这里也要重置状态。            continue;        }        dfs(k+1);        vis[i] = 0;  //重置标记        if(flag)return;    }}int main(){    while(scanf("%d %s" ,&n ,str) && n && strcmp(str,"END")){        memset(vis,0,sizeof(vis));        flag = 0;        len = strlen(str);        sort(str,str+len,cmp); //按照字典序降序排列        dfs(0);        if(flag) cout << ans << endl;        else cout << "no solution" <<endl;    }    return 0;}



0 0
原创粉丝点击