A very hard Aoshu problem(dfs或者数位)

题目连接: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

2012 ACM/ICPC Asia Regional Jinhua Online

题解:考虑枚举等号的位置,每次dfs等号左侧的值,对应找等号右边是否有对应的相同值,这里一定要注意调用的时候的细节,就是考虑每个位置的标号

也可以将每个数字之间的位置用二进制表示,枚举完等号后,其他的位置0表示不放+号,1表示放

dfs代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5 char str[17];
 6 int val[17][17];
 7 int len;
 8 int ans;
 9
10 void cal()
11 {
12     memset(val,0,sizeof(val));
13     for(int i = 0 ; i < len ; i++)
14         for(int j = i ; j < len ; j++)
15             for(int k = i ; k <= j ; k++)
16                 val[i][j] = val[i][j]*10+(str[k]-‘0‘);
17 }
18 void dfsr(int lsum,int pos, int rsum)
19 {
20     if(pos>=len)
21     {
22         if(rsum==lsum)
23             ans++;
24             return ;
25     }
26     for(int k = pos+1 ; k <= len ; k++)
27         dfsr(lsum,k,rsum+val[pos][k-1]);
28 }
29 void dfsl(int equ , int pos , int lsum)
30 {
31     if(pos>=equ)
32         dfsr(lsum,equ,0);
33         for(int k = pos+1 ; k <= equ ; k++)
34             dfsl(equ,k,lsum+val[pos][k-1]);
35 }
36
37 int main()
38 {
39     while(~scanf("%s",str)&&str[0]!=‘E‘)
40     {
41         ans = 0 ;
42         len = strlen(str);
43         cal();
44         int equ;
45         for(equ = 1 ; equ < len ; equ++)
46             dfsl(equ,0,0);
47         printf("%d\n",ans);
48     }
49     return 0;
50 }

数位:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 using namespace std;
 5 #define N 17
 6 char a[N];
 7 int equ;
 8 int len;
 9 bool ck(int sum)
10 {
11     int l= 0 , r=0;
12     int cur = 0;
13     for(int i = 0  ; i <= equ ;i++)
14     {
15         if(sum&(1<<i)) {
16             cur = cur * 10 + a[i]-‘0‘;
17             l+=cur;
18             cur = 0;
19         }
20         else cur = cur*10+a[i]-‘0‘;
21     }
22     if(cur != 0) l+=cur;
23     cur = 0 ;
24     for(int i = equ+1 ; i< len ; i++)
25     {
26         if(sum&(1<<i)){
27             cur = cur * 10 + a[i]-‘0‘;
28             r+=cur;
29             cur = 0;
30         }
31         else cur = cur*10+a[i]-‘0‘;
32     }
33     if(cur!=0) r += cur;
34     if(l==r) return true;
35     else return false;
36 }
37 int main()
38 {
39     while(~scanf("%s",a)&&a[0]!=‘E‘)
40     {
41         int ans = 0 ;
42         len = strlen(a);
43         for( equ = 0 ; equ < len-1 ; equ++)
44         {
45             int tm = 1<<(len-1);
46             for(int j = 0 ; j < tm ;j++)
47             {
48                 if(ck(j)){
49                     ans++;
50             //        printf("%d %d\n", equ, j);
51                 }
52             }
53         }
54         printf("%d\n",ans>>1);
55     }
56     return 0;
57 }
时间: 2024-08-25 00:46:01

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

HDU 4403 A very hard Aoshu problem(DFS)

A very hard Aoshu problem 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 pro

HDU4403 A very hard Aoshu problem DFS

A very hard Aoshu problem                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than o

[ACM] hdu 4403 A very hard Aoshu problem (DFS暴搜数字)

A very hard Aoshu problem 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 pro

HDU 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

hdu 4403 A very hard Aoshu problem

hdu 4403 A very hard Aoshu problem 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 DFS 这几天集训,一天也就写个4题,被虐哭QAQ.回寝室后游少说解搜索就大胆搜,最后剪个枝就好了Orz,然后我就尝试解这题(剪枝要风骚).我先枚举等号的位置equ,然后搜索加号add的位置,然后主要的剪枝:如果等号左边运算后小于等号右边各个位上的数的和,那么后面的肯定不满足条件,右边同理.最后要小心爆int,这里吃了很多W

hdu3699 A hard Aoshu Problem 暴搜

http://acm.hdu.edu.cn/showproblem.php?pid=3699 A hard Aoshu Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 968    Accepted Submission(s): 490 Problem Description Math Olympiad is calle

hdu 3699 10 福州 现场 J - A hard Aoshu Problem

Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. Nowadays, Aoshu is getting more and more difficult. Here is a classic Aoshu problem: ABBDE __ ABCCC = BDBDE In the equation above, a letter stands for

2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个mark坑了我好几个小时..哎.太弱. 先DFS这棵树,树形结构转换为线性结构,每个节点有一个第一次遍历的时间和最后一次遍历的时间,之间的时间戳都为子树的时间戳,用线段树更新这段区间即可实现更新子树的效果,用到懒操作节省时间. 坑我的地方: update时,不能写成:tree[rt].mark = 1,

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 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