首页 > ACM题库 > HDU-杭电 > HDU 1015 Safecracker-字符串-[解题报告] C++
2013
11-28

HDU 1015 Safecracker-字符串-[解题报告] C++

Safecracker

问题描述 :

=== 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.”

样例输入:

1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END

样例输出:

LKEBA
YOXUZ
GHOST
no solution

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1015

题意:找符合题目给定公式字典序最后的字符串。

mark:暴力过……

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int n,c[15];
char a[15], b[30] = "1ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int cmp(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;
}

void find()
{
    int i,j,k,p,q,l;
    l = strlen(a);
    for(i = 0; i < l; i++)
        c[i] = a[i]-'A'+1;
    qsort(c, l, 4, cmp);
    for(i = 0; i < l; i++)
        for(j = 0; j < l; j++)
        {
            if(j == i) continue;
            for(k = 0; k < l; k++)
            {
                if(k == i || k == j) continue;
                for(p = 0; p < l; p++)
                {
                    if(p == i || p == j || p == k) continue;
                    for(q = 0; q < l; q++)
                    {
                        if(q == i || q == j || q == k || q == p) continue;
                        if(c[i] - c[j]*c[j] + c[k]*c[k]*c[k] - c[p]*c[p]*c[p]*c[p] + c[q]*c[q]*c[q]*c[q]*c[q] == n)
                        {
                            printf("%c%c%c%c%c\n", b[c[i]], b[c[j]], b[c[k]], b[c[p]], b[c[q]]);
                            return ;
                        }
                    }
                }
            }
        }
    puts("no solution");
}

int main()
{
    while(~scanf("%d%s", &n, a))
    {
        if(!n && !strcmp(a, "END")) break;
        find();
    }
    return 0;
}

  1. 站长,你好!
    你创办的的网站非常好,为我们学习算法练习编程提供了一个很好的平台,我想给你提个小建议,就是要能把每道题目的难度标出来就好了,这样我们学习起来会有一个循序渐进的过程!

  2. 第一题是不是可以这样想,生了n孩子的家庭等价于n个家庭各生了一个1个孩子,这样最后男女的比例还是1:1

  3. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示

  4. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?