Safecracker【ZOJ - - 1403】

来源:互联网 发布:人工智能 智慧城市 编辑:程序博客网 时间:2024/06/03 20:19
=== 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."


题意:密码序列由一系列大写字母组成,在解密码序列不唯一的情况下,按字典序输出最后一个。解密公式:v - w^2 + x^3 - y^4 + z^5 = target。其中,target是数字,由题目给出。v,w,x,y,z属于同一个集合且各不相同,该集合由题目给出,由26个大写字母中的任意5~12个组成。每个字母对应的数值,以此类推(A=1,B=2,...,Z=26).


思路:这是一道典型的枚举题。题目中,解的值域已经确定,解元素中的v,w,x,y,z都是题目给定集合中的一个元素。尽管枚举法的算法复杂度是指数级的,但鉴于给定集合最多只有12个元素,完全可以直接用枚举。另外,题目中的约束条件,v,w,x,y,z各不相同,明显不属于解集的元素可以很容易地通过判断来排除。需要注意的是,题目求的密码序列是按字典序输出最后一个,所以需要事先将集合元素排好序。


吐槽:刚开始我就是用最简单的暴力枚举做的,什么优化方法都没用,如果答案有多种输出的密码序列必须是字典序中的最后一个,连这个要求我也是用的比较,把每个符合条件的集合元素都求了出来然后按字典序进行一一比较。然后又借鉴了其他大神的做法,是先把字母转化成其值存到一个数组里,然后将该数组按降序排序,这样得到的第一组情况的解即为字典序下的最后一组解,显然这种做法比我的纯暴力能优化很多。


Sample Input

1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END


Sample Output

LKEBA
YOXUZ
GHOST

no solution


方法一:

#include <cstdio>#include <algorithm>#include <cstring>#define LL long longusing namespace std;int main(){    LL n;    char st[20],anwser[10],b[10];    while(~scanf("%lld %s",&n,st))    {        if(n==0&&(strcmp(st,"END")==0))            break;        memset(anwser,0,sizeof(anwser));     //记得在每组的时候这两个数组都要清零        memset(b,0,sizeof(b));        int len=strlen(st);        LL ii,jj,kk,pp,qq;        bool flag=true;        for(int i=0;i<len;i++)        {            ii=st[i]-'A'+1;            for(int j=0;j<len;j++)            {                if(j==i)                    continue;                jj=(st[j]-'A'+1)*(st[j]-'A'+1);                for(int k=0;k<len;k++)                {                    if(k==i||k==j)                        continue;                    kk=(st[k]-'A'+1)*(st[k]-'A'+1)*(st[k]-'A'+1);                    for(int p=0;p<len;p++)                    {                        if(p==i||p==j||p==k)                            continue;                        pp=(st[p]-'A'+1)*(st[p]-'A'+1)*(st[p]-'A'+1)*(st[p]-'A'+1);                        for(int q=0;q<len;q++)                        {                            if(q==i||q==j||q==k||q==p)                                continue;                            qq=(st[q]-'A'+1)*(st[q]-'A'+1)*(st[q]-'A'+1)*(st[q]-'A'+1)*(st[q]-'A'+1);                            LL sum;                            sum=ii-jj+kk-pp+qq;                            if(sum==n)                            {                                b[0]=st[i];                                b[1]=st[j];                                b[2]=st[k];                                b[3]=st[p];                                b[4]=st[q];                                if(flag)                                {                                    strcpy(anwser,b);                                    flag=false;                                }                                else                                {                                    if(strcmp(anwser,b)<0)                                    {                                        strcpy(anwser,b);                                    }                                }                            }                        }                    }                }            }        }        if(flag)        printf("no solution\n");        else            printf("%s\n",anwser);    }    return 0;}


方法二:

#include <cstdio>#include <algorithm>#include <cstring>#define LL long longusing namespace std;bool cmp(int a,int b){    return a>b;}int main(){    //freopen("in.txt","r",stdin);    int n,val[10];    char st[20];    while(~scanf("%d %s",&n,st))    {        if(n==0&&(strcmp(st,"END")==0))            break;        memset(val,0,sizeof(val));        int len=strlen(st);        for(int i=0;i<len;i++)            val[i]=st[i]-'A'+1;        sort(val,val+len,cmp);        int ii,jj,kk,pp,qq;        bool flag=true;        for(int i=0;i<len;i++)        {            ii=val[i];            for(int j=0;j<len;j++)            {                if(j==i)                    continue;                jj=val[j]*val[j];                for(int k=0;k<len;k++)                {                    if(k==i||k==j)                        continue;                    kk=val[k]*val[k]*val[k];                    for(int p=0;p<len;p++)                    {                        if(p==i||p==j||p==k)                            continue;                        pp=val[p]*val[p]*val[p]*val[p];                        for(int q=0;q<len;q++)                        {                            if(q==i||q==j||q==k||q==p)                                continue;                            qq=val[q]*val[q]*val[q]*val[q]*val[q];                            int sum;                            sum=ii-jj+kk-pp+qq;                            if(sum==n)                            {                                printf("%c%c%c%c%c\n",val[i]+'A'-1,val[j]+'A'-1,val[k]+'A'-1,val[p]+'A'-1,val[q]+'A'-1);                                flag=false;                                break;                            }                        }                        if(!flag)                            break;                    }                    if(!flag)                        break;                }                if(!flag)                    break;            }            if(!flag)                break;        }        if(flag)            printf("no solution\n");    }    return 0;}


0 0