HDUOJ----Safecracker(1015)

时间:2022-05-05
本文章向大家介绍HDUOJ----Safecracker(1015),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Safecracker

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

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

Source

Mid-Central USA 2002

 搜索....dfs

代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 char maze[30],key[7];
 6 int str[7];
 7 /*rn长与cm寛*/
 8 int n,cnt,cm,cmt;
 9 int cmp(const void *a ,const void *b)
10 {
11     return *(char *)b-*(char *)a;
12 }
13 bool dfs(int step)   /*深度搜索*/
14 {
15   if(step==5&&(str[0]-str[1]*str[1]+str[2]*str[2]*str[2]-str[3]*str[3]*str[3]*str[3]+str[4]*str[4]*str[4]*str[4]*str[4])==n)  //单词有5个
16   {  
17     return true;
18   }
19   /*如果没有找到,继续搜索*/
20   if(step<5)
21   {
22       int i;
23    for( i=0;i<cm;i++)     //作为宽度   
24       {
25           if(maze[i])        //不为0
26           {
27              key[cnt++]=maze[i];
28              str[cmt++]=maze[i]-64;
29              maze[i]='';
30              if(dfs(step+1))
31                  return true;
32              maze[i]=key[--cnt];
33              key[cnt]='';
34              str[--cmt]=0;
35           }
36       }
37   }
38   return false;
39 }
40 
41 int main()
42 {
43    while(scanf("%d %s",&n,maze)!=EOF)
44     {
45        if(n==0&&strcmp(maze,"END")==0) break;
46         cmt=cnt=0;
47         cm=strlen(maze);   /*作为搜索的数组*/
48         memset(key,'',sizeof(key));
49         memset(str,0,sizeof(str));
50         qsort(maze,cm,sizeof(char),cmp);
51         if(dfs(0))
52             printf("%sn",key);
53         else
54             printf("no solutionn");
55     } 
56   return 0;
57 }