min(100,65)TLE求调
查看原帖
min(100,65)TLE求调
569235
w9095楼主2022/11/7 07:54
#include <bits/stdc++.h>
using namespace std;
char s[1000020];
int st1[1000020],st2[1000020],dor[1000020],dand[1000020],ansor=0,ansand=0,ans=0,p1=0,p2=0;
int pushin(int a)
{
	while(p2>0&&st2[p2-1]!=4&&a<=st2[p2-1])
	      st1[p1++]=st2[--p2];
	st2[p2++]=a;
	return 0;
}

int popout()
{
	while(p2>0&&st2[p2-1]!=4)
	    if(st2[p2-1]==4)break;
	    else st1[p1++]=st2[--p2];
	if(p2>0)st2[p2--]=0;
	return 0;
}

int orvalue(int i)
{
	register int op1=-1,op2=-1,opx,opy;
	for(register int j=i;j>=0;j--)
	    if(st1[j]==1||st1[j]==0)
	       if(op1==-1)op1=st1[j], opx=j,st1[j]=-1;
	       else
		    { 
		    op2=st1[j];
		    opy=j;
		    st1[j]=-1;
		    break;
		    }
	if(op2==1)
	   {
	   	dor[i]++;
	   	dor[opx]=0;
	   	dand[opx]=0;
	   }
	else
	   {
	   	dor[i]+=dor[opx];
	    dor[opx]=0;
	    dand[i]+=dand[opx];
	    dand[opx]=0;
	   }
	st1[i]=op1||op2;
	dor[i]+=dor[opy];
	dor[opy]=0;
	dand[i]+=dand[opy];
	dand[opy]=0;
	return 0;       
}

int andvalue(int i)
{
	register int op1=-1,op2=-1,opx,opy;
	for(register int j=i;j>=0;j--)
	    if(st1[j]==1||st1[j]==0)
	       if(op1==-1)op1=st1[j], opx=j,st1[j]=-1;
	       else
		    { 
		    op2=st1[j];
		    opy=j;
		    st1[j]=-1;
		    break;
		    }
	if(op2==0)
	   {
	   	dand[i]++;
	   	dor[opx]=0;
	   	dand[opx]=0;
	   }
	else
	   {
	   	dor[i]+=dor[opx];
	    dor[opx]=0;
	    dand[i]+=dand[opx];
	    dand[opx]=0;
	   }
	st1[i]=op1&&op2;
	dor[i]+=dor[opy];
	dor[opy]=0;
	dand[i]+=dand[opy];
	dand[opy]=0;
	return 0;       
}
 
int main()	
{
	scanf("%s",s);
	int l=strlen(s);
	for(register int i=0;i<l;i++)
	    {
	    	switch(s[i])
	    	   {
	    	   	case '0':st1[p1++]=0;break;
	    	   	case '1':st1[p1++]=1;break;
	    	   	case '&':pushin(3);break;
	    	   	case '|':pushin(2);break;
	    	   	case '(':st2[p2++]=4;break;
	    	   	case ')':popout();break;
			   }
 		}
 	popout();
 	for(register int i=0;i<p1;i++)
 	    {
 	        switch(st1[i])
			   {
				case 2:orvalue(i);break;
				case 3:andvalue(i);break;	
			   }
		}
	printf("%d\n",st1[p1-1]);
	printf("%d %d",dand[p1-1],dor[p1-1]);
	return 0;
}

可能有一点点长,思路是先转后缀表达式,然后求值。

似乎是求值方面慢了。

2022/11/7 07:54
加载中...