HDU:1015 Safecracker

来源:互联网 发布:藿香清胃丸 知柏地黄丸 编辑:程序博客网 时间:2024/05/16 05:22

Safecracker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13765    Accepted Submission(s): 7196
题目链接

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 ABCDEFGHIJKL11700519 ZAYEXIWOVU3072997 SOUGHT1234567 THEQUICKFROG0 END
 

Sample Output
LKEBAYOXUZGHOSTno solution
解题思路:
题目意思是给出一个整数,给出一个大写字母组成的字符串(字符个数为5~12),然后从中任意取五个字母,他们再字母表中位置组成的数能够满足题目中所给公式,则这五个字母就是一个解,如果有多解,则输出字典序最大的那个解,如果无解则输出no solution。
这道题目是搜索题,采用深搜。
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <set>using namespace std;//target为目标值,n为题目中提供字符串的长度,vis为标记数组//str1用来存放题中提供的字符串,str2用来存放一种情况,mmax为当前最大字符串int target,n,vis[20];char str1[20],str2[10],mmax[10];void dfs(int sum,int pos)//sum为当前和值,pos为当前str2中要放置字符的位置{    if(pos == 5)         //如果pos=5,则str2中刚好有五个字符    {        if(sum == target)//如果当前和值等于目标值        {            char temp[10];            for(int j = 0; j < 5; j++)                temp[j]=str2[j];            temp[5]='\0'; //将str2中的字符全部放入temp            if(strcmp(temp,mmax)>0)  //更新最大解                strcpy(mmax,temp);        }        return;        /*此时无论如何都要返回,如果pos=5,则已经有五个字符,如果这五个字符        是解之一,则看看是否可以更新最大解,然后返回;若不是解,由于密码只由五个字符        组成,则要回溯,考虑其他的情况*/    }    for(int i = 0; i < n; i++)    {        if(vis[i]==0)  //如果第i个字符没有访问过        {            vis[i]=1;  //标记为1            str2[pos]=str1[i];  //将这个字符放入str2            int mul=1,len,k,temp;            temp = str1[i]-'A'+1;  //temp是当前str[i]在字母表中的位置            k = len = pos+1;    //k和len,代表当前求的相是表达式中的第几相            while(k--)            {                mul = mul*temp;             }            if(len%2==0)                mul = mul*(-1);            dfs(sum+mul,pos+1);            vis[i]=0;  //恢复,将第i个字符恢复为未标记状态        }    }}int main(){    while(~scanf("%d%s",&target,str1))    {        if(target==0&&strcmp(str1,"END")==0)            break;        memset(vis,0,sizeof(vis));//0代表没有访问过,        n = strlen(str1);        strcpy(mmax,""); //将mmax初始化为空值        dfs(0,0);//因为初始和值为0,且str2中无字符,因此sum=0,pos=0        if(strcmp(mmax,"")==0)             printf("no solution\n");        else            printf("%s\n",mmax);    }    return 0;}


0 0
原创粉丝点击