标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11575 Accepted Submission(s):
5965
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 char ch[35],str[35],w[35]; 7 bool visit[30]; 8 int n,len; 9 void DFS(int k) 10 { 11 int i,j,t; 12 if(k==5) 13 { 14 t=(str[0]-‘A‘+1)-pow(str[1]-‘A‘+1,2)+pow(str[2]-‘A‘+1,3)-pow(str[3]-‘A‘+1,4)+pow(str[4]-‘A‘+1,5); 15 if(t==n&&strcmp(str,w)>0) //求出字典数最大的5位字符串 16 { 17 strcpy(w,str); 18 } 19 return ; 20 } 21 for(i=0; i<len; i++) //暴力枚举 22 { 23 if(!visit[ch[i]-‘A‘]) 24 { 25 str[k]=ch[i]; 26 visit[ch[i]-‘A‘]=true; 27 DFS(k+1); 28 visit[ch[i]-‘A‘]=false; 29 } 30 } 31 } 32 int main() 33 { 34 int i,j,t,m; 35 while(~scanf("%d %s",&n,ch)) 36 { 37 if(n==0&&strcmp(ch,"END")==0) 38 break; 39 memset(visit,false,sizeof(visit)); 40 memset(w,‘\0‘,sizeof(w)); 41 len=strlen(ch); 42 DFS(0); 43 if(strlen(w)==0) printf("no solution\n"); 44 else printf("%s\n",w); 45 } 46 return 0; 47 }
标签:
原文地址:http://www.cnblogs.com/pshw/p/5236108.html