Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression.
An Ahmes arithmetic expression can be defined as:
- "d" is an Ahmes arithmetic expression, where d is a one-digit positive integer;
- "(E1 op E2)" is an Ahmes arithmetic expression, where E1 and E2 are valid Ahmes arithmetic expressions (without spaces) and op is either plus ( + ) or minus ( - ).
On his trip to Egypt, Fafa found a piece of papyrus paper having one of these Ahmes arithmetic expressions written on it. Being very ancient, the papyrus piece was very worn out. As a result, all the operators were erased, keeping only the numbers and the brackets. Since Fafa loves mathematics, he decided to challenge himself with the following task:
Given the number of plus and minus operators in the original expression, find out the maximum possible value for the expression on the papyrus paper after putting the plus and minus operators in the place of the original erased operators.
The first line contains a string E (1 ≤ |E| ≤ 104) — a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'.
The second line contains two space-separated integers P and M (0 ≤ min(P, M) ≤ 100) — the number of plus and minus operators, respectively.
It is guaranteed that P + M = the number of erased operators.
Print one line containing the answer to the problem.
(1?1) 1 0
2
(2?(1?2)) 1 1
1
((1?(5?7))?((6?2)?7)) 3 2
18
((1?(5?7))?((6?2)?7)) 2 3
16
- The first sample will be (1 + 1) = 2.
- The second sample will be (2 + (1 - 2)) = 1.
- The third sample will be ((1 - (5 - 7)) + ((6 + 2) + 7)) = 18.
- The fourth sample will be ((1 + (5 + 7)) - ((6 - 2) - 7)) = 16.
题意:给出一个算式,由括号、小于10的正整数和问号组成,问号是算式中的符号,现给出原式中加号的个数p和减号的个数m,对于所有填放方式对应的结果,求最大值。
解题思路:可以把这个算式化成一个二叉树,叶子节点是一个数,非叶子节点表示一个符号,当一个非叶子结点填加号时,就需要最大化左右子树的值,当一个非叶子结点填减号时,就需要最大化左子树值,最小化右子树值。树形dp可以解决这个问题,可以用dp[i][j][k][0/1]示第i个点及其子树中填了j个加号,k个负号所能得到的最大 / 最小值,但是这样开数组开不下,但其实加号和减号数量可以互相转换,所以去掉加号或减号中的一维即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int dp[2][10010][110];
char ch[10010];
int son[10010][2], cnt, fa[10010], p, m, pre, mi;
void dfs(int x)
{
if (!son[x][0]) return;
int l = son[x][0], r = son[x][1];
dfs(l), dfs(r);
for (int i = 0; i <= mi; i++)
{
for (int j = 0; i + j <= mi; j++)
{
dp[0][x][i + j + (p < m)] = max(dp[0][x][i + j + (p < m)], dp[0][l][i] + dp[0][r][j]);
dp[0][x][i + j + (p >= m)] = max(dp[0][x][i + j + (p >= m)], dp[0][l][i] - dp[1][r][j]);
dp[1][x][i + j + (p < m)] = min(dp[1][x][i + j + (p < m)], dp[1][l][i] + dp[1][r][j]);
dp[1][x][i + j + (p >= m)] = min(dp[1][x][i + j + (p >= m)], dp[1][l][i] - dp[0][r][j]);
}
}
}
int main()
{
while (~scanf("%s", ch))
{
scanf("%d%d", &p, &m);
mi = min(p, m);
int len = strlen(ch);
cnt = pre = 1;
son[1][0] = son[1][1] = 0;
memset(dp[0], -INF, sizeof dp[0]);
memset(dp[1], INF, sizeof dp[1]);
for (int i = 0; i < len; i++)
{
if (ch[i] == '(' || ch[i] == '?')
{
son[pre][son[pre][0] ? 1 : 0] = ++cnt;
son[cnt][0] = son[cnt][1] = 0;
fa[cnt] = pre;
pre = cnt;
}
else if (ch[i] == ')') pre = fa[pre];
else
{
dp[0][cnt][0] = dp[1][cnt][0] = ch[i] - '0';
pre = fa[pre];
}
}
dfs(1);
printf("%d", dp[0][1][mi]);
}
return 0;
}