# HDOJ-1015(Safecracker)(多重for循环)

HDOJ-1015(Safecracker)(搜索dfs或多重for循环)

# Safecracker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10968    Accepted Submission(s): 5632

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

,其中target是输入的整数。若有多个答案则选择5个字母序号和最大的作为最终答案。

for循环选中一个字母，并标记（每个字母在一个组合中只能出现一次），这样通过for循环就可以遍历所有情况。之所以用sort排序，是因为查询是从小往大进行，查询的数依次增大，最终得到的结果必定是字典序号和最大的。

My  solution：
/*2015.11.16*/
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int c[20],map[91],ans[15];
char c1[20];
int njie(int n,int m)
{
int i,sum=1;
for(i=1;i<=m;i++)
sum*=n;
return sum;
}
int main()
{
int  n,i,j,t,i1,i2,i3,i4,sum;
while(scanf("%d",&n)==1&&n)
{
sum=0;
memset(map,0,sizeof(map));
memset(ans,0,sizeof(ans));
scanf("%s",c1);
t=strlen(c1);
for(i=0;i<t;i++)
c[i]=c1[i]-'A'+1;
sort(c,c+t);
for(i=0;i<t;i++)
{
map[i]=1;//第一个字母被使用,则把它标记一下
for(i1=0;i1<t;i1++)
{
if(!map[i1])
{
map[i1]=1;//第一个字母被使用,则把它标记一下
for(i2=0;i2<t;i2++)
{
if(!map[i2])
{
map[i2]=1;
for(i3=0;i3<t;i3++)
{
if(!map[i3])
{
map[i3]=1;
for(i4=0;i4<t;i4++)
{
if(!map[i4])
{
sum=c[i]-njie(c[i1],2)+njie(c[i2],3)-njie(c[i3],4)+njie(c[i4],5);
if(sum==n)//查询成功，则更新排列结果。
{
ans[0]=c[i];
ans[1]=c[i1];
ans[2]=c[i2];
ans[3]=c[i3];
ans[4]=c[i4];
}

}
}
map[i3]=0;
}
}
map[i2]=0;
}
}
map[i1]=0;//第二个字母使用完后，取消标记
}
}
map[i]=0;//第二个字母使用完后，取消标记 ，开始测试下一个字母
}
if(ans[0])
for(i=0;i<5;i++)
printf("%c",ans[i]-1+'A');
else
printf("no solution");
printf("\n");
}
return 0;
}

<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int flag=0,c1[20],length,n,c3[15];
bool mark[20];
char c[20];
int cmp(int a,int b)
{
return a>b;//从大到小排序
}
int jie(int n1,int m1)
{
int i,sum1=1;
for(i=1;i<=m1;i++)
sum1*=n1;
return  sum1;
}
int jisuan(int *k)
{
int sum;
sum=k[0]-jie(k[1],2)+jie(k[2],3)-jie(k[3],4)+jie(k[4],5);
return sum;
}
void dfs(int depth)
{
int i,j;
if(depth==5)
{
j=jisuan(c3);
if(j==n)
flag=1;//查询到第一个结果，则直接通过flag控制，结束所有dfs的调用
return ;
}
for(i=0;i<length;i++)
{
if(mark[i])
continue;
mark[i]=1;
c3[depth]=c1[i];
dfs(depth+1);
if(flag)
return ;
mark[i]=0;
}
}
int main()
{
int i,j;
while(scanf("%d",&n)==1&&n)
{
flag=0;
memset(mark,0,sizeof(mark));
scanf("%s",c);
length=strlen(c);
for(i=0;i<length;i++)
c1[i]=c[i]-'A'+1;
sort(c1,c1+length,cmp);
dfs(0);
if(flag)
for(i=0;i<5;i++)
printf("%c",c3[i]-1+'A');
else
printf("no solution");
printf("\n");
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：HDOJ-1015(Safecracker)(多重for循环) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)