HDU1015 Safecracker

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

1 ABCDEFGHIJKL 11700519 ZAYEXIWOVU 3072997 SOUGHT 1234567 THEQUICKFROG 0 END

Sample Output

LKEBA YOXUZ GHOST no solution

DFS递归枚举出各种可能的排列组合情况,计算其数值是否为目标值,如果符合且该组合字典序大于记录的其它答案组合,则更新答案记录!

#include<iostream>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iterator>
#define ll long long
using namespace std;
int n, flag, len, vis[30], ans1[6];    //vis 用来标记s[i]是否使用了,ans1用来记录字母对应的数字
string s;
char box[6],ans[6];
bool fun()                         //计算是否符合目标值,不要用POW函数有误差!
{
	for (int i = 0; i <5; i++)
		ans1[i] = ans[i] - 64;
	int res = ans1[0] - ans1[1] * ans1[1] + ans1[2] * ans1[2] * ans1[2] - ans1[3] * ans1[3] * ans1[3] * ans1[3] + ans1[4] * ans1[4] * ans1[4] * ans1[4] * ans1[4];
	if (res == n)
		return true;
	return false;
}
void dfs(int sum)
{
	if (sum == 5)                     //递归出口,选满五个字母了
	{
		if (fun())                //符合目标值且字典序更大,则更新答案
		{
			flag = 1;
			if (strcmp(ans, box) > 0)
				strcpy(box,ans);
		}
		return;
	}
	for (int i = 0; i < len; i++)
	{
		if (vis[i] == 0)         //遍历S如果有没有用过的则使用标记,记得递归出来后需要回溯
		{
			ans[sum] = s[i];
			vis[i] = 1;
			dfs(sum + 1);
			vis[i] = 0;
		}
	}
}
int main()
{
	while (cin >> n >> s)
	{
		if (n == 0&&s=="END")
			break;
	    flag = 0;
		len = s.size();
		memset(vis, 0, sizeof(vis));     //重置一下标记数组和答案数组
		strcpy(box,"@");
		dfs(0);
		if (flag)
		{
			for (int i = 0; i <5; i++)
				cout << box[i];
			cout << endl;
		}
		else
			cout << "no solution" << endl;
	}
	return 0;
}

JAVA版:


import java.util.*;

public class Main {
	 static char box[]=new char[6],ans[]=new char[6];
	 static int ans1[]=new int[6],vis[]=new int[30];
	 static int tar;public static String s;
	 static boolean flag=false;
	public static boolean fun()
	{
		for(int i=0;i<5;i++)
		 ans1[i]=ans[i]-64;
		int res = ans1[0] - ans1[1] * ans1[1] + ans1[2] * ans1[2] * ans1[2] - ans1[3] * ans1[3] * ans1[3] * ans1[3] + ans1[4] * ans1[4] * ans1[4] * ans1[4] * ans1[4];
		if(res==tar)
			return true;
		else
			return false;
	}
	public static void dfs(int sum)
	{
		if(sum==5) {
			if(fun())
			{
				flag=true;
				String boxstr=box.toString();
				String ansstr=ans.toString();
				if(ansstr.compareTo(boxstr)>0) {
					for(int i=0;i<5;i++)
						box[i]=ans[i];
				}
			}
			return;
		}
		for(int i=0;i<s.length();i++) {
			if(vis[i]==0) {
				vis[i]=1;
				ans[sum]=s.charAt(i);
				dfs(sum+1);
				vis[i]=0;
			}
		}
	}
	public static void main(String[] args)
	{
	   Scanner sc=new Scanner(System.in);
	   while(sc.hasNext()) {
	   tar=sc.nextInt();
	   s=sc.next();
	   if(tar==0&&s.equals("END"))
		   break;
	   Arrays.fill(vis, 0);box[0]='@';
	   flag=false;
	   dfs(0);
	   if(flag) {
		   for(int i=0;i<5;i++)
			   System.out.print(box[i]);
		   System.out.println();
	   }
	   else
		   System.out.println("no solution");
	   }
	}
}

猜你喜欢

转载自blog.csdn.net/slience_646898/article/details/82776511