A very hard Aoshu problem

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 just comes out with a problem to test his students:

Given a serial of digits, you must put a '=' and none or some '+' between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2". Please note that the digits only include 1 to 9, and every '+' must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations.

 
Input
There are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END".
 
Output
For each test case , output a integer in a line, indicating the number of equations you can get.
 
Sample Input
1212
12345666
1235
END
 
Sample Output
2
2
0
 
Answer
先预处理出第i位到第j位的数字是什么(sum数组),然后枚举插入等号的位置(两端不能插入等号),接下来dfs枚举等号左边的情况,每一种情况结束之后,继续dfs枚举等号右边的情况,同时将左边的情况(和)传递过去,等号右边每一种情况结束的时候,对比两个和,如果相等则答案加一。另外,枚举右边的情况的时候,可以加一个剪枝(不加也没事)。
 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
#define ms(a) memset(a,0,sizeof a)
using namespace std;
int sum[][];
vector<int> v;
string s;
int ans;
//处理等号右边
void dfs2(int lsu,int su,int l)//等号左边的和,和,起点
{
if(lsu<su)return;
if(l==(int)s.size()&&lsu==su)
{
ans++;
return;
}
for(int i=l+; i<=(int)s.size(); i++)
{
dfs2(lsu,su+sum[l][i-],i);
}
}
//处理等号左边
void dfs1(int su,int l,int r)//和,左,右界
{
if(l==r)
{
dfs2(su,,r);
return;
}
for(int i=l+; i<=r; i++)
{
dfs1(su+sum[l][i-],i,r);
}
}
int main()
{
string::iterator it1,it2;
while(cin>>s)
{
if(s=="END")break;
//ms(sum);
ans=;
if(s.size()==)printf("0\n");
else
{
//预处理,得到某个区间的数值
for(it1=s.begin(); it1!=s.end(); it1++)
{
int t=*it1-'';
sum[it1-s.begin()][it1-s.begin()]=t;
for(it2=it1+; it2!=s.end(); it2++)
{
t=t*+(*it2-'');
sum[it1-s.begin()][it2-s.begin()]=t;
}
}
//1=234566=6,等号的位置
for(it1=s.begin()+; it1!=s.end(); it1++)
{
dfs1(,,it1-s.begin());
}
printf("%d\n",ans);
}
}
return ;
}

HDU 4403 A very hard Aoshu problem(DFS)的更多相关文章

  1. HDU 4403 A very hard Aoshu problem(dfs爆搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...

  2. HDU 4403 A very hard Aoshu problem (DFS暴力)

    题意:给你一个数字字符串.问在字符串中间加'='.'+'使得'='左右两边相等. 1212  : 1+2=1+2,   12=12. 12345666 : 12+3+45+6=66.  1+2+3+4 ...

  3. HDU 4403 A very hard Aoshu problem

    暴力$dfs$. 先看数据范围,字符串最长只有$15$,也就是说枚举每个字符后面是否放置“$+$”号的复杂度为${2^{15}}$. 每次枚举到一种情况,看哪些位置能放“$=$”号,每个位置都试一下, ...

  4. A very hard Aoshu problem(dfs或者数位)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 A very hard Aoshu problem Time Limit: 2000/1000 ...

  5. HDU4403 A very hard Aoshu problem DFS

    A very hard Aoshu problem                           Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  6. 【HDOJ】4403 A very hard Aoshu problem

    HASH+暴力. /* 4403 */ #include <iostream> #include <cstdio> #include <cstring> #incl ...

  7. hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0

    Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...

  8. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)

    Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. N ...

随机推荐

  1. c#后台输出javascript语句和一些通用验证的类

    大家在用MVC的时候,经常会用到MODEL层的验证或者是正则表达式,我这边看到了一篇不错的文章,转载过来http://blog.csdn.net/accpxcb/article/details/311 ...

  2. 看精通SQL SERVER2008有感1

    SQLserver数据库中的其他数据库作用: Master:存储SQLserver所有的全局配置,也就是存储SQLserver所知道的关于自己的全部信息,包括自身的配置,和当前的状态,这些数据存储在系 ...

  3. NoSql数据库使用

    NoSql数据库使用半年后在设计上面的一些心得 NoSql数据库这个概念听闻许久了,也陆续看到很多公司和产品都在使用,优缺点似乎都被分析的清清楚楚.但我心里一直存有一个疑惑,它的出现究竟是为了解决什么 ...

  4. 怎么让猫吃辣椒 转载自 xiaotie

    典故: 某日,毛.周.刘三人聊天. 毛:怎么能让猫自愿吃辣椒? 刘:掐着脖子灌. 毛:强迫不是自愿. 周: 先饿几天,再混到猫爱吃的东西里. 毛:欺骗不是自愿.把辣椒涂到猫肛门上,它就会自己去舔了. ...

  5. Stimulsoft.Report.web viewer控件添加按钮

    当你购买了带源码的时候,你可以对源码进行修改以达到自己想要的效果,比较这里讲到的,向viewer控件工具栏添加按钮. 通过源码目录可以看出我们需要修改的有两部分代码 红色方块圈中的部分,[StiWeb ...

  6. win32多线程-异步过程调用(asynchronous Procedure Calls, APCs)

    使用overlapped I/O并搭配event对象-----win32多线程-异步(asynchronous) I/O事例,会产生两个基础性问题. 第一个问题是,使用WaitForMultipleO ...

  7. [转]How to create an anonymous IDA PRO database (.IDB)

    Source: http://www.0xebfe.net/blog/2013/01/13/how-to-create-an-anonymous-ida-pro-database-dot-idb/ P ...

  8. [转]Apple iPod, iPhone (2g, 3g), iPad Dock connector pinout

    Pin Signal Description Apple pin numbering* 1 GND Ground (-), internally connected with Pin 2 on iPo ...

  9. jquery扩展方法

    jquery插件的开发包括两种:一种是类级别的插件开发,即给jquery添加新的全局函数,相当于给jquery类本身添加方法. jquery的全局函数就是属于jquery命名空间的函数,另一种是对象级 ...

  10. EasyUI tree扩展获取实心节点

    <script type="text/javascript"> //扩展 获得tree 的实心节点 $(function(){ $.extend($.fn.tree.m ...