|
SafecrackerTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9270 Accepted Submission(s): 4701
Problem 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
Sample Output
|
哎学好英语很必须啊!题意纠结了好久!
题意:先输入一个值给 target 和一串大写字母(12个内包含12个)给字符串,然后按 A=1,B=2,......Z=26 的字典序列,搜索5个字母,若满足式子 v-w^2+x^3-y^4+z^5=target , 按字典序列由大到小输出5个字母,否则输出 no solution。当输入为 0 和 END 时结束。注意:每个输入输出字母不能重复!!
一开始想到用暴力穷举法,AC代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int target=0,target_1=0,i=0,v=0,w=0,x=0,y=0,z=0,k=0,number[26]={0};
char letters_2,letters[27]={'0'},letters_1[27]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
while(scanf("%d %s",&target,&letters)!=EOF){
if(target==0&&!strcmp(letters,"END"))break;
k=0;
for(i=0;letters[i]!='\0';i++){}//输入字母个数
for(v=0;v<i;v++){//为每个字母拟值
for(w=0;w<26;w++){
if(letters[v]==letters_1[w]){
number[v]=w+1;
}
}
}
for(v=0;v<i;v++){//按值大小排列字母
for(w=v+1;w<i-1;w++){
if(number[v]>number[w]){
x=number[v];
number[v]=number[w];
number[w]=x;
letters_2=letters[v];
letters[v]=letters[w];
letters[w]=letters_2;
}
}
}
for(v=i-1;k==0&&v>=0;v--){//按字典序列由大到小搜索
for(w=i-1;k==0&&w>=0;w--){
if(w!=v)
for(x=i-1;k==0&&x>=0;x--){
if(x!=w&&x!=v)
for(y=i-1;k==0&&y>=0;y--){
if(y!=x&&y!=w&&y!=v)
for(z=i-1;k==0&&z>=0;z--){
if(z!=y&&z!=x&&z!=w&&z!=v){
target_1=number[v]-number[w]*number[w]+number[x]*number[x]*number[x]-number[y]*number[y]*number[y]*number[y]+number[z]*number[z]*number[z]*number[z]*number[z];
if(target==target_1){
k=1;
printf("%c%c%c%c%c\n",letters[v],letters[w],letters[x],letters[y],letters[z]);
}
}
}
}
}
}
}
if(k==0){
printf("no solution\n");
}
}
}
而后想到另一个剪枝搜索想法:
v - target ========================== (w^2+y^4) - (x^3+z^5)
v-target = 奇 - 奇 ==== (w^2+y^4) - (x^3+z^5) = 偶 == (偶) - (偶) , 即x+z=偶;
v-target = 偶 - 奇 ==== (w^2+y^4) - (x^3+z^5) = 奇 == (奇) - (奇) , 即x* z=奇;
v-target = 奇 - 偶 ==== (w^2+y^4) - (x^3+z^5) = 奇 == (奇 ) - (奇) , 即x* z=奇;
v-target = 偶 - 偶 ==== (w^2+y^4) - (x^3+z^5) = 偶 == (偶) - (偶) , 即x+z=偶;