noip2017 DAY1- T2

模拟的题:https://www.luogu.org/problemnew/show/P3952

别忘了栈!!!

调试时出的问题:

1.忘了删freopen(回坑的疏忽)

2.很多地方是数字字符串结合输入。。。判断误认为数字为一位数

3.栈没有彻底清空

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>

using namespace std;

struct node
{
	char i;
	int n;//n的指数 
};

int main()
{
	int a,b,i,t,line,tr[27],j,k,err,rans,ans,zs,max1,begin1,end1;
	char s[500],s1[5],s2[5],begin[50],end[50];
	stack <node> st;
	stack <int> st1;
	node n1,n2;
	
	freopen("a.txt","r",stdin);
	scanf("%d",&t);//程序数
	
	for (i=0;i<t;i++)
	{
		rans=0;
		scanf("%d %s",&line,&s);
		if (s[2]=='1')
			rans=0;		//right answer
		else
			for (j=4;j<strlen(s)-1;j++)
				rans=rans*10+s[j]-'0';
			
		memset(tr,0,sizeof(tr));	//初始化 
		err=0;	ans=0;
		st1.push(0);
		for (j=0;j<line;j++)
		{
			scanf("%s",&s1);
			if (s1[0]=='F')
			{
				scanf("%s%s%s",&s2,&begin,&end);
				if (err==1)		//已经错误 
					continue;
				
				begin1=0;	end1=0;
				if (begin[0]!='n')
					for (k=0;k<strlen(begin);k++)
						begin1=begin1*10+begin[k]-'0';
				if (end[0]!='n')
					for (k=0;k<strlen(end);k++)
						end1=end1*10+end[k]-'0';
					
				if (begin[0]!='n' && end[0]=='n')
					zs=1;//n or 常数 
				else if ((begin[0]=='n' && end[0]!='n')||(begin1>end1))
					zs=-1;
				else
					zs=0;
				
				if (tr[s2[0]-'a']==0)		//判重 
				{
					tr[s2[0]-'a']=1;
					n1.i=s2[0];		n1.n=zs;
					st.push(n1);	st1.push(0);
				}
				else
					err=1;			
			}
			else if (s1[0]=='E')
			{
				if (err==1)
					continue;
					
				if (st.empty())		//E!=F
				{
					err=1;
					continue;
				}
				n2=st.top();	max1=st1.top();
				st.pop();		st1.pop();
				tr[n2.i-'a']=0;
					
				if (n2.n==-1)			
					continue;
				
				ans=max(st1.top(),max1+n2.n);
				st1.pop();
				st1.push(ans);
			}
		}
		if (err==1)
		{
			printf("ERR\n");
			while (!st.empty())
				st.pop();
			while (!st1.empty())
				st1.pop();
//			printf("%d\n",st1.size());
			continue;
		}
		if (!st.empty())
		{
			printf("ERR\n");
			while (!st.empty())
				st.pop();
			while (!st1.empty())
				st1.pop();
			continue;
		}
		if (st1.top()==rans)
			printf("Yes\n");
		else
		{
//			printf("%d ",st1.top());
			printf("No\n");
		}
		st1.pop();
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/scutbenson/article/details/81286352