关于代码能力

代码能力是很重要的,像我,稍微复杂一点的题就做得想吐。

写了个200line+的代码,就已经写的想吐了。重新构思敲一遍得了。

最讨厌的就是无力感啊!

#include
#include
#define MN 555555
#define l Recnt&1
#define r (Recnt+1)&1
using namespace std;

struct Node{ int next[2],val,id; }deque[MN<<1],stL[MN<<1],stR[MN<<1];
int Recnt,topL,topR;

int N,M,L,R;

void init()
{
 	 topL=topR=Recnt=0;
 	 for( int i=1;i<=N;i++ )
 	 {
	  	  scanf( "%d",&deque[i].val );
	  	  deque[i].next[0]=i-1;
	  	  deque[i].next[1]=i+1;
	  	  deque[i].id=i;
	 }
	 deque[0].next[1]=1;
	 deque[0].next[0]=-1;
	 deque[0].id=0;
	 deque[N+1].next[1]=-1;
	 deque[N+1].next[0]=N;
	 deque[0].id=N+1;
}

void print()
{
 	 bool f=true;
 	 for( int i=0;i=0;i-- )
  	 {
	  	  if( f ) printf( "%d",stR[i].val );
  	  	  else printf( " %d",stR[i].val );
  	  	  f=false;
  	 }
	 printf( "\n" );
}

void solve()
{
 	 char str[111];
 	 int val;
 	 scanf( "%d %d %d",&L,&R,&M );
 	 for( int i=1;iR;i-- )
 	 	  stR[topR++]=deque[i];
 	 getchar();
 	 int NN=N+2;
 	 while( M-- )
 	 {
	  		gets(str);
	  		if( !strcmp(str,"MoveLeft R") )
	  		{
			 	if( Recnt&1 )
			 	{
				 	stR[topR].id=L;
				 	stR[topR++]=deque[L];
				 	L=deque[L].next[l];
	 			}
	 			else
	 			{
				 	stR[topR].id=R;
			 	 	stR[topR++]=deque[R];
			 		R=deque[R].next[l];
				}
		 	}
		 	else if( !strcmp(str,"MoveLeft L") )
		 	{
			 	 deque[L].next[l]=stL[topL-1].id;
			 	 stL[topL-1].next[r]=L;
			 	 topL--;
			 	 L=deque[L].next[l];
	 	 	}
	 	 	else if( !strcmp(str,"MoveRight R") )
	 	 	{
			 	  	 deque[R].next[r]=stR[topR-1].id;
			 	 	 stR[topR-1].next[l]=R;
			 	 	 topR--;
			 	 	 R=deque[R].next[r];
 	 	 	}
 	 	 	else if( !strcmp(str,"MoveRight L") )
 	 	 	{
			 	 if( Recnt&1 )
			 	 {
				  	 stL[topL].id=R;
				  	 stL[topL++]=deque[R];
				  	 R=deque[R].next[r];
 	 		 	 }
 	 		 	 else
 	 		 	 {
			 	  	 stL[topL].id=L;
			 	 	 stL[topL++]=deque[L];
			 	 	 L=deque[L].next[r];
				 }
 	 	 	}
 	 	 	else if( sscanf(str,"Insert L %d",&val) )
 	 	 	{
			 	 if( Recnt&1 )
			 	 { 	 
 	 		 	 	 deque[NN].val=val;
			 	 	 deque[NN].next[l]=R;
			 	 	 deque[NN].next[r]=deque[R].next[r];
			 	 	 deque[deque[R].next[r]].next[l]=NN;
			 	 	 deque[R].next[r]=NN++;
			 	 	 //R=deque[R].next[r];
 	 		 	 }
 	 		 	 else
 	 		 	 {
			 	  	 deque[NN].val=val;
			 	 	 deque[NN].next[l]=deque[L].next[l];
			 	 	 deque[NN].next[r]=L;
			 	 	 deque[deque[L].next[l]].next[r]=NN;
			 	 	 deque[L].next[l]=NN++;
			 	 	 L=deque[L].next[l];
				 }
 	 	 	}
 	 	 	else if( sscanf(str,"Insert R %d",&val) )
 	 	 	{
			 	 if( Recnt&1 )
			 	 {
				  	 deque[NN].val=val;
			 	 	 deque[NN].next[l]=deque[L].next[l];
			 	 	 deque[NN].next[r]=L;
			 	 	 deque[deque[L].next[l]].next[r]=NN;
			 	 	 deque[L].next[l]=NN++;
			 	 	 L=deque[L].next[l];
	 		 	 }
	 		 	 else
	 		 	 {
			 	  	 deque[NN].val=val;
			 	 	 deque[NN].next[l]=R;
			 	 	 deque[NN].next[r]=deque[R].next[r];
			 	 	 deque[deque[R].next[r]].next[l]=NN;
			 	 	 deque[R].next[r]=NN++;
			 	 	 R=deque[R].next[r];
				 }
 	 	 	}
 	 	 	else if( !strcmp(str,"Delete L") )
 	 	 	{
			 	 if( Recnt&1 )
			 	 {
			 	  	 deque[deque[R].next[l]].next[r]=deque[R].next[r];
			 	 	 deque[deque[R].next[r]].next[l]=deque[R].next[l];
			 	 	 R=deque[R].next[r];
				 }
				 else
				 {
				  	 deque[deque[L].next[l]].next[r]=deque[L].next[r];
			 	 	 deque[deque[L].next[r]].next[l]=deque[L].next[l];
			 	 	 L=deque[L].next[r];
				 }
 	 	 	}
 	 	 	else if( !strcmp(str,"Delete R") )
 	 	 	{
			 	 if( Recnt&1 )
			 	 {
			 	  	 deque[deque[L].next[l]].next[r]=deque[L].next[r];
			 	 	 deque[deque[L].next[r]].next[l]=deque[L].next[l];
					 L=deque[L].next[l];
				 }
				 else
				 {
			 	  	 deque[deque[R].next[l]].next[r]=deque[R].next[r];
			 	 	 deque[deque[R].next[r]].next[l]=deque[R].next[l];
					 R=deque[R].next[l];
				 }
	 	 	}
 	 	 	else if( !strcmp(str,"Reverse") )
 	 	 	{
			 	 Recnt^=1;
 	 	 	}
 	 	 	//print();
	 }
}

int main()
{
 	freopen( "test.in","r",stdin );
 	freopen( "test.out","w",stdout );
 	int T;
 	scanf( "%d",&T );
 	while( T-- )
 	{
	 	scanf( "%d",&N );
		init();
		solve();
		print();
  	}
 	return 0;
}


你可能感兴趣的:(碎碎念,insert,delete)