题目回顾(HDU-1015)
Safecracker
Problem 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 - 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."
"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
题目分析:
题目的意思大概是,输入整数target和一串由5-12个大写字母表示的字符串,字母的值分别是A=1,B=2,...,Z=26。从中找出5个字母,满足v - w^2 + x^3 - y^4 + z^5 = target。并输出满足条件的字典序最大的情况。
容易分析出计算的情况只有5*4*3*2*1 ~ 12*11*10*9*8种,因此通过暴力枚举的方法应该不会超时。(此题也可以使用DFS)
在编码中出现过一个小情况,使用sort快排的时候,sort(x,x+len,compare)写成了sort(x,x+len-1,compare)导致出错。
1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 using namespace std; 5 6 bool compare(int a,int b){ 7 return a>b; 8 } 9 10 int main(){ 11 int n; 12 string s; 13 int x[13]; 14 while(cin>>n>>s&&(n!=0&&s!="END")){ 15 int len=s.length(); 16 for(int i=0;i<len;i++){ 17 x[i]=s[i]-‘A‘+1; 18 } 19 sort(x,x+len,compare); 20 bool flag=false; 21 int a,b,c,d,e; 22 for(a=0;a<len;a++){ 23 for(b=0;b<len;b++){ 24 if(b==a){ 25 continue; 26 } 27 for(c=0;c<len;c++){ 28 if(c==a||c==b){ 29 continue; 30 } 31 for(d=0;d<len;d++){ 32 if(d==a||d==b||d==c){ 33 continue; 34 } 35 for(e=0;e<len;e++){ 36 if(e==a||e==b||e==c||e==d){ 37 continue; 38 } 39 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){ 40 flag=true; 41 break; 42 } 43 } 44 if(flag){ 45 break; 46 } 47 } 48 if(flag){ 49 break; 50 } 51 } 52 if(flag){ 53 break; 54 } 55 } 56 if(flag){ 57 break; 58 } 59 } 60 if(flag){ 61 char ans[5]; 62 ans[0]=‘A‘+(x[a]-1); 63 ans[1]=‘A‘+(x[b]-1); 64 ans[2]=‘A‘+(x[c]-1); 65 ans[3]=‘A‘+(x[d]-1); 66 ans[4]=‘A‘+(x[e]-1); 67 for(int i=0;i<5;i++){ 68 cout<<ans[i]; 69 } 70 cout<<endl; 71 }else{ 72 cout<<"no solution"<<endl; 73 } 74 } 75 return 0; 76 }
原文地址:https://www.cnblogs.com/orangecyh/p/9769836.html