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
题解:考虑枚举等号的位置,每次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 }

 

转载于:https://www.cnblogs.com/shanyr/p/4750337.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值