HDOJ1015(简单深搜)
Safecracker
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12702 Accepted Submission(s): 6581
"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."
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
int target,len;
char s[MAXN];
int vis[MAXN];
int buf[MAXN];
bool comp(char ch1,char ch2)
{
return ch1 > ch2;
}
bool dfs(int dep)
{
if(dep==)
{
long long sum=;
for(int i=;i<dep;i++)
{
long long e=;
for(int j=;j<=i;j++)
{
e*=buf[i];
}
if(i%==) sum+=e;
else sum-=e;
}
if(sum==(long long)target)
{
return true;
}
else
{
return false;
}
}
for(int i=;i<len;i++)
{
if(!vis[i])
{
vis[i]=;
buf[dep]=s[i]-'A'+;
if(dfs(dep+))
{
return true;
}
vis[i]=;
}
}
return false;
}
int main()
{
while(scanf("%d%s",&target,s)!=EOF)
{
if(target==&&strcmp(s,"END")==) break;
memset(vis,,sizeof(vis));
len=strlen(s);
sort(s,s+len,comp);
if(dfs())
{
for(int i=;i<;i++)
{
printf("%c",buf[i]+'A'-);
}
printf("%c\n",buf[]+'A'-);
}
else
{
printf("no solution\n");
}
}
return ;
}
HDOJ1015(简单深搜)的更多相关文章
- POJ 2386 Lake Counting (简单深搜)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- 简单深搜:POJ1546——Sum it up
结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 ...
- poj 1562 简单深搜
//搜八个方向即可 #include<stdio.h> #include<string.h> #define N 200 char ma[N][N]; int n,m,vis[ ...
- POJ-1321棋盘问题(简单深搜)
简单搜索step1 POJ-1321 这是第一次博客,题目也很简单,主要是注意格式书写以及常见的快速输入输出和文件输入输出的格式. 递归的时候注意起始是从(-1,-1)开始,然后每次从下一行开始递归. ...
- NYoj The partial sum problem(简单深搜+优化)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...
- nyoj587 hdu1045 简单深搜
#include<iostream> #include<cstdio> #include<queue> #include<vector> #includ ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- 【笔记】「pj复习」深搜——简单剪枝
深搜--简单剪枝 说在最前面: 因为马上要 NOIP2020 了,所以菜鸡开始了复习qwq. pj 组 T1 ,T2 肯定要拿到满分的,然后 T3 , T4 拿部分分, T3 拿部分分最常见的做法就是 ...
- 【BZOJ】1016: [JSOI2008]最小生成树计数 深搜+并查集
最小生成树计数 Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小 ...
随机推荐
- spring boot数据库操作汇总
1 关于orm orm即object relational mapping,对象关系映射,即将数据库中的表映射成对象. 常用的orm有以下: mybatis spring jdbc template ...
- rm -rf 删除文件找回
一个不小心rm掉文件了吧? 后悔莫及了吧! 把这段代码加入你的home目录的.bashrc或者.zshrc就可以了! 工作原理: 在你的home目录会创建一个.trash文件夹 里面会按照删除时间 年 ...
- Android系统移植与调试之------->如何修改Android设备添加3G上网功能
1.首先先来看一下修改前后的效果对比图 step1.插上3G设备前 step2.插上3G设备后,获取信号中.... step3.插上3G设备后,获取到信号 step4.使用3G信号浏览网页 2.下面讲 ...
- 正则表达式 匹配符合A表达式切不符合B表达式的字符串
有一道这样的面试题 写一个Java方法,利用正则表达式判断输入str中包含字符串”ios“或”apple“(大小写不敏感),但不包括”mediaplayer“.如果满足条件,返回所包含的字符串”ios ...
- 关于highcharts-ng
1.内容都正确但是不显示,使用parseInt()方法转换
- chorme 插件
json-handle: json可视化工具 开发中需要用到json,在浏览器显示的json非常乱,难以理解.有没有让人一目了然的工具,让json看起来非常直观呢,json-handle随之而出,包含 ...
- 【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array
描述: Given an array of integers nums sorted in ascending order, find the starting and ending position ...
- python内置方法补充all
all(iterable) 版本:该函数在python2.5版本首次出现,适用于2.5以上版本,包括python3,兼容python3版本. 说明:如果iterable的所有元素不为0.''.Fals ...
- hd acm1466
http://www.cnblogs.com/alihenaixiao/p/4107907.html#undefined.这个博客有详解,我这个只是写一些·自己的总结. 问题:平面上有n条直线,且无三 ...
- thinkphp 的 Action 控制器中的系统常量总结
THINK_PATH // ThinkPHP系统目录 APP_PATH // 当前项目目录 APP_NAME // 当前项目名称 CONTROLLER_NAME // 当前控制器名称 MODULE_N ...