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. php 算法(二分法)只适用于有序表,且限于顺序存储结构

    function demo($array,$low,$high,$k){ if($low<=$high){//判断该数组是否存在 $mid =  intval(($low+$high)/2 ); ...

  2. Hadoop 动态扩容 增加节点

    基础准备 在基础准备部分,主要是设置hadoop运行的系统环境 修改系统hostname(通过hostname和/etc/sysconfig/network进行修改) 修改hosts文件,将集群所有节 ...

  3. 免考final linux提权与渗透入门——Exploit-Exercise Nebula学习与实践

    免考final linux提权与渗透入门--Exploit-Exercise Nebula学习与实践 0x0 前言 Exploit-Exercise是一系列学习linux下渗透的虚拟环境,官网是htt ...

  4. docker 在window 10 专业版的安装 && .net core 在docker的部署

    1.如果无法安装Hyper-V,八成是自己的杀毒软件给关了,我的是 电脑管家-启动项里面 给关掉了. 2.如果部署.net core 后 运行 报 An assembly specified in t ...

  5. ORB-SLAM(十)LoopClosing

    构造函数 LoopClosing(Map* pMap, KeyFrameDatabase* pDB, ORBVocabulary* pVoc,const bool bFixScale); 主要分两部分 ...

  6. Grafana学习

    一.安装 Grafana最新版本4.3.1安装(后端使用mysql) 二.使用

  7. 金山WPS面试题

    1.windows的handle 1)是一个宏定义#define void* HANDLE 2) HANDLE提供了一种统一的方式去获得系统资源,并对其进行操作. 3) HANDLE使得程序设计的细节 ...

  8. 03-JVM内存模型:堆与方法区

    一.堆(Heap) 1.1.什么是堆 堆是用于存放对象的内存区域.因此,它是垃圾收集器(GC)管理的主要目标.其具有以下特点: 堆在逻辑上划分为“新生代”和“老年代”.由于JAVA中的对象大部分是朝生 ...

  9. python爬虫实例大全

    WechatSogou [1]- 微信公众号爬虫.基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典. DouBanSpider [2]- ...

  10. OSG的组成结构

    OSG的组成结构 核心结构 OSG的功能类采用“命名空间+类名称”的形式来命名.命名空间的命名方式为:第一个单词小写,后继单词的首字母大写,例如osg.osgUtil.osgViewer等:类的名称则 ...