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. 学习/linux/list.h_双链表实现

    linux-3.5/include/linux/list.h 使用只含指针域的双向循环链表进行链表的操作. 下面是我选取部分list.h中代码: #ifndef _LINUX_LIST_H #defi ...

  2. DMVPN的实验模拟与分析

    此篇博客正在介绍的是下图中的DMVPN: 为什么会出现DMVPN这个技术呢? 在这篇博客中https://www.cnblogs.com/huwentao/p/9355240.html介绍过Dynam ...

  3. php+IIS 配置环境(windows环境)

    继php7+apache2.4 配置环境(window环境)后,由于B2C项目准备上线:特此小编在阿里云上搭建PHP7环境,为此特写上搭建过程希望正处于搭建php7+IIS(windows环境)中的朋 ...

  4. TCP/IP协议族、版本以及编址机制

    TCP/IP协议族简称TCP/IP.这么命名是因为该协议家族中的两个核心协议:TCP(传输控制协议)和IP(网际协议),为该家族中最早通过的标准.TCP/IP提供点对点的链接机制,将数据应该如何封装, ...

  5. DevExpress 学习链接

    http://blog.csdn.net/u013816709/article/category/3114039 http://blog.csdn.net/david_520042/article/c ...

  6. 北京Uber优步司机奖励政策(1月13日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. OpenCV 3.2 Tracking 物体跟踪

    跟踪就是在连续视频帧中定位物体,通常的跟踪算法包括以下几类: 1. Dense Optical Flow 稠密光流 2. Sparse Optical Flow 稀疏光流 最典型的如KLT算法(Kan ...

  8. 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)

    题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...

  9. P/Invoke 光标的操作

    获取与设置光标在屏幕上的位置 GetCursorPos 获取光标在屏幕上的位置,光标位置始终是在屏幕坐标纵指定的,并且不受包含光标的窗口映射模式的影响 函数原型: BOOL GetCursorPos( ...

  10. python操作符及其循环语句(非常全)

    //2018.10.14 1. Windows + R可以直接进行运行cmd 2. Random.randint(a,b):产生a-b的任意一个整数,在IDLE里面运行时需要注意在前面写好调用impo ...