https://vjudge.net/contest/67836#problem/F

"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

时间复杂度:$O(C_n^5 * A_5^5)$

题解:dfs

代码:

#include <bits/stdc++.h>
using namespace std; int tar, len;
char a[20];
int visi[20];
char res[10];
char tmp[10]; int debug(char *s) {
double sum = 0;
for(int i = 0; i < 5; i += 2) {
sum += pow(double(s[i] - 'A' + 1), double(i + 1));
}
for(int i = 1; i < 5;i += 2) {
sum -= pow(double(s[i] - 'A' + 1), double(i + 1));
}
return sum;
} void dfs(int step) {
if(step == 5) {
tmp[5] = '\0';
if(debug(tmp) == tar) {
if(strcmp(tmp, res) > 0)
strcpy(res, tmp);
}
return ;
}
for(int i = 0; i < len; i ++) {
if(!visi[i]) {
tmp[step] = a[i];
visi[i] = 1;
dfs(step + 1);
visi[i] = 0;
}
}
} int main() {
while(scanf("%d %s", &tar, a)) {
if(tar == 0 && strcmp(a, "END") == 0)
break;
memset(visi, 0, sizeof(visi));
strcpy(res, "@");
len = strlen(a); for(int i = 0; i < len; i ++) {
visi[i] = 1;
tmp[0] = a[i];
dfs(1);
visi[i] = 0;
} if(strcmp(res, "@") == 0)
printf("no solution\n");
else
printf("%s\n", res);
}
return 0;
}

  

ZOJ 1403 F-Safecracker的更多相关文章

  1. 暴力 ZOJ 1403 Safecracker

    题目传送门 /* 暴力:纯暴力,在家水水 */ #include <cstdio> #include <cstring> #include <algorithm> ...

  2. ZOJ 1403&&HDU 1015 Safecracker【暴力】

    Safecracker Time Limit: 2 Seconds      Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 ...

  3. ZOJ 1403 解密

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6412212.htmlSafecracker Time Limit: 2 Seconds       ...

  4. dp式子100个……

    1.        资源问题1-----机器分配问题F[I,j]:=max(f[i-1,k]+w[i,j-k]) 2.        资源问题2------01背包问题F[I,j]:=max(f[i- ...

  5. A过的题目

    1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...

  6. dp方程

    1.        资源问题1 -----机器分配问题 F[I,j]:=max(f[i-1,k]+w[i,j-k]) 2.        资源问题2 ------01背包问题   F[I,j]:=ma ...

  7. The 2014 ACM-ICPC Asia Mudanjiang Regional

    继续复盘之前的Regional......出题者说这一套题太简单,对当时没有AK很不满......真是醉了,弱校没法活了 [A]签到题 [B]树结构,树的中心 [C]-_-/// [D]概率DP [E ...

  8. Mysql_以案例为基准之查询

    查询数据操作

  9. ZOJ 3962 Seven Segment Display 16进制的八位数加n。求加的过程中所有的花费。显示[0,F]有相应花费。

    Seven Segment Display Time Limit: Seconds Memory Limit: KB A seven segment display, or seven segment ...

随机推荐

  1. while do while switch语句的简要分析

    1 //// while是C语言的一个关键字,其后是使用一个小括号中的条件表达式来做为执行循环的条件, 2 ////也就是说当条件表达式的结果为真时执行大括号里面的的程序内容, 3 ////而当条件表 ...

  2. C语言:一个数组中只有两个数字是出现一次

    //1.一个数组中只有两个数字是出现一次, //其他所有数字都出现了两次. //找出这两个数字,编程实现.a //^=单独两个数的^结果 //单独出现的两个数不同位的标记 //position: ^结 ...

  3. Go语言入门(二)Go语言中的变量、常量、数据类型、流程控制以及函数

    Go语言中的变量 通常用var关键声明变量,有常规方式和简化方式. 常规方式: var name1 type1 name1 = value1 //赋值 简化方式: var name2 = value1 ...

  4. Excelファイルを扱う方法

    概要 データをローカルに落としたいという要件がある場合.ユーザーはExcelを希望するケースが多いだろう.そんな時は以下の汎用モジュールを使用して簡単に作る事ができます.使用方法は.GUI_UPLOA ...

  5. python--基本类型之集合

    set(集合): 定义和创建: 定义:集合是一个无序的,不重复的数据集合,它主要作用1:去重,把一个列表变成集合,就自动去重了2:关系测试,测试两组数据之间的交集,差集,并集等关系 集合:把不同的数据 ...

  6. 使用MATLAB设计FIR滤波器

    1.      采用fir1函数设计,fir1函数可以设计低通.带通.高通.带阻等多种类型的具有严格线性相位特性的FIR滤波器.语法形式: b = fir1(n, wn) b = fir1(n, wn ...

  7. sas简单使用

    1 数据存取: 逻辑库: libname  自己起的名字 ‘文件所在的路径’,若无这步数据则存在默认的work中. 另一个方法在sas里自己建立一个逻辑库,但是关闭后就消失了. 新建数据:data  ...

  8. 20145202马超《网络对抗》Exp5MSF基础应用

    20145202马超<网络对抗>Exp5MSF基础应用 本实践目标,掌握metasploit的基本应用方式,掌握常用的三种攻击方式的思路.具体需要完成(1)一个主动攻击,如ms08_067 ...

  9. Hough变换-理解篇

    Hough变换-理解篇 霍夫变换(Hough Transform)是图像处理中的一种特征提取技术,它通过一种投票算法检测具有特定形状的物体.该过程在一个参数空间中通过计算累计结果的局部最大值得到一个符 ...

  10. python之Queue

    一.多进程的消息队列 “消息队列”是在消息的传输过程中保存消息的容器 消息队列最经典的用法就是消费者和生成者之间通过消息管道来传递消息,消费者和生成者是不通的进程.生产者往管道中写消息,消费者从管道中 ...