题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4403

A very hard Aoshu problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1080    Accepted Submission(s): 742

Problem Description
Aoshu
is very popular among primary school students. It is mathematics, but
much harder than ordinary mathematics for primary school students.
Teacher Liu is an Aoshu teacher. He just comes out with a problem to
test his students:

Given a serial of digits, you must put a '='
and none or some '+' between these digits and make an equation. Please
find out how many equations you can get. For example, if the digits
serial is "1212", you can get 2 equations, they are "12=12" and
"1+2=1+2". Please note that the digits only include 1 to 9, and every
'+' must have a digit on its left side and right side. For example,
"+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and
"11+1=12" are different equations.

 
Input
There
are several test cases. Each test case is a digit serial in a line. The
length of a serial is at least 2 and no more than 15. The input ends
with a line of "END".
 
Output
For each test case , output a integer in a line, indicating the number of equations you can get.
 
Sample Input
1212
12345666
1235
END
 
Sample Output
2
2
0
 
Source
题解:考虑枚举等号的位置,每次dfs等号左侧的值,对应找等号右边是否有对应的相同值,这里一定要注意调用的时候的细节,就是考虑每个位置的标号
也可以将每个数字之间的位置用二进制表示,枚举完等号后,其他的位置0表示不放+号,1表示放
dfs代码:
 #include<cstdio>
#include<cstring>
#include<string>
using namespace std;
char str[];
int val[][];
int len;
int ans; void cal()
{
memset(val,,sizeof(val));
for(int i = ; i < len ; i++)
for(int j = i ; j < len ; j++)
for(int k = i ; k <= j ; k++)
val[i][j] = val[i][j]*+(str[k]-'');
}
void dfsr(int lsum,int pos, int rsum)
{
if(pos>=len)
{
if(rsum==lsum)
ans++;
return ;
}
for(int k = pos+ ; k <= len ; k++)
dfsr(lsum,k,rsum+val[pos][k-]);
}
void dfsl(int equ , int pos , int lsum)
{
if(pos>=equ)
dfsr(lsum,equ,);
for(int k = pos+ ; k <= equ ; k++)
dfsl(equ,k,lsum+val[pos][k-]);
} int main()
{
while(~scanf("%s",str)&&str[]!='E')
{
ans = ;
len = strlen(str);
cal();
int equ;
for(equ = ; equ < len ; equ++)
dfsl(equ,,);
printf("%d\n",ans);
}
return ;
}

数位:

 #include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define N 17
char a[N];
int equ;
int len;
bool ck(int sum)
{
int l= , r=;
int cur = ;
for(int i = ; i <= equ ;i++)
{
if(sum&(<<i)) {
cur = cur * + a[i]-'';
l+=cur;
cur = ;
}
else cur = cur*+a[i]-'';
}
if(cur != ) l+=cur;
cur = ;
for(int i = equ+ ; i< len ; i++)
{
if(sum&(<<i)){
cur = cur * + a[i]-'';
r+=cur;
cur = ;
}
else cur = cur*+a[i]-'';
}
if(cur!=) r += cur;
if(l==r) return true;
else return false;
}
int main()
{
while(~scanf("%s",a)&&a[]!='E')
{
int ans = ;
len = strlen(a);
for( equ = ; equ < len- ; equ++)
{
int tm = <<(len-);
for(int j = ; j < tm ;j++)
{
if(ck(j)){
ans++;
// printf("%d %d\n", equ, j);
}
}
}
printf("%d\n",ans>>);
}
return ;
}

A very hard Aoshu problem(dfs或者数位)的更多相关文章

  1. HDU 4403 A very hard Aoshu problem(DFS)

    A very hard Aoshu problem Problem Description Aoshu is very popular among primary school students. I ...

  2. HDU4403 A very hard Aoshu problem DFS

    A very hard Aoshu problem                           Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0

    Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...

  4. HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)

    Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...

  5. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  6. HDU 4403 A very hard Aoshu problem(dfs爆搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...

  7. HDU 4403 A very hard Aoshu problem (DFS暴力)

    题意:给你一个数字字符串.问在字符串中间加'='.'+'使得'='左右两边相等. 1212  : 1+2=1+2,   12=12. 12345666 : 12+3+45+6=66.  1+2+3+4 ...

  8. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

  9. A very hard Aoshu problem

    A very hard Aoshu proble Problem Description Aoshu is very popular among primary school students. It ...

随机推荐

  1. Python学习日记:day7-----集合

    1.基础数据类型汇总补充 1,list: 在循环一个列表是,最好不能删除列表中的元素. 2,bool 空列表.int:0.空str.空dict.空set--->bool:false 其余为tru ...

  2. Panel控件的使用

    我们对控件进行分组的原因不外乎三个: 1.为了获得清晰的用户界面而将相关的窗体元素进行可视化分组. 2.编程分组,如对单选按钮进行分组. 3.为了在设计时将多个控件作为一个单元来移动. 在vb.net ...

  3. laravel and lumen 软删除操作

    知识都是有联系的,这绝对是真理.作为一名小白,看了一点官方文档,把我自己理解的软删除操作给大家讲讲.有些就是套用官方文档的话. 定义:什么是软删除呢,所谓软删除指的是数据表记录并未真的从数据库删除,而 ...

  4. 在Ubuntu14.04下安装 labelImg (标数据用)

    安装 SIP 下载 SIP 并解压 : $ sudo python configure.py $ make $ sudo make install 安装 依赖库 $  sudo apt-get ins ...

  5. 为什么要学ADO.NET。。。什么是ADO.NET。。。

    之前学的 •只能在查询分析器里查看数据,操作数据,我们不能让普通用户去学sql,所以我们搭建一个界面(Web Winform)让用户方便的操作数据库中的数据.   •ADO.NET就是一组类库,这组类 ...

  6. 用C#实现微信“跳一跳”小游戏的自动跳跃助手

    一.前言: 前段时间微信更新了新版本后,带来的一款H5小游戏“跳一跳”在各朋友圈里又火了起来,类似以前的“打飞机”游戏,这游戏玩法简单,但加上了积分排名功能后,却成了“装逼”的地方,于是很多人花钱花时 ...

  7. Python学习_10__python2到python3

    同样作为动态语言,python的面相对像和ruby有很多类似的地方,这里还是推荐<Ruby元编程>一书来参考学习python的面向对象.然而python并不是纯面向对象设计,所以很多rub ...

  8. 浅析Python解释器的设计

    从现代编译器的角度看,解释器和编译器的边界已经相当的模糊.我们后面的讨论说到的编译器就是Python的解释器,没有特别说明的指的是CPython的实现. 内存管理(Memory Management) ...

  9. opacity的背景透明&background中rgba的背景色透明

    近期使用css实现了一个loading旋转加载的图片效果,类似gif动画 过程中,需要透明背景,但是图片不要透明 只要背景透明!只要背景透明!只要背景透明! 这里对透明模糊了,两种写法,模糊了 A: ...

  10. Mysql:执行source sql脚本时,出现:error 2

    Centos下部署mysql: 1.yum -y install mysql*; 2.service mysqld start; 3.chkconfig mysqld on; 4.设置用户名和密码:m ...