D - Safecracker
Crawling in process...
Crawling failed
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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."
"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."
Input
Output
Sample Input
1 ABCDEFGHIJKL 11700519 ZAYEXIWOVU 3072997 SOUGHT 1234567 THEQUICKFROG 0 END
Sample Output
LKEBA YOXUZ GHOST no solution
看题目那么复杂,其实题目的大致意思是:
就是让你找一个符合要求的密码,
1:密码全是由大写字母组成;
2:要求不能出现同一个字母;
3:要使得v - w^2 + x^3 - y^4 + z^5 = target ;
4:这5个字母的排列顺序是按照字典序排下来的最大的;(注意!)
这道题有两种方法:
1:暴力枚举法(因为我想5个for应该不会超时的,所以试着写一下,结果过了,但是里面有一些细节还是要注意下)
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(char a,char b){
return a>b;
}
int main(){
int i,v,w,x,y,z,m1,m2,m3,m4,m5,l,f;
__int64 k;
char ss[100];
while(scanf("%I64d",&k)!=EOF){
scanf("%s",ss);
m1=m2=m3=m4=m5=-1;
f=0;
if(k==0 && strcmp(ss,"END")==0) break;
l=strlen(ss);
sort(ss,ss+l,cmp);
for(i=0;i<l;i++)
ss[i]=ss[i]-'0'-16;
for(v=0;v<l;v++)
for(w=0;w<l;w++)
for(x=0;x<l;x++)
for(y=0;y<l;y++)
for(z=0;z<l;z++){
if((ss[v]-ss[w]*ss[w]+ss[x]*ss[x]*ss[x]-ss[y]*ss[y]*ss[y]*ss[y]+ss[z]*ss[z]*ss[z]*ss[z]*ss[z]==k)&&
//注意这里的逻辑关系是与,而不是或,比如只当其中一个满足但是其余的不满足时那也不行的;
(m1<v&&m2<w&&m3<x&&m4<y&&m5<z)&&(v!=w&&v!=x&&v!=y&&v!=z)&&(w!=x&&w!=y&&w!=z)&&
(x!=y&&x!=z)&&(y!=z)){
m1=v; m2=w; m3=x; m4=y; m5=z; f=1;
}
}
if(f) printf("%c%c%c%c%c\n",ss[m1]+16+'0',ss[m2]+16+'0',ss[m3]+16+'0',ss[m4]+16+'0',ss[m5]+16+'0');
else printf("no solution\n");
}
}
下面一个是网上找的,可能比我的要清晰些:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
bool cmp(char a,char b){
return a>b;
}
int main(){
int i,v,w,x,y,z,m1,m2,m3,m4,m5,l,f;
__int64 k;
char ss[100];
while(scanf("%I64d",&k)!=EOF){
scanf("%s",ss);
m1=m2=m3=m4=m5=-1;
f=1;
if(k==0 && strcmp(ss,"END")==0) break;
l=strlen(ss);
sort(ss,ss+l,cmp);
for(i=0;i<l;i++)
ss[i]=ss[i]-'0'-16;
for(v=0;v<l&&f;v++){
for(w=0;w<l;w++){
if(v==w) continue;
for(x=0;x<l&&f;x++){
if(v==x||w==x) continue;
for(y=0;y<l&&f;y++){
if(y==v||y==w||y==x) continue;
for(z=0;z<l&&f;z++){
if(z==v||z==w||z==x||z==y) continue;
else if((ss[v]-ss[w]*ss[w]+ss[x]*ss[x]*ss[x]-ss[y]*ss[y]*ss[y]*ss[y]+ss[z]*ss[z]*ss[z]*ss[z]*ss[z]==k)){
m1=v; m2=w; m3=x; m4=y; m5=z; f=0;
}
}
}
}
}
}
if(f==0) printf("%c%c%c%c%c\n",ss[m1]+16+'0',ss[m2]+16+'0',ss[m3]+16+'0',ss[m4]+16+'0',ss[m5]+16+'0');
else printf("no solution\n");
}
}
要注意的是当相等情况时要排除掉,而且要有标记的f来判断是否有解。
方法二:dfs搜索
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<string.h>
using namespace std;
int t,len;
int a[15];
bool use[15];
char str[15];
int tt[15];
bool flag;
bool cmp(char a,char b)
{
return a>b;
}
int f(int a,int b,int c,int d,int e)
{
return (a-b*b+c*c*c-d*d*d*d+e*e*e*e*e);
}
void dfs(int x)
{
int i;
if(x>=6)
{
if(flag==1)return ;
if(f(tt[1],tt[2],tt[3],tt[4],tt[5])==t)
{
//printf("%d\n",f(tt[1],tt[2],tt[3],tt[4],tt[5]));
for(i=1;i<=5;i++)printf("%c",tt[i]+'A'-1);
flag=1;
printf("\n");
}
return ;
}
for(i=0;i<len;i++)
{
if(use[i])continue;
tt[x]=a[i];
use[i]=1;
dfs(x+1);
use[i]=0;
}
}
int main()
{
int i;
while(1)
{
cin>>t>>str;
if(t==0&&str[0]=='E'&&str[1]=='N'&&str[2]=='D') break;
flag=0;
len=strlen(str);
memset(use,0,sizeof(use));
sort(str,str+len,cmp);
for(i=0;i<len;i++)a[i]=(str[i]+1-'A');
dfs(1);
if(!flag)printf("no solution\n");
}
return 0;
}