问题链接:Problem H
问题简述:
给出一个结果和一段大写英文字母字符串,每个英文字母代表的数字为其在字母表中的位置,求出能使等式
v - w^2 + x^3 - y^4 + z^5 = target 成立的字母排列顺序,若有多个结果,输出字典序最大的。
问题分析:
用DFS逐个位置搜索即可,字典序最大的问题可预先将字符按由大到小排列。
AC通过的C++语言程序如下:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
bool vis[28];
char a[28],c[6],ans[6];
int flag,n,len;
bool cmp(char a,char b){
return a>b;
}
int judge(int v,int w,int x,int y,int z){
if(v-w*w+x*x*x-y*y*y*y+z*z*z*z*z==n)
return 1;
else
return 0;
}
void DFS(int depth){
if(depth==5){
if(judge(c[0]-64,c[1]-64,c[2]-64,c[3]-64,c[4]-64)==1){
flag=1;
strcpy(ans,c);
}
return;
}
else{
for(int i=0;i<len;i++){
if(!vis[i]){
vis[i]=1;
c[depth]=a[i];
DFS(depth+1);
vis[i]=0;
if(flag)
return;
}
}
}
}
int main(){
while(cin>>n>>a){
if(n==0&&strcmp(a,"END")==0)
break;
len=strlen(a);
memset(ans,'\0',sizeof(ans));
memset(vis,0,sizeof(vis));
sort(a,a+len,cmp);
flag=0;
DFS(0);
if(flag)
cout<<ans<<endl;
else
cout<<"no solution"<<endl;
}
}