Topic Review (HDU-1015)safecracker
problem Description"The item is locked in a Klein safe behind a painting in the Second-floor library. Klein safes is extremely rare; Most of the them, along with Klein and he factory, were destroyed in world War Ii. Fortunately old Brumbaugh from knew Klein's secrets and wrote them down before he died. A Klein Safe has both distinguishing features:a combination lock that uses letters instead of numbers, and an engraved quo Tation on the door. A Klein Quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentence S, 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 to get a numeric target. (The details of the constructing the target number are classified.) To find the combination you must select five Letters V, W, X, y, and z so satisfy the following equation, where each let TER is replaced by it 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 are the one that's 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 is actually several solutions in this case, and the combination turns off to be lkeba. Klein thought it is safe to encode the combination within the engraving, because it could take months of effort to try Al L The possibilities even if you knew the secret. But the course computers didn ' t exist then. "
"Develop a program to find Klein combinations on preparation for field deployment. Use standard test methodology as per departmental regulations. Input consists of one or more lines containing a positive an integer target less than twelve million, a space, then at least Five and at the most twelve distinct uppercase letters. The last line would 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 C Ombination. Use the exact format shown below. "
Sample Input1 ABCDEFGHIJKL11700519 Zayexiwovu3072997 sought1234567 Thequickfrog0 END
Sample OutputLkebaYoxuzGHOSTNo solutionTopic Analysis:the meaning of the title is probably the input integer target and a string of 5-12 uppercase letters, the value of which is a=1,b=2,...,z=26. Find 5 letters to meet v-w^2 + x^3-y^4 + z^5 = target. and outputs the maximum number of dictionaries that satisfy the condition. it is easy to analyze the case only 5*4*3*2*1 ~ 12*11*10*9*8, so the method of brute force enumeration should not time out. (Dfs can also be used for this topic)There was a small case in the encoding, and sort (X,x+len,compare) was written in sort (x,x+len-1,compare) to cause an error when using sort fast.
1#include <iostream>2#include <string>3#include <algorithm>4 using namespacestd;5 6 BOOLCompareintAintb) {7 returnA>b;8 }9 Ten intMain () { One intN; A strings; - intx[ -]; - while(cin>>n>>s&& (n!=0&&s!="END")){ the intlen=s.length (); - for(intI=0; i<len;i++){ -x[i]=s[i]-'A'+1; - } +Sort (x,x+len,compare); - BOOLflag=false; + inta,b,c,d,e; A for(a=0; a<len;a++){ at for(b=0; b<len;b++){ - if(b==a) { - Continue; - } - for(c=0; c<len;c++){ - if(c==a| | c==b) { in Continue; - } to for(d=0;d <len;d++){ + if(d==a| | d==b| | d==c) { - Continue; the } * for(e=0; e<len;e++){ $ if(e==a| | e==b| | e==c| | e==d) {Panax Notoginseng Continue; - } the if(x[a]-x[b]*x[b]+x[c]*x[c]*x[c]-x[d]*x[d]*x[d]*x[d]+x[e]*x[e]*x[e]*x[e]*x[e]==N) { +flag=true; A Break; the } + } - if(flag) { $ Break; $ } - } - if(flag) { the Break; - }Wuyi } the if(flag) { - Break; Wu } - } About if(flag) { $ Break; - } - } - if(flag) { A Charans[5]; +ans[0]='A'+ (x[a]-1); theans[1]='A'+ (x[b]-1); -ans[2]='A'+ (x[c]-1); $ans[3]='A'+ (x[d]-1); theans[4]='A'+ (x[e]-1); the for(intI=0;i<5; i++){ thecout<<Ans[i]; the } -cout<<Endl; in}Else{ thecout<<"No solution"<<Endl; the } About } the return 0; the}
HDU-1015 safecracker (Violent enumeration)