poj 1248 Safecracker(暴力枚举)

来源:互联网 发布:linux安装mrtg详解 编辑:程序博客网 时间:2024/05/16 18:55
Safecracker
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2178 Accepted: 1231

Description

"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

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.

Output

For each line output the unique Klein combination, 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

LKEBAYOXUZ   //这组数据有问题,我的程序的结果是"no solution"(已Ac)GHOSTno solution题目大意:A,B,C,D......Z这26个大写字母的值依次为1,2,3.......26.给你个数target和可选字符表,在表中找5个字符,是否满足v - w2 + x3 - y4 + z5 = target,若没有,则输出no solution,否则按照最大字典序输出5个字符。注:最大字典序:先排大的,然后才是小的(从小到大严格递减),例如a,d,c,z,h的最大字典序为z,h,d,c,a思路:先将可选字符串集合按照从大到小的方式排序,然后就是5重循环依次判断,若找到,输出并立刻跳出代码:
/*    @author : liuwen*/#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <vector>#include <cmath>using namespace std;bool cmp(const char &a,const char &b){    return a>b;}int fun(int  v,int  w,int  x,int  y,int z){    return v - pow(w*1.0,2.0) + pow(x*1.0,3.0) - pow(y*1.0,4.0) + pow(z*1.0,5.0);}int main(){    //freopen("in.txt","r",stdin);    int a,b,c,d,e,target;    char src[50];    while(scanf("%d%s",&target,src)==2){        if(target==0&&strcmp(src,"END")==0) break;        int len=strlen(src);        sort(src,src+len,cmp);//从大到小排序        bool isFind=false;        for(a=0;a<len;a++){            for(b=0;b<len;b++) if(b!=a){                for(c=0;c<len;c++) if(c!=b&&c!=a){                    for(d=0;d<len;d++) if(d!=c&&d!=b&&d!=a){                        for(e=0;e<len;e++)if(e!=d&&e!=c&&e!=b&&e!=a){                            if(fun(src[a]-'A'+1,src[b]-'A'+1,src[c]-'A'+1,src[d]-'A'+1,src[e]-'A'+1)==target){                                isFind=true;                                break;                            }                            if(isFind)  break;                        }                        if(isFind)  break;                    }                    if(isFind)  break;                }                if(isFind)  break;            }            if(isFind)  break;        }        if(!isFind) printf("no solution\n");        else{            printf("%c%c%c%c%c\n",src[a],src[b],src[c],src[d],src[e]);        }    }    return 0;}


原创粉丝点击