Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

讨论中的数据都过了 还是RE 求特殊数据!!!!!!!!!!!!!!!

Posted by NYQCBHH at 2017-09-10 10:00:20 on Problem 2106
#include<cstdio>
#include<iostream>
#include<stack>
#include<string>

using namespace std;

struct Pri{
	char op;
	int pri;
}lpri[5]={{'(',1},{'|',3},{'&',5},{'!',6},{')',8}},
 rpri[5]={{'(',8},{'|',2},{'&',4},{'!',7},{')',1}};
 
 int find_left(char op)
 {
 	for(int i=0;i<5;i++)
 		if(lpri[i].op==op)
 			return lpri[i].pri;
 }
 
 int find_right(char op)
 {
 	for(int i=0;i<5;i++)
 		if(rpri[i].op==op)
 			return rpri[i].pri;
 }
 
int Precede(char op1,char op2)
{
	int L=find_left(op1);
	int R=find_right(op2);
	if(L==R)	
		return 0;
	if(L>R)
		return 1;
	return -1; 	
} 

int Run(stack<int>& num,char op)
{
	int a=num.top();
	num.pop();
	
	if(op=='!')
		return !a;
		
//	cout<<"a="<<a;
	int b=num.top();
	num.pop();
	//	cout<<" b="<<b;
//	cout<<" op="<<op<<endl;
	switch(op)
	{
		case '|':
			return a||b;
		case '&':
			return a&&b;
	}
}

int main()
{
	string s;
	int ca=1;
	while(getline(cin,s))
	{
		int i;
		stack<int> num;
		stack<char> op;
 		for(i=0;i<s.length();i++)
		{
		//	cout<<"i="<<i<<endl;
			if(s[i]==' ')
			{
			//	cout<<"char is space"<<endl;
				continue;
			}	
			else if(s[i]=='V')
			{
			//	cout<<"op is "<<s[i]<<endl;
				num.push(1);
			}	
			else if(s[i]=='F')
			{
			//	cout<<"op is "<<s[i]<<endl;
				num.push(0);
			}
			else
			{
				if(op.empty())
				{
					op.push(s[i]);
				//	cout<<"space->"<<s[i]<<endl;	
				}
				else
				{
					int judg=Precede(op.top(),s[i]);
				//	cout<<"judg="<<judg<<" op is "<<s[i]<<endl;
					int t;
					switch(judg)
					{
						case 0:
					//		cout<<"put out "<<op.top()<<endl;
							op.pop();break;
						case 1:
							t=Run(num,op.top());
					//		cout<<"Run(num,op.top())="<<t<<endl;
							num.push(t);
							op.pop();
							i--;break;
						case -1:
					//		cout<<"get in "<<s[i]<<endl;
							op.push(s[i]);break;
					}
				}
			}
		}
		while(!op.empty())
		{
			num.push(Run(num,op.top()));
				op.pop();
		}
		if(num.top()==1)
			printf("Expression %d: V\n",ca++);
		else
			printf("Expression %d: F\n",ca++);
	}
	
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator