杭电1015————DFS+字符串

Safecracker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8377    Accepted Submission(s): 4226


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,都知道想用DFS必须有搜索条件和终止条件。最开始的想法是这样的
if( equation(sub) && used(sub) == 0 )
{
    if(strcmp(sub,resultsub) > 0 )
        strcpy(resultsub,sub);
} 
即寻找到子串sub对其进行两种判断
1.是否满足题目要求的等式
2.当前子串的这种排列是否已经被判断过
那么按照以上思路,有三个问题
1:sub怎么给分离出来
2:used(sub)怎么表示
3:DFS这种递归必须有递归出口,递归出口在哪?
我想了很久也没有想出来,因为sub是一个5位的字符串,不好分离,used(sub)也不好判断。
再一看题目,要求最后的结果是字典序最大的。
看到这里,应该想到了排序。
先把字符串按照字典序从大到小排序,这样首先找到的就是字典序最大的(这样就很容易定递归出口)第三个问题解决了。
至于前两个问题...明显给自己找麻烦了..usd(sub)不好判断,但是used[i](i表示第几个字符)是比较容易去判断的。
即把排序后的字符串的字母一个一个的分离,而不是直接把5个字符当做一个字符串分离出来
那么DFS的参数传递也顺便解决了...就传递当前字符的个数,当字符是5并且满足上边那段小代码时,就是递归出口
代码如下
(亲测91MS)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

char s[15]; /*所有字母数*/ 
char sub[6];/*最终结果*/ 
int  vis[15];/*第i个字母已经被用过*/ 
int  num[15];
int target,len,flag ;

int cmp(int a, int b){
	return  a > b;
}
bool equation()
{
	int temp[5] = {0};
	for(int i = 0 ; i < 5 ; i++)
	{
		temp[i] = sub[i] - 'A' + 1;
		temp[i] = pow(temp[i],i+1);
	}
	if(temp[0] - temp[1] + temp[2] - temp[3] + temp[4] == target)
		return true;
	else
		return false;
}
void DFS(int count)/*count表示当前是第几个"字母"*/ 
{
	/*最先找到的一定是最大的*/ 
	if(count == 5 && flag == 0)
	{
		if(equation())
		{
			flag = 1;
			printf("%s\n",sub);
			return ;
		}
	}
	for(int i = 0 ; i < len ; i ++)
	{
		if(vis[i] == 0 && count < 5)
		{
			sub[count] = num[i] - 1 + 'A';
<span style="white-space:pre">			</span>/*此处如果我写sub[count++] = num[i] - 1 + 'A',下边写DFS(count)是错的..这个地方有些不解*/
			vis[i] = 1;
			DFS(count+1);
			vis[i] = 0;
		}
	}
}
int main()
{	
	while(scanf("%d %s",&target,s) != EOF)
	{
		if(target == 0 && strcmp(s,"END") == 0) break;
		memset(vis,0,sizeof(vis));
		flag = 0;/*标志是否有找到结果,默认为无*/ 
		len = strlen(s);
		for(int i = 0 ; i < len ; i ++)
			num[i] = s[i] - 'A' + 1;
		sort(num,num+len,cmp);
		/*先按照从大到小排序,这样找到的第一个答案一定是要求的答案*/ 
		DFS(0);
		if(flag == 0)
			printf("no solution\n");
	}
	return 0;
}



posted @ 2014-08-03 16:25  SixDayCoder  阅读(210)  评论(0编辑  收藏  举报