#include <iostream>
#include <algorithm>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define eps 1e-6
#define pi 3.14159265359
typedef long long LL;
const int maxn = 1001;
int ta, m, len;
int vis[maxn],num[maxn];
char s[maxn],ch[maxn],m_ch[maxn];
map<char,int>mm;
void fun(){
char c = 'A';
for(int i=1;i<=26;i++){
mm[c++] = i;
}
}
int is(int v,int w,int x,int y,int z,int tar){
if(v - w*w + x*x*x - y*y*y*y + z*z*z*z*z == tar) return 1;
else return 0;
}
void dfs(int cur){
if(cur==5){
if( is(num[0],num[1],num[2],num[3],num[4],ta) && strcmp(ch,m_ch)>0 ){
strcpy(m_ch,ch);
}
}else{
for(int i=0;i<len;i++){
if(vis[mm[s[i]]]==0){
num[cur] = mm[s[i]];//储存字符s[i]表示的数字
ch[cur] = s[i];//储存字符s[i]
vis[mm[s[i]]] = 1;//标记用过
dfs(cur+1);
vis[mm[s[i]]] = 0;
}
}
}
}
int main(){
fun();//打表
//WA
//1234567 THEQUICKFROG
//KIUUQ(不能出现两次)
while(~scanf("%d %s",&ta,s) && (ta||strcmp(s,"END"))){
memset(vis,0,sizeof(vis));
memset(ch,'\0',sizeof(ch));
memset(m_ch,'\0',sizeof(m_ch));
memset(num,0,sizeof(num));
len = strlen(s);
sort(s,s+len);//升序排列
dfs(0);
if(strlen(m_ch)==0) printf("no solution\n");
else printf("%s\n",m_ch);
}
return 0;
}
Safecracker-DFS
最新推荐文章于 2021-04-28 17:42:40 发布