您的位置:首页 > 其它

搜索专题训练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
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: