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
搜索....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 }
- SQL Server 2012将与Hadoop无缝集成
- 系统性能优化一例
- Android中include标签的使用
- css3弹性盒子模型——回顾。
- confluence与jira账号对接、查看到期时间及问题总结
- 物联网、人工智能时代来临五大隐忧不可不提防
- Contact Manager Web API 示例[1]CRUD 操作
- 3个域名交易:fde.com11万被秒
- 新手指南OpenStack:Nova的基础知识
- Python-装饰器详解
- Contact Manager Web API 示例[2] Web API Routing
- Android一些关于分辨率和布局的设置
- log4net 中错误 System.Web.HttpException (0x80004005): 文件不存在
- Contact Manager Web API 示例[2] Web API Routing
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法