HDU1015
2016-04-19 23:15
295 查看
<h1 style="COLOR: #1a5cc8">Safecracker</h1><span size="+0" style=""><strong><span style="font-family: Arial; color: green; font-size: 12px;">Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11887 Accepted Submission(s): 6142 </span></strong></span> <div class="panel_title" align="left">Problem Description</div><div class="panel_content">=== 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." </div><div class="panel_bottom"> </div> <div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="FONT-FAMILY: Courier New,Courier,monospace">1 ABCDEFGHIJKL 11700519 ZAYEXIWOVU 3072997 SOUGHT 1234567 THEQUICKFROG 0 END</div>
[align=left]Sample Output[/align]
LKEBA YOXUZ GHOST no solution
#include <iostream> #include<algorithm> #include<cmath> #include<cstring> #include <cstdio> using namespace std; int m,len; struct node { char ch; int num; }; node ans[30]; int vist[30],flag; char str[10]; int cmp(node a,node b) { return a.ch>b.ch; } int back(int a,int b) { if(a==1)return b; if(a==2)return -b*b; if(a==3)return b*b*b; if(a==4)return -b*b*b*b; if(a==5)return b*b*b*b*b; return 0; } void dfs(int sum,int k) { if(flag) return; if(k==6) { if(sum==m) { for(int i=0;i<6;i++) printf("%c",str[i]); printf("\n"); flag=1; return; } return; } for(int i=0;i<len;i++) { if(vist[i]==0) { vist[i]=1; str[k]=ans[i].ch; int s=back(k,ans[i].num); dfs(sum+s,k+1); if(flag) return; vist[i]=0; } } return; } int main() { int i,k; char ss[20]; while(cin>>m) { flag=0; k=0; cin>>ss; len=strlen(ss); if(m==0&&strcmp(ss,"END")==0) break; memset(vist,0,sizeof(vist)); for(i=0;i<len;i++) { ans[k].ch=ss[i]; ans[k++].num=ss[i]-'A'+1; } sort(ans,ans+k,cmp); dfs(0,1); if(!flag) printf("no soultion\n"); } }
相关文章推荐
- Java 多线程
- C语言运算符表达式
- 广商14级软件工程分数:第六回合
- 第二天
- 数的长度
- 并查集详解
- Linux零基础入学之1-4 Linux文件管理-xfs文件系统的备份恢复-服务器来电后自动开机
- Python 第三方库总论
- VC操作word实例详解
- Android内存溢出问题
- Java语言基础1
- 循序渐进之Spring AOP(6) - 使用@Aspect注解
- Entity Framework
- 个人工作总结02
- 算法导论, 10.2-1
- Hibernate懒加载在SSH项目中的配置及原理
- POJ1066
- 教师面试详细流程
- 响应式web设计
- Mac系统终端命令行不执行命令 总出现command not found解决方法