天天看點

hdu 1015 Safecracker Safecracker

題目位址:http://acm.hdu.edu.cn/showproblem.php?pid=1015

Safecracker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4847    Accepted Submission(s): 2427

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
        

題意:給出一組資料 資料由數字和大寫字母串組成,問從大寫字母串中選出字典序最大的長度為5子串按照A=1,B=2.....Z=26 的規律帶入公式v - w^2 + x^3 - y^4 + z^5 ,使其值等于所給的數字。

題解:資料規模很小,串長度最大為12,數字範圍也很小int足夠。對大寫字母串遞歸DFS枚舉出所有串長為5的子串(注意串是有序的),然後帶入公式檢驗,找出字典序最大的滿足條件的串。

/*
hdu:Safecracker
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<string>
//know the define 's Essence is the absolutely replace,take this care
#define FFFF1(a) (int)(a)
#define FFFF2(a) FFFF1(a)*FFFF1(a)
#define FFFF3(a) FFFF2(a)*FFFF1(a)
#define FFFF4(a) FFFF2(a)*FFFF2(a)
#define FFFF5(a) FFFF2(a)*FFFF3(a)

using namespace std;
int Target=0;
char GStr[12+5]={'\0'};
int GStrLen=0;
bool GStrVis[12+5]={false};
char SubSet[5+5]={'\0'};
int SubSetLen=0;
int Count=0;
char MaxSubSet[5+5]={'\0'};
int MaxSubSetLen=0;
/*intialize the var*/
int InitVar()
{
	Count=0;
	memset(GStrVis,false,sizeof(GStrVis));
	GStrLen=strlen(GStr);
	memset(MaxSubSet,'\0',sizeof(MaxSubSet));
	return(0);
}
/*judge the string is available*/
bool IsAvailable()
{//there has the problem of the para that must be the int number
	if(FFFF1(SubSet[0]-'A'+1)-FFFF2(SubSet[1]-'A'+1)+FFFF3(SubSet[2]-'A'+1)-FFFF4(SubSet[3]-'A'+1)+FFFF5(SubSet[4]-'A'+1)==Target)
	{
		return(true);
	}
	else
	{
		return(false);
	}
}
/*make the all aubset and test the available*/
int MakeSubSet(int cur)
{
	if(cur==5)
	{
		SubSet[5]='\0';
		if(IsAvailable())
		{
			Count++;
			if(MaxSubSet[0]=='\0'||strcmp(MaxSubSet,SubSet)<0)
			{
				strcpy(MaxSubSet,SubSet);
			}
		}
	}
	else
	{
		int i=0;
		for(i=0;i<=GStrLen-1;i++)
		{
			if(GStrVis[i]==false)
			{
				GStrVis[i]=true;
				SubSet[cur]=GStr[i];
				MakeSubSet(cur+1);
				GStrVis[i]=false;
			}
		}
	}
	return(0);
}
/*for test*/
int test()
{
	return(0);
}
/*main process*/
int MainProc()
{
	while(scanf("%d%s",&Target,GStr)!=EOF&&Target>0)
	{
		InitVar();
		MakeSubSet(0);
		if(Count>0)
		{
			printf("%s\n",MaxSubSet);
		}
		else
		{
			printf("no solution\n");
		}
	}
	return(0);
}
int main()
{
	MainProc();
	return(0);
}
           

繼續閱讀