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;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值