您的位置:首页 > 其它

nyoj305-表达式求值(栈。。dfs)

2017-11-25 20:49 471 查看

题目来源: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'<
4000
/span>;
return x;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
i=0;
printf("%d\n",dfs());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: