ZOJ 1403

Safecracker


Time Limit: 2 Seconds      Memory Limit: 65536 KB


=== 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

no solution

no solution

no solution

题意

  密码序列由一系列大写字母组成,在解密序列不唯一的情况下,按字典序输出最后一个,解密公式:v - w^2 + x^3 - y^4 + z^5 = target

  由于题目中解的值域已经确定,解元素中的v,w,x,y,z都是题目中给定集合中的一个元素,数据范围较小枚举便可。

解题思路:

由于题目中解的值域已经确定,解元素中的v,w,x,y,z都是题目中给定集合中的一个元素,数据范围较小枚举便可。

*注意:由于题目求得是密码序列是按字典顺序的最后一个,所以再次我将之先降序排序,这样一来找到的第一个符合条件的肯定便是最后的!

代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char letters[15];
 4 int value[15],target;
 5 void process(int len)
 6 {
 7     int a,b,c,d,e;
 8     for(a=0;a<len;a++)
 9         for(b=0;b<len;b++)
10             if(a!=b)
11                 for(c=0;c<len;c++)
12                     if(a!=c&&b!=c)
13                         for(d=0;d<len;d++)
14                             if(a!=d&&b!=d&&c!=d)
15                                 for(e=0;e<len;e++)
16                                     if(a!=e&&b!=e&&c!=e&&d!=e)
17                                         if(value[a]-pow(value[b],2.0)+pow(value[c],3.0)-pow(value[d],4.0)+pow(value[e],5.0)==target)
18                                         {
19                                             printf("%c%c%c%c%c\n",value[a]+‘A‘-1,value[b]+‘A‘-1,value[c]+‘A‘-1,value[d]+‘A‘-1,value[e]+‘A‘-1);
20                                             return;
21                                         }
22                                          printf("no solution\n");
23                                     }
24 bool compare(int a,int b)
25 {
26     return a>b;
27 }
28 int main()
29 {
30     int i;
31     while(scanf("%d%s",&target,letters)!=EOF)
32     {
33         if(target==0&&strcmp(letters,"END")==0)
34             return 0;
35         i=0;
36         while(letters[i])
37         {
38             value[i]=letters[i]-‘A‘+1;
39             i++;
40         }
41         sort(value,value+i,compare);
42         process(i);
43     }
44     return 0;
45 }

出处

原文地址:https://www.cnblogs.com/fangxiaoqi/p/10357826.html

时间: 2024-11-01 18:32:06

ZOJ 1403的相关文章

暴力 ZOJ 1403 Safecracker

题目传送门 1 /* 2 暴力:纯暴力,在家水水 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <iostream> 8 #include <string> 9 #include <vector> 10 #include <cmath> 11 using namespace std; 12 13 const i

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost

ZOJ 2588

求一个无向图的桥(可能存在重边),输出割边的数目,并按顺序输出割边的序号(输入的顺序). 由于内存的限制 , 无法使用邻接矩阵 , 只能用邻接表了 . 第一次用了邻接表,超内存了: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <string.h> 5 using namespace std; 6 const int N=10002; 7 const i