题意是A-Z对应1-26,然后给个目标数字和字符串,看看字符串里的某5个字符的组合能不能使v - w^2 + x^3 - y^4 + z^5 = target等式成立,其实多写几个循环也可以达到目的,不过应该会超时,所以还是dfs。

result数组保存最后5个数字,注意要用sort先降序排序,sort默认升序,得加个compare函数,因为本题可能有多解,人家要字典序最大的。num1数组是用来标记该数字被用过没有,注意要memset一下(忘了memset于是每次只能对一组数据也是醉了)。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;
int pow(int x,int y)
{
int z=;
for(;y>;y--)
z*=x;
return z;
}
bool compare(int a,int b)
{
return a>b; }
int num[]={};
int num1[]={};
int result[]={};
char word[];
int tar;
int len;
int dfs(int m,int sum)
{
if(sum==tar&&m==)
return ;
else if(sum!=tar&&m==)
return ;
else if(m<=)
{
for(int i=;i<len;i++)
{
if(!num1[i])
{
num1[i]=;
result[m]=num[i];
if(m%==)
{
if(dfs(m+,sum-pow(num[i],m)))
return ;}
else {
if(dfs(m+,sum+pow(num[i],m)))
return ;} num1[i]=;
}
}
}
return ;
}
int main()
{
while(scanf("%d%s",&tar,word)!=EOF)
{
memset(num1,,sizeof(num1));
if(!strcmp(word,"END"))
break;
len=strlen(word);
for(int i=;i<len;i++)
{
num[i]=word[i]-'A'+;
}
sort(num,num+len,compare);
if(dfs(,))
{
for(int i=;i<;i++)
printf("%c",result[i]+'A'-);
printf("\n");
}
else printf("no solution\n");
}
return ;
}

Hdu1015&&寒假作业第二组I题的更多相关文章

  1. 寒假作业第二组C题题解

    这道题题意很简单,主要是练习map的使用.看输入有三个数据,水果名,地名,和出现次数.再看输出,很容易想到map<string,int> string是水果,int是次数,那个地名怎么用m ...

  2. 寒假作业第二组E题题解

    注意看题,注意看题,注意看题.重要的事情三遍感觉都不够.不怕大家笑话,这道题RuntimeError 9次,我一直以为是哪里越界了,结果最后发现的时候,真是无语了,题目里说了,所有的integer都不 ...

  3. 寒假作业第二组P&&Q&&R题解

    P的题意是有M份作业,这些作业有不同的截止日期,超过截止日期完成,不同的作业有不同的罚分,求如何完成罚分最低. 首先,从截止日期最长的那个作业到截止日期,这些天数是固定的,所做的就是把这些作业填进这些 ...

  4. 寒假作业第二篇随笔(A+B)

    Github链接:https://github.com/heihuifei/object-oriented A+B Format (20) Calculate a + b and output the ...

  5. 福建工程学院寒假作业第一周G题

    涨姿势题1 TimeLimit:1000MS  MemoryLimit:128000KB 64-bit integer IO format:%lld   涨姿势题就是所谓的优化题,在组队赛中,队伍发现 ...

  6. FJUT寒假作业第二周G题解快速幂

    题目来源:http://210.34.193.66:8080/vj/Contest.jsp?cid=161#P6     题意:求n个数字的乘积对c取摸.主要就是有快速幂扩展到广义幂的过程. 首先题目 ...

  7. 福建工程学院寒假作业第一周F题

    Subsequence TimeLimit:1000MS  MemoryLimit:65536K 64-bit integer IO format:%lld   问题描述: A sequence of ...

  8. FJUT寒假作业第二周C题解(位运算)

    题目来源:http://210.34.193.66:8080/vj/Contest.jsp?cid=161#P2 题意比较好理解.如果直接按题目要求一步一解.一定超时.作为一个懒人也不会这么暴力一个肯 ...

  9. 2016蓝桥杯省赛C/C++A组第六题 寒假作业

    题意:现在小学的数学题目也不是那么好玩的. 看看这个寒假作业: □ + □ = □ □ - □ = □ □ × □ = □ □ ÷ □ = □ 每个方块代表1~13中的某一个数字,但不能重复. 比如: ...

随机推荐

  1. 分享知识-快乐自己:intellij Idea报错Could not autowire. No beans of...

    intellig idea 使用@Resource或者@Autowire报错,出现红色波浪线: 虽然不影响使用,但是看着很不爽,所以还是解决了下: 报错提示: Could not autowire. ...

  2. C++(一)— stringstream的用法

    输入输出的头文件 <iostream>  string流的头文件 <sstream>  文件流的头文件   <fstream> 1.利用输入输出做数据转换 stri ...

  3. openfire性能测试

    使用TSung对Jabber服务器openfire进行压力测试 http://blog.csdn.net/spider_zhcl/article/details/6073920 Tsung负载测试Ti ...

  4. 团队作业第5周——测试与发布(Alpha版本)

    1.发现的bug a.同时按下和蛇前进方向相反的键和垂直方向的任意一个键贪吃蛇会死亡(比如贪吃蛇向右行走,同时按左上或左下都会game over) b.刷新的苹果会在蛇身上出现 暂时还没能力修复,以后 ...

  5. [acm]HDOJ 3082 Simplify The Circuit

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3082 字符串处理+并联电阻公式 //11481261 2014-08-18 16:52:47 Acc ...

  6. POJ3693Maximum repetition substring (循环节)(后缀数组+RMQ)

    The repetition number of a string is defined as the maximum number R such that the string can be par ...

  7. python-while循环,for ,以及字符串格式化

    1.字符串格式化 name="suwukong" print("欢迎",name,"光临")print("欢迎 "+na ...

  8. 【LeetCode】091. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  9. 创建自己的YUM仓库

    1. 首先,假定我们应用的名字叫helloworld(可以参考简单 RPM 包制作来创建两个版本helloworld安装RPM包,helloworld-1.0.0-1.el6.x86_64.rpm和h ...

  10. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...