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 ,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
 大意:
给出一个字符串,往任意二个字符间添加‘+’‘-‘,使得其值等于给定的aim,求方案个数,字符串前后均无符号;
自己第一次使用dfs保存一个加工过的字符串,再用一个pd函数判定此字符串的和;
其实可以直接暴力dfs水果,更省时:                      //ps:55555555~~~感觉自己好傻

#include<bits/stdc++.h>
using namespace std;
char num[15],b[105];
int aim,ans,n;
void dfs(int cur,int sum)
{
if(cur==n){
if(sum==aim) ans++;
return;
}
int t=0;
for(int i=cur;i<n;i++){
t=t*10+num[i]-'0';                         //其实就是暴力枚举出所有的+-情况
dfs(i+1,sum+t);
if(cur>0) dfs(i+1,sum-t);
}
}

int main()
{
while(cin>>num>>aim/*scanf("%s%d",num,&aim)!=EOF*/){
n=strlen(num);
ans=0;
dfs(0,0);
printf("%d\n",ans);
}
return 0;
}

例如 1234
也是关于深搜的一些看法吧:
此类问题类似“决策”,就是每一次/每一个位数对应着几个状态,取/不取或/////
想到之前的全排列问题也是如此,只不过那个问题所有的位置都要有数字只不过要求顺序不同和去重:
这道题与1258有点类似吧
1258是每一位的数字加/不加,且要去重:由于每次考虑两个状态:加/不加,如果相连的几个数字都是同一个数字的话会造成重复解,
每次两个状态,展开后就是一颗二叉树:假设有4个数abcd(*表示不加)
                                  a                                                     *
                        b                *                                       b                  *
                     c     *           c     *                              c      *           c      *
                 d   *   d   *    d   *   d   *                     d    *  d   *    d   *    d   *
 
每个字母下面对应的两个状态如此,如果假设a==b则:
                                  a                                                     *
                        a                *                                       a                 *
                     c     *           c     *                              c      *           c      *
                 d   *   d   *    d   *   d   *                     d    *  d   *    d   *    d   *
会发现左边的右树和右边的左树是重复的,只要出现重复得数就会出现此情况,另一颗树的一颗子树必然会与另一颗的一颗子树相同结果,因为只是前两个位置调换后面出现的所有情况是一样的;
这道题就是在任意相邻数字间加+-号;

hdu 2266 dfs+1258的更多相关文章

  1. hdu 2266 dfs

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

  2. 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 [从零 ...

  3. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  4. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

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

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

  6. 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 ...

  7. Sum It Up POJ 1564 HDU 杭电1258【DFS】

    Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...

  8. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  9. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

随机推荐

  1. TED #09# You don't have to be an expert to solve big problems

    Tapiwa Chiwewe: You don't have to be an expert to solve big problems Collection noticed a haze hangi ...

  2. Python Web学习笔记之TCP、UDP、ICMP、IGMP的解释和区别

    TCP与UDP解释 TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据.TCP提供超时重发,丢弃重复数据, ...

  3. 05: MySQL高级查询

    MySQL其他篇 目录: 参考网站 1.1 GROUP BY分组使用 1.2 mysql中NOW(),CURDATE(),CURTIME()的使用 1.3 DATEDIFF() 函数 1.4 DATE ...

  4. 20165310_JavaExp1

    20165310_JavaExp1_Java开发环境的熟悉 一.Exp1 Exp1_1 实验目的与要求: 使用JDK编译.运行简单的Java程序: 使用Vim进行Java源代码编译: 利用Git上传代 ...

  5. C# 获取当前IIS请求地址

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  6. linux下递归列出目录下的所有文件名(不包括目录)

    1.linux下递归列出目录下的所有文件名(不包括目录) ls -lR |grep -v ^d|awk '{print $9}'2.linux下递归列出目录下的所有文件名(不包括目录),并且去掉空行 ...

  7. Pandas fillna('Missing')

    https://blog.csdn.net/donghf1989/article/details/51167083/ .使用0替代缺失值(当然你可以用任意一个数字代替NaN) df.fillna(0) ...

  8. Farey Sequence (素筛欧拉函数/水)题解

    The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/ ...

  9. 51nod 1242 斐波那契数列的第N项

    之前一直没敢做矩阵一类的题目 其实还好吧 推荐看一下 : http://www.cnblogs.com/SYCstudio/p/7211050.html 但是后面的斐波那契 推导不是很懂  前面讲的挺 ...

  10. MVC ---- EF批处理

    #region 批处理 ///<summary> ///两增一删一改 ///</summary> public void Save(){ //新增参一 Parameter pa ...