题目描述
A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to escape the castle.
There are N rooms from the place where he was imprisoned to the exit of the castle. In the ith room, there is a wizard who has a resentment value of a[i]. The prince has M curses, the jth curse is f[j], and f[j] represents one of the four arithmetic operations, namely addition(’+’), subtraction(’-’), multiplication(’*’), and integer division (’ / ‘). The prince’s initial resentment value is K. Entering a room and fighting with the wizard will eliminate a curse, but the prince’s resentment value will become the result of the arithmetic operation f[j] with the wizard’s resentment value. That is, if the prince eliminates the jth curse in the ithroom, then his resentment value will change from x to (x f[j] a[i]), for example, when x=1, a[i]=2, f[j]=’+’, then x will become 1+2=3.
Before the prince escapes from the castle, he must eliminate all the curses. He must go from a[1] to a[N] in order and cannot turn back. He must also eliminate the f[1] to f[M] curses in order(It is guaranteed that N≥M). What is the maximum resentment value that the prince may have when he leaves the castle?
输入
The first line contains an integer T(1≤T≤1000), which is the number of test cases.
For each test case, the first line contains three non-zero integers: N(1≤N≤1000), M(1≤M≤5) and K(-1000≤K≤1000), the second line contains N non-zero integers: a[1], a[2], … , a[N] (-1000≤a[i]≤1000), and the third line contains M characters: f[1], f[2], … , f[M] (f[j] 〖= 〗^’ +’,’-’,’*’,’ / '), with no spaces in between.
输出
For each test case, output one line containing a single integer.
样例输入
3
2 1 5
2 3
/
3 2 1
1 2 3
++
4 4 5
1 2 3 4
±*/
样例输出
2
6
3
思路
dp推最大值即可,注意乘除操作会将负值转正,因此需要两个数组储存最大值最小值
代码实现
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1005;
const int mod=1e9+7;
const ll INF=(ll)1e18;
int T;
int n,m;
char op[10];
ll dpa[N][10],dpi[N][10],a[N],k;
inline ll cal(ll a,char c,ll b)
{
if(c=='+') return (a+b);
if(c=='-') return (a-b);
if(c=='*') return (a*b);
if(c=='/') return (a/b);
}
inline ll getmax(ll a,ll b)
{
if(a>b) return a;
return b;
}
inline ll getmin(ll a,ll b)
{
if(a<b) return a;
return b;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d%lld",&n,&m,&k);
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
scanf("%s",op+1);
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
dpa[i][j]=-INF;
dpi[i][j]=INF;
}
}
for(int i=0;i<=n;i++)
{
dpa[i][0]=dpi[i][0]=k;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m && j<=i;j++)
{
dpa[i][j]=getmax(dpa[i-1][j],dpa[i][j]);
dpi[i][j]=getmin(dpi[i-1][j],dpi[i][j]);
ll x=cal(dpa[i-1][j-1],op[j],a[i]);
ll y=cal(dpi[i-1][j-1],op[j],a[i]);
dpa[i][j]=getmax(dpa[i][j],getmax(x,y));
dpi[i][j]=getmin(dpi[i][j],getmin(x,y));
}
}
printf("%lld\n",getmax(dpa[n][m],dpi[n][m]));
}
return 0;
}