题意:给定一个长度为5……12个不同字符组成的字符串,从中选取5个,设为v,w,x,y,z,要满足等式:v-w^2+x^3-y^4+z^5=target,现在给出字符串和target,求满足该条件的5个字符(字典序要最大)
我开始时忘记了需要字典序最大,所以造成了一些wa,这个错误是题目条件没有用完,以后小心
Safecracker
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1163 | Accepted: 727 |
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.
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
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 ABCDEFGHIJKL 11700519 ZAYEXIWOVU 3072997 SOUGHT 1234567 THEQUICKFROG 0 END
Sample Output
LKEBA YOXUZ GHOST no solution
Source
源代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
double char_2_int(char ch){
return ch - 'A' + 1;
}
double target;
char upper[15];
int main(){
while(scanf("%lf%s", &target, upper)&&(target != 0 || strcmp(upper,"END") != 0)){
sort(upper,upper+strlen(upper));
int sum;
int falg = 0;
string str;
for(int i = 0; upper[i]; i++){
for(int j = 0; upper[j]; j++){
for(int k = 0; upper[k];k++){
for(int l = 0; upper[l]; l++){
for(int m = 0; upper[m]; m++){
if(i != j && i != k && i != l && i != m && j != k &&
j != l && j != m && k != l && k != m && l != m){
sum = char_2_int(upper[i]) - char_2_int(upper[j]) *
char_2_int(upper[j]) + char_2_int(upper[k]) *char_2_int(upper[k]) *
char_2_int(upper[k]) - char_2_int(upper[l]) *char_2_int(upper[l]) *
char_2_int(upper[l]) *char_2_int(upper[l]) + char_2_int(upper[m]) *
char_2_int(upper[m]) *char_2_int(upper[m]) *char_2_int(upper[m]) *
char_2_int(upper[m]);
if(sum == target){
falg = 1;
str = "";
str = str + upper[i]+upper[j]+upper[k]+upper[l]+upper[m];
//printf("%c%c%c%c%c%c\n",upper[i],upper[j],upper[k],upper[l],upper[m]);
break;
}
}
}
}
}
}
}
if(falg == 1) {
printf("%s\n", str.c_str());
}
else printf("no solution\n");
}
return 0;
}