标签:algo hdu tar return turn targe mes for i++
给你大写字母的字符串,A=1,...Z=26,以及target
问你是否有v - w^2 + x^3 - y^4 + z^5 = target
有输出字典序大的那个字符串
dfs
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<math.h>
using namespace std;
#define ll long long
char s[20],a[20];
int n,vis[20],len;
bool cmp(const char &a,const char &b){
return a>b;
}
int dfs(int sum,int step){
if(step==5){
if(sum==n) return 1;
else return 0;
}
int tem;
if(step&1) tem=-1;
else tem=1;
for(int i=0;i<len;i++)
{
if(!vis[i]){
vis[i]=1;
int x=s[i]-‘A‘+1;
int nsum=sum+tem*pow(x,step+1);
if(dfs(nsum,step+1)){
a[step]=s[i];
return 1;
}
vis[i]=0;
}
}
return 0;
}
int main(){
//freopen("in.txt","r",stdin);
while(cin>>n>>s&&n&&s!="END"){
len=strlen(s);
sort(s,s+len,cmp);
memset(vis,0,sizeof(vis));
if(dfs(0,0)) cout<<a<<endl;
else cout<<"no solution\n";
}
return 0;
}
标签:algo hdu tar return turn targe mes for i++
原文地址:https://www.cnblogs.com/mch5201314/p/9813880.html