題目來源:http://noi.openjudge.cn/ch0201/250/ D:Safecracker
總時間限制: 1000ms 記憶體限制: 65536kB
描述
"The item islocked in a Klein safe behind a painting in the second-floor library. Kleinsafes are extremely rare; most of them, along with Klein and his factory, weredestroyed in World War II. Fortunately old Brumbaugh from research knew Klein'ssecrets and wrote them down before he died. A Klein safe has two distinguishingfeatures: a combination lock that uses letters instead of numbers, and anengraved quotation on the door. A Klein quotation always contains between fiveand twelve distinct uppercase letters, usually at the beginning of sentences,and mentions one or more numbers. Five of the uppercase letters form thecombination that opens the safe. By combining the digits from all the numbersin the appropriate way you get a numeric target. (The details of constructingthe target number are classified.) To find the combination you must select fiveletters v, w, x, y, and z that satisfy the following equation, where eachletter 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 thenthe combination is the one that is lexicographically greatest, i.e., the onethat would appear last in a dictionary."
v - w2+ x3- y4+ z5= target
"For example, given target 1 and letter set ABCDEFGHIJKL, one possiblesolution is FIECB, since 6 - 92+ 53- 34+ 25=1. There are actually several solutions in this case, and the combination turnsout to be LKEBA. Klein thought it was safe to encode the combination within theengraving, because it could take months of effort to try all the possibilitieseven if you knew the secret. But of course computers didn't existthen."
"Develop a program to find Klein combinations in preparation for fielddeployment. Use standard test methodology as per departmentalregulations.
輸入
Input consists of one or more lines containing a positiveinteger target less than twelve million, a space, then at least five and atmost twelve distinct uppercase letters. The last line will contain a target ofzero and the letters END; this signals the end of the input.
輸出
For each line output the unique Klein combination, or 'nosolution' if there is no correct combination. Use the exact format shownbelow."
範例輸入
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END
範例輸出
LKEBA
YOXUZ
GHOST
no solution
-----------------------------------------------------
解題思路
暴力枚舉
-----------------------------------------------------
代碼
#include<iostream>#include<vector>#include<string>#include<fstream>#include<algorithm>using namespace std;int main(){#ifndef ONLINE_JUDGEint n, i, len;int oper[5] = {0};char tmp[6] = {0};int ind[5] = {0};string s;bool has_solution;vector<int> vec;ifstream fin("xly2017D.txt");fin >> n >> s;while(s!="END"){vec.clear();has_solution = false;len = s.length();for (i=0; i<len; i++){vec.push_back((int)(s.at(i) - 'A' + 1));}sort(vec.begin(),vec.end());for (ind[0] = len-1; ind[0] >= 0; ind[0] -- ){for (ind[1] = len-1; ind[1] >= 0; ind[1]--){if(ind[1] != ind[0]){for (ind[2] = len-1; ind[2] >= 0; ind[2] --){if (ind[2] != ind[1] && ind[2] != ind[0]){for (ind[3] = len-1; ind[3] >=0; ind[3]--){if (ind[3]!=ind[2] && ind[3]!=ind[1] && ind[3]!=ind[0]){for (ind[4] = len-1; ind[4]>=0; ind[4]--){if(ind[4]!=ind[3] && ind[4]!=ind[2] && ind[4]!=ind[1] && ind[4]!=ind[0]){for (i=0; i<5; i++){oper[i] = vec[ind[i]];}if (oper[0]-oper[1]*oper[1]+oper[2]*oper[2]*oper[2]-oper[3]*oper[3]*oper[3]*oper[3]+oper[4]*oper[4]*oper[4]*oper[4]*oper[4]==n){for (i=0; i<5; i++){tmp[i] = (char) (oper[i] + 'A' - 1);}string ans(tmp);cout << ans << endl;has_solution = true;goto mark;}}}}}}}}}}mark:if (!has_solution){cout << "no solution" << endl;}fin >> n >> s;}fin.close();#endif#ifdef ONLINE_JUDGEint n, i, len;int oper[5] = {0};char tmp[6] = {0};int ind[5] = {0};string s;bool has_solution;vector<int> vec;cin >> n >> s;while(s!="END"){vec.clear();has_solution = false;len = s.length();for (i=0; i<len; i++){vec.push_back((int)(s.at(i) - 'A' + 1));}sort(vec.begin(),vec.end());for (ind[0] = len-1; ind[0] >= 0; ind[0] -- ){for (ind[1] = len-1; ind[1] >= 0; ind[1]--){if(ind[1] != ind[0]){for (ind[2] = len-1; ind[2] >= 0; ind[2] --){if (ind[2] != ind[1] && ind[2] != ind[0]){for (ind[3] = len-1; ind[3] >=0; ind[3]--){if (ind[3]!=ind[2] && ind[3]!=ind[1] && ind[3]!=ind[0]){for (ind[4] = len-1; ind[4]>=0; ind[4]--){if(ind[4]!=ind[3] && ind[4]!=ind[2] && ind[4]!=ind[1] && ind[4]!=ind[0]){for (i=0; i<5; i++){oper[i] = vec[ind[i]];}if (oper[0]-oper[1]*oper[1]+oper[2]*oper[2]*oper[2]-oper[3]*oper[3]*oper[3]*oper[3]+oper[4]*oper[4]*oper[4]*oper[4]*oper[4]==n){for (i=0; i<5; i++){tmp[i] = (char) (oper[i] + 'A' - 1);}string ans(tmp);cout << ans << endl;has_solution = true;goto mark;}}}}}}}}}}mark:if (!has_solution){cout << "no solution" << endl;}cin >> n >> s;}#endif}