Problem Description
=== Op tech briefing, 2002/11/02 06:42 CST === 
"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
 
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <cmath>
#include <math.h>
#include <algorithm>
using namespace std;

#define Maxn 1000

int aim;
int dir[Maxn];
bool visit[Maxn+1];
int Aim[Maxn];
string A;
bool flag;

bool cmp(int a,int b)
{
    return a > b;
}

void dfs(int T)
{
    if (flag)
    {
        return ;
    }
    if (T == 5)
    {
        if ( Aim[0] - pow(Aim[1],2) + pow(Aim[2],3) - pow(Aim[3],4) + pow(Aim[4],5) == aim)
        {
            flag = true;
            printf("%c%c%c%c%c\n",Aim[0]+'A'-1,Aim[1]+'A'-1,Aim[2]+'A'-1,Aim[3]+'A'-1,Aim[4]+'A'-1);
        }
        return ;
    }
    for(int i = 0; i < A.length(); i++)
    {
        if (!visit[i])
        {
            Aim[T] = dir[i];
            visit[i] = true;
            dfs(T+1);
            visit[i] = false;
        }
    }
}

int main()
{
    while(cin >> aim >> A)
    {
        if (aim == 0)
        {
            break;
        }
        flag = false;
        for (int i = 0; i < A.length(); i++)
        {
            dir[i] = A[i] - 'A' + 1;
        }
        sort(dir,dir+A.length(),cmp);
        for (int i = 0; i < A.length(); i++)
        {
            if (flag)
            {
                break;
            }
            memset(visit,false,sizeof(visit));
            Aim[0] = dir[i];
            visit[i] = true;
            dfs(1);
        }
        if (!flag)
        {
            printf("no solution\n");
        }
    }
}

  

 
Sample Output
LKEBA
YOXUZ
GHOST
no solution

hdu 1015 dfs的更多相关文章

  1. HDOJ(HDU).1015 Safecracker (DFS)

    HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...

  2. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  3. hdu 1015 Safecracker 水题一枚

    题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein s ...

  4. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  5. hdu 1015(DFS)

    Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. HDU 1015 Safecracker【数值型DFS】

    Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. HDU 1015 Safecracker (DFS)

    题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...

  8. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  9. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

随机推荐

  1. cocos2dx arpg单机手游

    这只是一个DEMO. ARPG 单机手游, 个人DEMO. 支持剧情编辑, 支持气泡对话, 支持人物图像对话, 支持随时角色切换, 支持NPC跟随, 共同作战, 支持LUA扩展, 支持BUFF技能, ...

  2. php 连接mysql数据库并显示数据 实例 转载

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  3. JavaScript学习心得(八)

    Cookie是Netscape发明的技术,是动态网站必不可少的部分,用于浏览器请求Web页面的超文本传输协议是一种无状态的协议. 两种方法维护状态:使用会话(session)(使用服务器技术实现,数据 ...

  4. UML2.0统一建模语言

      Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是支持模型化和软件系统开发的图形化语言,为软件开发的所有阶段提供模型化和可视化支持,包括由需求分析到规 ...

  5. 用Python实现的一个简单的爬取省市乡镇的行政区划信息的脚本

    # coding=utf-8 # Creeper import os import bs4 import time import MySQLdb import urllib2 import datet ...

  6. iOS+Swift: 使用MessageUI.framework发送短信

    在iOS中, 可以使用MessageUI.framework框架发送短信, 步骤如下: 代码下载http://git.oschina.net/yao_yu/swift_cnblogs_samples/ ...

  7. 多线程-GCD学习笔记

    ********************************* 基本概念 *********************************** 1. Grand Central Dispatch ...

  8. 关于scroll无法绑定的问题

    关于jQuery的scroll([[data],fn])事件, 概述是:当用户滚动指定的元素(元素包括:所有可滚动的元素和 window 对象)时,会触发该事件. 如绑定窗口对象: $(window) ...

  9. 第一个deeplearning4jproject跑起

    deeplearning4j是基于java的深度学习库,当然,它有许多特点,但暂时还没学那么深入,所以就不做介绍了 需要学习dl4j,无从下手,就想着先看看官网的examples,于是,下载了exam ...

  10. hibernate中的缓存机制

    一.为什么要用Hibernate缓存? Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数据源中的数 ...