搜索专题训练hdu4403A very hard Aoshu problem
2017-04-01 21:53
369 查看
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.
InputThere 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".
OutputFor each test case , output a integer in a line, indicating the number of equations you can get.
Sample Input
Sample Output
题意很简单,通过对一串数字填上一个等号和任意个加号,使等式成立,符号两侧均必须有数字。
思路就是去按位选择等号的位置,对于等号两侧搜索所有可能,看两侧有多少相等的值。
处理过程中遇到一点问题,我们需要记录一侧得到的值在另一侧有没有出现过,如果一侧能够通过不同的方式得到多次同一个数字,我们需要记录数字出现次数的,对于两面出现相同的数字,总可能情况是两侧个数的乘积。
搜索的过程,等式两侧,对于每一个位置,有加入和不加入+号两个选择,对于加入+号的情况,直接将当前和加到部分和上,将下一位作为新的当前和,对于不加入+号的情况,将当前和*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;
}
comput
4000
e(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;
}
}
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.
InputThere 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".
OutputFor 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
题意很简单,通过对一串数字填上一个等号和任意个加号,使等式成立,符号两侧均必须有数字。
思路就是去按位选择等号的位置,对于等号两侧搜索所有可能,看两侧有多少相等的值。
处理过程中遇到一点问题,我们需要记录一侧得到的值在另一侧有没有出现过,如果一侧能够通过不同的方式得到多次同一个数字,我们需要记录数字出现次数的,对于两面出现相同的数字,总可能情况是两侧个数的乘积。
搜索的过程,等式两侧,对于每一个位置,有加入和不加入+号两个选择,对于加入+号的情况,直接将当前和加到部分和上,将下一位作为新的当前和,对于不加入+号的情况,将当前和*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;
}
comput
4000
e(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;
}
}
相关文章推荐
- CDOJ 搜索专题训练A
- CDOJ 训练搜索专题G 八数码固定终点问题
- 搜索专题训练hdu1175连连看
- 搜索专题训练hdu2510符号三角形
- HDU上的专题训练(背包问题-线段树+树状数组+DP优化+网络流+字符匹配+最短路+矩阵乘法+搜索专题++)
- 搜索专题训练hdu1455 Sticks
- 搜索专题训练hdu1043Eight
- 暑假训练1-搜索专题
- 搜索专题训练(2)
- 算法专题训练 搜索a-T3 Ni骑士(ni)
- DFS-BFS搜索专题【经典训练题】【有时间一个个做下来】
- 搜索专题训练【CSUST_Newer_12级入门】
- DFS-BFS搜索专题【经典训练题】【有时间一个个做下来】
- 搜索专题训练【CSUST_Newer_12级入门】
- [kuangbin带你飞]专题1 简单搜索 E - Find The Multiple POJ - 1426
- 搜索专题 O
- (复习)基础算法--搜索--深入训练(USACO-Feb08、WOW模拟赛Day2-T4、USACO-Dec13、CTSC-1999)
- 数学专题训练4
- 搜索专题 A
- 元旦图论搜索训练的总结