ZCMU—1795

Problem C: wjw的hard problem

Time Limit: 1 Sec   Memory Limit: 128 MB
[ Submit][ Status][ Web Board]

Description

Wjw is very popular among primary school students.
Now give a string of numbers, you need to add a "=" and a number of "+" so that the string is divided into an equation, and both sides are equal, such as the given number is "1212", then it We can form "1 + 2 = 1 + 2" and "12 = 12" two equations, note that the number does not contain 0, and each "+" on both sides must be a number, that is, "1 ++ 1" "++ 2" is illegal. Now wjw would like to know how a number of different tones can be divided into several 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 20. The input ends with a line of "0".

Output

For each test case , output a integer in a line, indicating the number of equations you can get.

Sample Input

1212
12345666
1235
0

Sample Output

2
2
0

【分析】

这种题目先看数据范围...所以直接dfs,模拟等号位置,左边右边两个dfs匹配.... 但是这里有一点需要注意,dfs的深度虽然不大,但是总的运行次数还是不小的,所以需要预处理f[i][j]表示第i位到第j位能组成的数字是什么,有了这个优化之后直接匹配就可以了 。。
【代码】
#include <cstdio>  
#include <cstring>  
char s[100];  
int len,ans;  
int f[100][100];  
void findd(int deep,int sum,int summ)  
{  
    if(deep==len)  
    {  
        if(sum==summ) ans++;  
        return;  
    }  
    for(int i=deep;i<len;i++)  
        findd(i+1,sum+f[deep][i],summ);  
}  
void find(int deep,int end,int sum)  
{  
    if(deep==end) findd(end,0,sum);  
    for(int i=deep;i<end;i++)  
        find(i+1,end,sum+f[deep][i]); 
}  
int main()  
{  
/*  FILE *fp1, *fp2;
    fp1 = fopen("input03.in","r");
    fp2 = fopen("output03.out","w");
 */  
    while(~scanf("%s",s))  
    {  
        if(s[0]=='E') break;  
        memset(f,0,sizeof(f));  
        len=strlen(s);  
        ans=0;  
        for(int i=0;i<len;i++)  
            for(int j=i;j<len;j++)    
                for(int k=i;k<=j;k++)  
                    f[i][j]=f[i][j]*10+s[k]-48;  
        for(int i=1;i<len;i++)  
            find(0,i,0);  
        printf("%d\n",ans);  
    }  
//  fclose(fp1);
//  fclose(fp2);
    return 0;  
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值