题目连接: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. 利用aop插入异常日志的2种方式

    AOP是面向切面编程,利用这个技术可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各个部分的耦合性降低,提高代码的可重用性,同时提高开发效率(来自百度百科). Spring AOP有两种实现方式,一 ...

  2. Spring(概念)

    在本文中只讲述一些概念性的东西,因为我在开始学习JAVA的时候对这些概念性的东西总是不太理解,总结总结再感悟一下,也方便后人. 理解的不深,用通俗的语言讲一下: 百度百科这样介绍: spring框架主 ...

  3. 【model模型传入view的数据类型错误】传入字典的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery`1[MapScience.PovertyAlleviation.Web.Models.Qu

    出现这个问题的原因是控制器中传给view的数据类型与View中设置的model类型不一致导致,比如控制器返回的IList类型的,而你在View里面model设置的是IEnumerable<> ...

  4. Java I/O---Reader & Writer(字符流)

    1.Reader & Writer 当我们初次看见Reader和Writer类时,可能会以为这是两个用来替代InputStream和OutputStreamt的类,但实际上并非如此. 尽管一些 ...

  5. java I/O---复制文本文件

    利用FileInputStream 和FileOutputStream 复制文本 1 public class CopyTextByBuffer { 2 3 /** 4 * @param args 5 ...

  6. NET Framework 版本和依赖关系

    原文:https://docs.microsoft.com/zh-cn/dotnet/framework/migration-guide/versions-and-dependencies 每个版本的 ...

  7. Linux_异常_08_本机无法访问虚拟机web等工程

    这是因为防火墙的原因,把响应端口开启就行了. # Firewall configuration written by system-config-firewall  # Manual customiz ...

  8. Django学习日记06_视图_URLconf、View

    URLconf Django通过URLconf来响应网页请求,在项目设置settings.py中,设定了ROOT_URLCONF值来指定默认的URLconf(即mysite.urls),当HTTPRe ...

  9. Debian安装fail2ban来防止扫描

    vps的root密码不要设置的太简单,这样很容易被攻破,你可以安装如下软件来降低vps被攻破的机会. 输入如下命令: apt-get install fail2ban 提示如下表示安装完成: root ...

  10. 基于web的网上书城系统开发-----登录注册扩展-------验证码功能

    public class CheckCode extends HttpServlet { private static final long serialVersionUID = 1L; privat ...