A very hard Aoshu problem
2015-10-24 18:49
211 查看
D - A very hard Aoshu problem
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
4403
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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
#define N 20
char s
;
int a
, len;
map<int, int>ml, mr;
void dfs(int pos, int x, int v, int res, map<int, int> &m)
{
if(pos == x)
{
int val = res + v * 10 + a[pos];
m[val]++;
return ;
}
dfs(pos + 1, x, 0, res + v * 10 + a[pos], m);
dfs(pos + 1, x, v * 10 + a[pos], res, m);
}
int solve(int x)
{
int res = 0;
ml.clear();
mr.clear();
dfs(0, x, 0, 0, ml);
dfs(x + 1, len - 1, 0, 0, mr);
for(map<int, int>::iterator it = ml.begin(); it != ml.end(); it++)
res += (it->second) * mr[(it->first)];
return res;
}
int main()
{
while(~scanf("%s", s) && s[0] != 'E')
{
int ans = 0;
len = strlen(s);
for(int i = 0; i < len; i++) a[i] = s[i] - '0';
for(int i = 0; i < len - 1; i++)
ans += solve(i);
printf("%d\n", ans);
}
return 0;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
4403
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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
#define N 20
char s
;
int a
, len;
map<int, int>ml, mr;
void dfs(int pos, int x, int v, int res, map<int, int> &m)
{
if(pos == x)
{
int val = res + v * 10 + a[pos];
m[val]++;
return ;
}
dfs(pos + 1, x, 0, res + v * 10 + a[pos], m);
dfs(pos + 1, x, v * 10 + a[pos], res, m);
}
int solve(int x)
{
int res = 0;
ml.clear();
mr.clear();
dfs(0, x, 0, 0, ml);
dfs(x + 1, len - 1, 0, 0, mr);
for(map<int, int>::iterator it = ml.begin(); it != ml.end(); it++)
res += (it->second) * mr[(it->first)];
return res;
}
int main()
{
while(~scanf("%s", s) && s[0] != 'E')
{
int ans = 0;
len = strlen(s);
for(int i = 0; i < len; i++) a[i] = s[i] - '0';
for(int i = 0; i < len - 1; i++)
ans += solve(i);
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points