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. HTML5 -canvas拖拽、移动 绘制图片可操作移动,拖动

    关于canvas 的基础知识就不多说了,可以进这个网址学习 http://www.w3school.com.cn/html5/html_5_canvas.asp 对于canvas 和 SVG 其实一开 ...

  2. JUC原子类 1

    根据修改的数据类型,可以将JUC包中的原子操作类可以分为4类. 1. 基本类型: AtomicInteger, AtomicLong, AtomicBoolean ; 2. 数组类型: AtomicI ...

  3. linux环境下安装jdk1.6

    卸载rpm版的jdk: #rpm -qa|grep jdk 显示:jdk1.6.0_29-fcs 卸载:#rpm -e --nodeps jdk1.6.0_29-fcs 1.从sun公司网站www.s ...

  4. 20145321《网络对抗技术》逆向与Bof基础

    20145321<网络对抗技术>逆向与Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...

  5. visual studio扩展插件Visual Assist x给代码插入注释模板(转载)

    转载:http://www.cnblogs.com/xiongmao-cpp/p/5196555.html Visual Assist 是由Whole Tomato公司为Microsoft Visua ...

  6. 零碎知识点 telnet操作IMAP查看邮件

    1.用SQL语句查找出字符型字段内的最大值 select max(cast(字段 as int)) from 表 注:因为数据库是按字符比较的,10,9.因为9比1大,而只要有一位数字大,10后面就不 ...

  7. rabbitmq direct、fanout、topic 三种Exchange java 代码比较

    Producer端 1.channel的创建 无论是才用什么样的Exchange,创建channel代码都是相同的,如下 ConnectionFactory factory = new Connect ...

  8. printf("%f\n",5);

    http://zhidao.baidu.com/link?url=87OGcxtDa6fQoeKmk1KylLu4eIBLJSh7CA3n5NWY-Ipm9TxZViFnIui307duCXWhaM0 ...

  9. 【复制虚拟机】虚拟机复制后无ip的问题

    先编辑虚拟机选项,把网络适配器删掉后保存,再重新添加网络适配器 然后开机 编辑文件/etc/udev/rules.d/70-persistent-net.rules,进去之后是这个样子 把前两个删掉, ...

  10. React Native控件之Picker

    1. import React,{Component}from 'react'; import { AppRegistry, StyleSheet, Text, View, Picker, } fro ...