HDU 2266 How Many Equations Can You Find(DFS)

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[120];
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=0;
    for(int i=1;i<=Length-Now_Position;i++)
    {
        Temp_Value=Temp_Value*10+String[i-1+Now_Position]-‘0‘;
        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=0;
        Length=strlen(String);
        Search_For_Answer(0,0);
        printf("%d\n",Answer);
    }
    return 0;
}
时间: 2024-08-25 19:22:41

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

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

http://acm.hdu.edu.cn/showproblem.php?pid=2266 给一个字符串和一个数n,在字符串中间可以插入+或者 -,问有多少种等于n的情况. 要注意任意两个数之间都可以插入也可以不插入,注意枚举完所有情况. #include <cstdio> #include <cstring> char s[15]; int n,l,cnt; void dfs(int k,int sum) { if(k==l) { if(sum==n) cnt++; retur

hdu 2266 How Many Equations Can You Find

How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 644    Accepted Submission(s): 424 Problem Description Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,

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

题目 这是传说中的深搜吗....不确定,,,,貌似更加像是模拟,,,, //我要做深搜题目拉 //实际上还是模拟 #include<iostream> #include<string> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int ans; char a[20]; __int64 n;

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

HDU 1016 Prime Ring Problem (素数筛+DFS)

题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... 1 //1016 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std ; 7 8 bool vis[21]; 9 int prime[42] ,cs[21]; 10 int n ; 11

杭电2266 How Many Equations Can You Find【DFS】

How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 892    Accepted Submission(s): 590 Problem Description Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5

hdu 2266 dfs

How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 985    Accepted Submission(s): 659 Problem Description Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5

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

How Many Equations Can You Find(dfs)

How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 714    Accepted Submission(s): 467 Problem Description Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5