本文主要是介绍【JZOJ4840】小W砍大树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Description
给出一些括号(匹配)和T/F,奇数层括号里的运算符为and,偶数层为or,计算结果。
Solution
开一个栈存储即可。
Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
#define N 33000
using namespace std;
char s[N];
int a[N];
int main()
{freopen("form.in","r",stdin);freopen("form.out","w",stdout);int num=0;while(scanf("%s",s+1)!=EOF){num++;int top=0,l=strlen(s+1);int t=2;fo(i,1,l)if(s[i]=='(') a[++top]=t,t=-t;else if(s[i]==')'){a[top-1]=a[top];t=-t;top--;int q=-t;if(a[top-1]!=2 && a[top-1]!=-2 && top>1){if(q==2) a[top-1]=a[top-1]&a[top];else a[top-1]=a[top-1]|a[top];top--;}}else{int p=(s[i]=='T'),q=-t;if(a[top]!=0 && a[top]!=1) a[++top]=p;else{if(q==2) a[top]=a[top]&p;else a[top]=a[top]|p;}}if(a[top]) printf("%d. true\n",num);else printf("%d. false\n",num);}
}
这篇关于【JZOJ4840】小W砍大树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!