杭电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 ,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

Author

dandelion

Source

HDU
8th Programming Contest Online

Recommend

lcy

AC代码:

 1 #include<cstring>
 2 #include<cstdio>
 3 using namespace std;
 4 #define LL long long
 5 LL num,n,len;
 6 char str[15];
 7 void DFS(LL x, LL sum)
 8 {//x是取的字符串的长度, sum是当前的值
 9     if(x == len)//当取得字符串长度==字符串总串长度时
10     {
11         if(sum==n)//当前结果== n时
12             num++;
13         return ;
14     }
15     LL k=0;//核心的东东  短短的几句  却是指数级的搜索量啊
16     for(int i=x;i<len;i++)//这里刚开始写成了从0开始,哎合适能搜到头啊
17     {
18         k=k*10+str[i]-‘0‘;
19
20         //加减两种可能
21         DFS(i+1, sum+k);
22         if(x != 0)//第一个数字前不能加“-”号
23             DFS(i+1, sum-k);
24     }
25 }
26 int main()
27 {
28     while(scanf("%s %lld",str,&n)!=EOF)
29     {
30         len=strlen(str);
31         num = 0;
32         DFS(0,0);
33         printf("%lld\n", num);
34     }
35     return 0;
36 }
时间: 2024-10-29 20:01:38

杭电2266 How Many Equations Can You Find【DFS】的相关文章

杭电 HDU ACM 1496 Equations

Equations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6065    Accepted Submission(s): 2455 Problem Description Consider equations having the following form: a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 a,

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 char

杭电2181--哈密顿绕行世界问题(Dfs)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=2181 好像很久没有刷题了, 真的是手生到不要不要的, 这道应该很明显就是Dfs, 然而我第一眼看上去竟然没有什么感觉, 还是看题解解出来的. 看来以后每天要至少A道题保持手感了. //这个比较好理解, 算是一种比较常规的写法了: #include <cstdio> #include <cstring> #include <iostream> using namespace s

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

一个人的旅行 HDU杭电2066【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段时间,可不

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b

杭电1276--士兵队列训练问题

士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4605    Accepted Submission(s): 2148 Problem Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行