Hdu1015Safecracker(dfs)

Hdu1015Safecracker(dfs)

 

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

Total Submission(s): 7401    Accepted Submission(s): 3730

 

 

Problem Description

=== Op tech briefing, 2002/11/02 06:42 CST===

"The item is locked in a Klein safebehind 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 themdown before he died. A Klein safe has two distinguishing features: acombination lock that uses letters instead of numbers, and an engravedquotation on the door. A Klein quotation always contains between five andtwelve distinct uppercase letters, usually at the beginning of sentences, andmentions 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 - w^2 + x^3 - y^4 + z^5 = target

 

"For example, given target 1 andletter 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 thecombination turns out to be LKEBA. Klein thought it was safe to encode thecombination within the engraving, because it could take months of effort to tryall the possibilities even if you knew the secret. But of course computersdidn't exist then."

 

=== Op tech directive, computer division,2002/11/02 12:30 CST ===

 

"Develop a program to find Kleincombinations in preparation for field deployment. Use standard test methodologyas per departmental regulations. Input consists of one or more lines containinga positive integer target less than twelve million, a space, then at least fiveand at most twelve distinct uppercase letters. The last line will contain atarget of zero and the letters END; this signals the end of the input. For eachline output the Klein combination, break ties with lexicographic order, or 'nosolution' if there is no correct combination. Use the exact format shownbelow."

 

 

Sample Input

1 ABCDEFGHIJKL

11700519 ZAYEXIWOVU

3072997 SOUGHT

1234567 THEQUICKFROG

0 END

 

 

Sample Output

LKEBA

YOXUZ

GHOST

no solution

 

 

Source

Mid-Central USA 2002

題解:

Dfs的水題,題意是給出一個字符串,長度小於13,然後在這個字符串中找到按照v- w^2 + x^3 - y^4 + z^5 = target 這條公式算出來後值和輸入值ssum相等的,v,w……這些值是當前字符ch[i]-‘A’+1的出來的。然後就是很裸的深搜,答案要求多個輸出,要那個字符串大的。

注意(int)pow(double,int)的轉換啊,沒有pow(int,int)和pow(int,double)的。

源代碼:

#include <iostream>

#include <string.h>

#include <math.h>

#include <stdio.h>

using namespace std;

 

bool visit[20];

char ch[20];

char ans[20],rec[20];

__int64 ssum;int len;

 

void dfs(__int64 sum,int level)

   if(level== 6)

   {

     if(sum== ssum)

     {

        rec[level] = '\0';

        if(strcmp(rec,ans)> 0)

          strcpy(ans,rec);

     }

     return;

   }

   for(int i = 0;i < len;i++)

   {

     if(visit[i])

        continue;

     visit[i] = true;

     rec[level-1] = ch[i];

     inta = (int)pow((double)(ch[i]-'A'+1),level);

     if(!(level%2))

        a = 0-a;

     dfs(sum+a,level+1);

 

     visit[i] = false;

   }

}

int main()

{

 

   while(scanf("%I64d%s",&ssum,ch) != EOF)

   {

     if(!ssum&& !strcmp(ch,"END"))

        break;

     len = strlen(ch);

     memset(ans,'\0',sizeof(ans));

     memset(rec,'\0',sizeof(rec));

     memset(visit,false,sizeof(visit));

     dfs(0,1);

     if(strlen(ans))

        printf("%s\n",ans);

     else

        printf("no solution\n");

   }

   return0;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章