HDOJ/HDU 1015 Safecracker(深搜)

简介: 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.

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

Sample Output
LKEBA
YOXUZ
GHOST
no solution

上个我用枚举做了,感觉不怎么好,毕竟是练算法的,就试试了深搜。
题意:
给你一个数,再给一个全部是大写字母构成的字符串。
从里面选5个字母v,m,x,y,z(不重复),计算v-m^2+x^3-y^4+z^4是否等于目标值
选出来的方案可能有很多种,那么你应该选择字典序最大的那种。

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static char handle[] = new char[6];
    static char at[]={' ','A','B','C','D','E','F','G','H','I','J'
            ,'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    static char chs[];
    static int target;
    static String str;
    static boolean map[];//标识是否已经用了
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //for(int i='A';i<='Z';i++){
            //char c = (char)i;
            //System.out.print("'"+c+"',");
        //}
        while(sc.hasNext()){
            target = sc.nextInt();
            str = sc.next();
            if(target==0&&str.equals("END")){
                return;
            }
            map = new boolean[str.length()];
            chs = str.toCharArray();
            Arrays.sort(chs);
            for(int i=0,j=chs.length-1;i<chs.length/2;i++,j--){
                char c=chs[i];
                chs[i]=chs[j];
                chs[j]=c;
            }

            if(dfs(0)){
                for(int i=0;i<5;i++){
                    System.out.print(handle[i]);
                }
                System.out.println();
            }else{
                System.out.println("no solution");
            }


        }
    }
    private static boolean dfs(int m) {
        if(m==5){
            if( res(handle[0],handle[1],handle[2],handle[3],handle[4]) ){
                return true;
            }
            return false;
        }else{
            for(int i=0;i<str.length();i++){
                if(!map[i]){
                    map[i]=true;
                    handle[m]=chs[i];
                    if(dfs(m+1)){
                        return true;
                    }
                    map[i]=false;
                }
            }
        }

        return false;
    }
    private static boolean res(char a, char b, char c, char d, char e) {
        int ap[] = new int[5];
        for(int j=0;j<ap.length;j++){
            for(int i=1;i<at.length;i++){
                if(j==0){
                    if(a==at[i]){
                        ap[0]=i;
                        break;
                    }
                }else
                if(j==1){
                    if(b==at[i]){
                        ap[1]=i;
                        break;
                    }
                }else
                if(j==2){
                    if(c==at[i]){
                        ap[2]=i;
                        break;
                    }
                }else
                if(j==3){
                    if(d==at[i]){
                        ap[3]=i;
                        break;
                    }
                }else
                if(j==4){
                    if(e==at[i]){
                        ap[4]=i;
                        break;
                    }
                }
            }
        }

        int sum=0;
        for(int i=0;i<ap.length;i++){
            if(i%2==0){
                sum+=Math.pow(ap[i], i+1);
            }else{
                sum-=Math.pow(ap[i], i+1);
            }
        }
        if(sum==target){
            return true;
        }else{
            return false;
        }
    }
}
目录
相关文章
|
9天前
|
数据采集 人工智能 安全
|
4天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:七十、小树成林,聚沙成塔:随机森林与大模型的协同进化
随机森林是一种基于决策树的集成学习算法,通过构建多棵决策树并结合它们的预测结果来提高准确性和稳定性。其核心思想包括两个随机性:Bootstrap采样(每棵树使用不同的训练子集)和特征随机选择(每棵树分裂时只考虑部分特征)。这种方法能有效处理大规模高维数据,避免过拟合,并评估特征重要性。随机森林的超参数如树的数量、最大深度等可通过网格搜索优化。该算法兼具强大预测能力和工程化优势,是机器学习中的常用基础模型。
298 164
|
3天前
|
机器学习/深度学习 自然语言处理 机器人
阿里云百炼大模型赋能|打造企业级电话智能体与智能呼叫中心完整方案
畅信达基于阿里云百炼大模型推出MVB2000V5智能呼叫中心方案,融合LLM与MRCP+WebSocket技术,实现语音识别率超95%、低延迟交互。通过电话智能体与座席助手协同,自动化处理80%咨询,降本增效显著,适配金融、电商、医疗等多行业场景。
311 155
|
11天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
857 6
|
5天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:六十九、Bootstrap采样在大模型评估中的应用:从置信区间到模型稳定性
Bootstrap采样是一种通过有放回重抽样来评估模型性能的统计方法。它通过从原始数据集中随机抽取样本形成多个Bootstrap数据集,计算统计量(如均值、标准差)的分布,适用于小样本和非参数场景。该方法能估计标准误、构建置信区间,并量化模型不确定性,但对计算资源要求较高。Bootstrap特别适合评估大模型的泛化能力和稳定性,在集成学习、假设检验等领域也有广泛应用。与传统方法相比,Bootstrap不依赖分布假设,在非正态数据中表现更稳健。
250 113