题目来源:http://acm./problem.php?pid=305
题意
求表达式的值。
思路
利用深搜回溯的性质,模拟栈,,,
代码
#include<cmath>
#include<stack>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
int i;
char str[1000+10];
int dfs()
{
int x=0,a,b;
while(str[i]==','||str[i]==')')
i++;
if(str[i+1]=='d')
{
i+=4;
return dfs()+dfs();
}
if(str[i+1]=='a')
{
i+=4;
a=dfs();
b=dfs();
return a>b?a:b;
}
if(str[i+1]=='i')
{
i+=4;
a=dfs();
b=dfs();
return a<b?a:b;
}
while(str[i]>='0'&&str[i]<='9')
x=x*10+str[i++]-'0';
return x;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
i=0;
printf("%d\n",dfs());
}
}