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. xml之一

    xml基础知识 标记语言<> XML与HTML区别 1.HTML主要用来显示  XML是用来进行数据传输 2.HTML是一种预定义的(<a>表示超链接),xml不是预定义的(& ...

  2. (五)使用GitHub的前期准备

    创建账户 创建账号成功后将得到一个个人的公开页面URL:https://github.com/xkfx. 设置头像 设置SSH Key SSH 为 Secure Shell 的缩写. from bai ...

  3. GD32芯片移植完全攻略

    GD32是国产兆易创新公司生产的完全兼容STM32系列的Cortex-M3处理器,具有几大亮点:1,高主频108MHz.性能提升30%以上,可超频到120MHz2,Flash零等待.STM32的72M ...

  4. [LeetCode] 701. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  5. pip 更换国内镜像与记录

    更换pip源到国内镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/sim ...

  6. springboot中配置主从redis

    测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...

  7. bootstrap栅格系统进行偏移格式

    本文为博主原创,转载请注明出处: offset偏移都是向右偏移,且只能向右偏移,例: col-md-offset-2,向右偏移两列. col-md-pull-偏移数值         //向左偏移 c ...

  8. python 处理命令行参数--转载

    标题写了那么久,现在现在才有时间,整理下自己的思路.首先先总结下自己对sys模块的理解.手册上对sys的描述是系统参数和系统函数,这里的系统实际上是python解释器.这个模块提供了用户可以访问的解释 ...

  9. Spring/Spring MVC/Spring Boot的区别

    1.spring boot更简单,容易上手: 2.spring boot对第三方技术进行了很好的封装,提供了大量的第三方接口: 3.通过依赖配置,不需要XML等配置文件: 4.提供了安全性等特性. S ...

  10. DATEDIFF 和 DATEADD

    /* DATEDIFF函数计算两个日期之间的小时.天.周.月.年等时间间隔总数 语法 DATEDIFF(interval, date1, date2[, firstdayofweek[, firstw ...