问题描述:
=== 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."
这题的模型很常见,提供一些可用的数字和一个目标值,利用全排列找出满足条件的解。但这题的数据采集有点意思,它提供给你一些无序的字母,每个字母代表着相应的数字,要求你找出按照字典排序最大的那一组解,并输出出来。刚开始,我想的是把所有解找出来,通过比较,找到一个最大解。但我转念一想,这样肯定会超时,并且产生好多冗余的数据。回想起全排序的特性,完全可以先把字符串按降序排好,再进行DFS搜索,这样第一组解一定是最大的。
测试数据:
Input:
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END
Output:
LKEBA
YOXUZ
GHOST
no solution
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
long long n;
string s;
int book[30]; //标记
int temp[5]; //记录5个数据
int sSize; //字符串长度
int flag = 0;
int step = 0;
int num[30]; //字符转换过对应的数字
char ch[5];
bool cmp(char a, char b)
{
return a > b;
}
int chtn(char a)
{
return a - 64;
}
char numtc(int a)
{
return a + 64;
}
void dfs(int step)
{
if (flag)
return;
if (step == 5) {
if (n == temp[0] -
temp[1]*temp[1] +
temp[2]*temp[2]*temp[2] -
temp[3]*temp[3]*temp[3]*temp[3] +
temp[4]*temp[4]*temp[4]*temp[4]*temp[4]) {
flag = 1;
return;
}
else {
return;
}
}
//这里的!flag判断条件十分重要!!
for (int i = 0; i < sSize; i++) {
if (book[i] == 0 && !flag) {
book[i] = 1;
temp[step] = num[i];
dfs(step + 1);
book[i] = 0;
}
}
}
int main()
{
while (cin >> n >> s) {
if (n == 0 && s == "END") {
break;
}
sSize = s.length();
sort(s.begin(), s.end(), cmp);
flag = 0;
memset(book, 0, sizeof(book));
for (int i = 0; i < sSize; i++) {
num[i] = chtn(s[i]);
}
dfs(0);
if (!flag)
cout << "no solution" << endl;
else {
for (int i = 0; i < 5; i++) {
ch[i] = numtc(temp[i]);
cout << ch[i];
}
cout << endl;
}
}
return 0;
}