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 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小 ...
随机推荐
- word2vec_basic.py
ssh://sci@192.168.67.128:22/usr/bin/python3 -u /home/win_pymine_clean/feature_wifi/word2vec_basic.py ...
- IntelliJ IDEA 添加类注释模板
效果展示 /** * Created with IntelliJ IDEA * USER:jacun * CLASSNAME: HalloWorldController * DATE: 2019/1/ ...
- node-sass 安装失败的解决措施[转]
转自:http://blog.csdn.net/nzb329/article/details/51935236 在编译一个项目的时候,一直报错 后来发现是因为node-sass没有装成功, 最终的解决 ...
- oracle字符串函数总结
字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分 返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库 ...
- simple--factory--abstract
<?php /* 示例2: */ //简单工厂模式 /* * 定义运算类 */ abstract class Operation { protected $_NumberA = 0; prote ...
- HDU - 1114 Piggy-Bank 【完全背包】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1114 题意 给出一个储钱罐 不知道里面有多少钱 但是可以通过重量来判断 先给出空储钱罐的重量 再给出装 ...
- iOS 屏幕原点坐标 && 导航栏风格的自定义
其一 屏幕原点坐标 (x ,y) 受 self.navigationController. navigationBar 的 setTranslucent (BOOL) 属性控制 在 iOS7 以后 ...
- 【leetcode刷题笔记】Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- zabbix实现mysql数据库的监控(二)
上章我们把zabbix的服务端和客户端都部署完成了,本章接着进行两部分的设置: 1 添加对mysql数据库主机的监控 2 添加对mysql数据库的监控 一.对数据库服务器主机监控 1 创建主机 步 ...
- css3条纹边框效果
在线演示 本地下载