搜索专题训练hdu4403A very hard Aoshu problem
来源:互联网 发布:php 执行shell命令 编辑:程序博客网 时间:2024/04/30 06:15
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.
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.
1212123456661235END
220
题意很简单,通过对一串数字填上一个等号和任意个加号,使等式成立,符号两侧均必须有数字。
思路就是去按位选择等号的位置,对于等号两侧搜索所有可能,看两侧有多少相等的值。
处理过程中遇到一点问题,我们需要记录一侧得到的值在另一侧有没有出现过,如果一侧能够通过不同的方式得到多次同一个数字,我们需要记录数字出现次数的,对于两面出现相同的数字,总可能情况是两侧个数的乘积。
搜索的过程,等式两侧,对于每一个位置,有加入和不加入+号两个选择,对于加入+号的情况,直接将当前和加到部分和上,将下一位作为新的当前和,对于不加入+号的情况,将当前和*10并将下一位加到当前和。
#include<bits/stdc++.h>using namespace std;typedef long long LL;map<LL, int> mp[2];string s;void compute(int l, int r, int add, int sum, int pos){ if(l >= r) { add += sum; mp[pos][add]++; return; } compute(l+1, r, add+sum, s[l+1]-'0', pos); compute(l+1, r, add, sum*10 + s[l+1]-'0', pos);}int main(){ while(cin >> s && s != "END") { int len = s.length(); int ans = 0; for(int i = 0; i < len-1; ++i) { compute(0, i, 0, s[0]-'0', 0); compute(i+1, len-1, 0, s[i+1]-'0', 1); //cout << "ok" << endl; map<LL, int>::iterator p; for(p = mp[0].begin(); p != mp[0].end(); ++p) if(mp[1].count(p->first)) { ans += (p->second * mp[1][p->first]); //cout << p->first << endl; } mp[0].clear(); mp[1].clear(); } cout << ans << endl; }}
1 0
- 搜索专题训练hdu4403A very hard Aoshu problem
- hdu4403A very hard Aoshu problem
- hdu4403A very hard Aoshu problem
- hdu4403A very hard Aoshu problem 线段树
- A very hard Aoshu problem
- A very hard Aoshu problem
- hdu_4403 A very hard Aoshu problem 暴力搜索
- HDU 4403 A very hard Aoshu problem
- hdu 4403 A very hard Aoshu problem
- HDU:4403 A very hard Aoshu problem
- HDU 4403 A very hard Aoshu problem
- HDU 4403 A very hard Aoshu problem
- HDU 4403(A very hard Aoshu problem)
- HDU 4403 A very hard Aoshu problem
- A very hard Aoshu problem(HDU4402)
- hdu 4403 A very hard Aoshu problem
- hdu 4403 A very hard Aoshu problem
- HDU 4403A very hard Aoshu problem
- ubuntu: apt-get update的时候遇到“Hash Sum mismatch”错误
- u-boot详细解析
- 学术笔记·2015 IWA Alternative Water Resources Conference·十
- 题目1070:今年的第几天? 九度OJ
- 【机器学习 基本概念】隐马尔可夫模型
- 搜索专题训练hdu4403A very hard Aoshu problem
- 在Excel表格中,通过公式查询数据并在另外一个文档中保存
- 【SpringMVC+mybatis】2.mybatis框架原理
- WSAM_GPU
- 题目1186:打印日期 九度OJ
- linux的那些事
- C/C++外部指针访问类内部变量
- 计蒜客蓝桥杯模拟赛5 九宫格
- PAT1006. Sign In and Sign Out (25)