How Many Equations Can You Find

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sign ‘+’ or ’-’ between the characters. Just like give you a string “12345”, you can work out a string “123+4-5”. Now give you an integer N, please tell me how many ways can you find to make the result of the string equal to N .You can only choose at most one sign between two adjacent characters.

Input

Each case contains a string s and a number N . You may be sure the length of the string will not exceed 12 and the absolute value of N will not exceed 999999999999.

Output

The output contains one line for each data set : the number of ways you can find to make the equation.

Sample Input

123456789 3 21 1

Sample Output

18 1

简单翻译:

给你两个数n,m.你需要在n的中间填上加号或减号,或者不填。让n这个部分的值等于m。问有多少种方案。

举个例子:n=12345.

你可以操作它,使它变成这样 123+4-5=122。

解题思路:

dfs,考虑已经处理的数字的个数和当前这部分的值,处理到终点时,如果等于m,结果+1.

代码:

#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
char String[];
LL Equation;
int Length,Answer;
void Search_For_Answer(int Now_Position,LL Now_Value)
{
if(Now_Position>=Length)
{
if(Now_Value==Equation) Answer++;
return;
}
LL Temp_Value=;
for(int i=;i<=Length-Now_Position;i++)
{
Temp_Value=Temp_Value*+String[i-+Now_Position]-'';
Search_For_Answer(Now_Position+i,Now_Value+Temp_Value);
if(Now_Position)
Search_For_Answer(Now_Position+i,Now_Value-Temp_Value);
}
}
int main()
{
while(scanf("%s%lld",String,&Equation)!=EOF)
{
Answer=;
Length=strlen(String);
Search_For_Answer(,);
printf("%d\n",Answer);
}
return ;
}

HDU 2266 How Many Equations Can You Find(DFS)的更多相关文章

  1. HDOJ(HDU).2266 How Many Equations Can You Find (DFS)

    HDOJ(HDU).2266 How Many Equations Can You Find (DFS) [从零开始DFS(9)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零 ...

  2. hdu - 2266 How Many Equations Can You Find (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2266 给一个字符串和一个数n,在字符串中间可以插入+或者 -,问有多少种等于n的情况. 要注意任意两个数之间都可 ...

  3. HDU 2266 How Many Equations Can You Find(模拟,深搜)

    题目 这是传说中的深搜吗....不确定,,,,貌似更加像是模拟,,,, //我要做深搜题目拉 //实际上还是模拟 #include<iostream> #include<string ...

  4. hdu 2266 dfs+1258

    How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  5. 【HDOJ】2266 How Many Equations Can You Find

    简单DFS. #include <cstdio> #include <cstring> #define MAXN 15 char str[MAXN]; __int64 x; i ...

  6. hdu 2266 dfs

    题意:在数字之间添加运算符号,使得结果等于题目中要求的Sample Input123456789 321 1Sample Output181 这题虽然看起来比较简单,但是之前和差的状态不太好表示,因此 ...

  7. HDU 5416 CRB and Tree(前缀思想+DFS)

    CRB and Tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  8. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. How Many Equations Can You Find(dfs)

    How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. 关于Fragment与Activity的想法

    View,Fragment,Activity,ListView等都会涉及到Layout文件 不要从Layout来考虑,而是从Activity,Fragment,来考虑,Layout只是他们的一个属性 ...

  2. 向着DJANGO奔跑!

    这个项目明天上半年要弄好,就牛X了哈哈. 平台化运维.PYTHON,SVN,SALTSTACK,.....一锅端~~:) from django.contrib import admin # Regi ...

  3. mysql use mysql hang

    uat-db03:/root# mysql -A -uroot -p1234567 Warning: Using a password on the command line interface ca ...

  4. java package 重命名时注意事项

    如果要对包重命名时,需要关注以下方面: 1. java关联类里的重命名(这个一般通过开发工具会自动修正,如eclipse) 2.配置文件,如原先配置为com.abc,现在更名为com.abc123,这 ...

  5. cf486B OR in Matrix

    B. OR in Matrix time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. checkbox全选和反选

     $("#CheckBox").click(function () {                 if (this.checked) {                    ...

  7. 3 Sum Closest 解答

    Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  8. nodejs 设置网络代理

    在使用nodejs的过程中,加入使用代理上网,那么安装组件会失败,此时配置代理即可,命令如下: [root@oracle ~]#npm config set proxy=http://10.101.1 ...

  9. 第07讲- Android项目的打包apk

    第07讲Android项目的打包apk 方法一:在工作目录bin文件夹下有一个与项目同名的apk文件 (最懒惰的方式,不推荐,不安全,不利于版本更新,只有在开发模式时使用) 方法二:使用key方式 签 ...

  10. mysql-error --(ERROR 1135 (HY000): Can't create a new thread (errno 11); if you are not out)

    报错信息: ERROR 1135 (HY000): Can't create a new thread (errno 11); if you are not out 解决办法: # 查看限制情况 [r ...