Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 689 Accepted Submission(s): 159
/* HDU G++ 4015ms 11968K 双向链表模拟 注意细节 */ #include<stdio.h> #include<string.h> #include<algorithm> #include<algorithm> using namespace std; const int MAXN=2000000; struct Node { int from; int to; int val; }node[MAXN]; int tol; int L,R;//两个指针 int LL,RR;//分别是指针L,R的左侧和右侧,由于倒一下可能改变方向,所以记录下方向 int n; void moveleft_L(void)//左移L指针 { if(L==0)return;//最左侧了 if(node[L].to==LL) { LL=L; L=node[L].from; } else { LL=L; L=node[L].to; } } void moveleft_R() { if(RR==0)return; if(node[RR].to==R) { R=RR; RR=node[RR].from; } else { R=RR; RR=node[RR].to; } } void moveright_L() { if(LL==n+1)return; if(L==node[LL].from) { L=LL; LL=node[LL].to; } else { L=LL; LL=node[LL].from; } } void moveright_R() { if(R==n+1)return; if(node[R].from==RR) { RR=R; R=node[R].to; } else { RR=R; R=node[R].from; } } void insert_L(int v) { node[tol].val=v; node[tol].from=L; node[tol].to=LL; if(node[L].to==LL)node[L].to=tol; else node[L].from=tol; if(node[LL].from==L)node[LL].from=tol; else node[LL].to=tol; LL=tol; tol++; } void insert_R(int v) { node[tol].val=v; node[tol].from=RR; node[tol].to=R; if(node[RR].to==R)node[RR].to=tol; else node[RR].from=tol; if(node[R].from==RR)node[R].from=tol; else node[R].to=tol; RR=tol; tol++; } void del_L() { if(LL==n+1)return; if(L==n+1)return; int t; if(node[LL].from==L)t=node[LL].to; else t=node[LL].from; if(node[t].from==LL)node[t].from=L; else node[t].to=L; if(node[L].to==LL)node[L].to=t; else node[L].from=t; LL=t; } void del_R() { if(RR==0)return; if(R==0)return; int t; if(node[RR].to==R)t=node[RR].from; else t=node[RR].to; if(node[t].from==RR)node[t].from=R; else node[t].to=R; if(node[R].from==RR)node[R].from=t; else node[R].to=t; RR=t; } void reverse() { if(node[L].to==LL)node[L].to=RR; else node[L].from=RR; if(node[LL].from==L)node[LL].from=R; else node[LL].to=R; if(node[R].from==RR)node[R].from=LL; else node[R].to=LL; if(node[RR].to==R)node[RR].to=L; else node[RR].from=L; int temp; temp=LL; LL=RR; RR=temp; } void output() { int flag=true; int tt=0; for(int i=node[0].to;i!=n+1;) { if(flag)flag=false; else printf(" "); printf("%d",node[i].val); if(node[i].from==tt) { tt=i; i=node[i].to; } else { tt=i; i=node[i].from; } } printf("\n"); } char str[20]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T; int m; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&node[i].val); node[i].from=i-1; node[i].to=i+1; } node[0].to=1; node[0].from=-1; node[n+1].from=n; node[n+1].to=-1; tol=n+2;//结点数 scanf("%d%d",&LL,&RR); L=LL-1; R=RR+1; scanf("%d",&m); while(m--) { scanf("%s",&str); if(strcmp(str,"MoveLeft")==0) { scanf("%s",&str); if(str[0]=='L')moveleft_L(); else moveleft_R(); } else if(strcmp(str,"MoveRight")==0) { scanf("%s",&str); if(str[0]=='L')moveright_L(); else moveright_R(); } else if(strcmp(str,"Delete")==0) { scanf("%s",&str); if(str[0]=='L')del_L(); else del_R(); } else if(strcmp(str,"Insert")==0) { scanf("%s",&str); if(str[0]=='L') { int v; scanf("%d",&v); insert_L(v); } else { int v; scanf("%d",&v); insert_R(v); } } else reverse(); } output(); } }