社区讨论

40求调

P9518queue参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lvg2h5se
此快照首次捕获于
2024/04/26 10:41
2 年前
此快照最后确认于
2024/04/26 16:52
2 年前
查看原帖
CPP
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
using namespace std;
const int N = 1000005;//10 6
map<string,int> m;
struct node{
	int pre;
	int nex;
	int mov;
};
node q[N];
string name[N];
string op;
string play[3];
int n,tail,pl,lenq,lh,lt,gok;
int main(){
	scanf("%d",&n);
	tail=0;
	lenq=0;
	lh=1;
	lt=0;
	gok=0;
	while(n--){
		cin>>op;
		if(op[0]=='s'){
			if(lenq==0){//empty
			    printf("Error\n");
			    continue;
            }
			if(gok==0){//the first game
                gok=1;
			}
			else{//the last game
                if(pl==1){
                    tail++;
                    name[tail]=name[lh];
                    m[name[tail]]=tail;
                    q[tail].nex=-1;
                    if(lt==0)
                        q[tail].pre=-1;
                    else{
                        q[tail].pre=lt;
                        q[q[tail].pre].nex=tail;
                    }
                    lt=tail;
                    lh=q[lh].nex;
                }
                if(pl==2){
                    tail++;
                    name[tail]=name[lh];
                    m[name[tail]]=tail;
                    q[tail].nex=-1;
                    if(lt==0)
                        q[tail].pre=-1;
                    else{
                        q[tail].pre=lt;
                        q[q[tail].pre].nex=tail;
                    }
                    lt=tail;
                    lh=q[lh].nex;
                    tail++;
                    name[tail]=name[lh];
                    m[name[tail]]=tail;
                    q[tail].nex=-1;
                    if(lt==0)
                        q[tail].pre=-1;
                    else{
                        q[tail].pre=lt;
                        q[q[tail].pre].nex=tail;
                    }
                    lt=tail;
                    lh=q[lh].nex;
                }
			}
			//new game
			if(lenq==1){
				cout<<name[lh]<<endl;
				play[1].clear();
				play[2].clear();
				play[1]=name[lh];
				pl=1;
			}
			else if(lenq>=2){
				cout<<name[lh]<<' '<<name[lh+1]<<endl;
				play[1].clear();
				play[2].clear();
				play[1]=name[lh];
				play[2]=name[lh+1];
				pl=2;
			}
		}
		else if(op[0]=='a'){
			cin>>op;
			if(m[op]==0){
				m[op]=tail+1;
				name[++tail]=op;
				q[tail].nex=-1;
				if(lt!=0){
                    q[tail].pre=lt;
                    q[lt].nex=tail;
				}
				else q[tail].pre=-1;
				lt=tail;
				lenq++;
				printf("OK\n");
			}
			else{
				printf("Error\n");
			}
		}
		else if(op[0]=='l'){
			cin>>op;
			if(m[op]!=0){
				if((pl>0 && play[1]!=op && play[pl]!=op) || pl==0){
					int tmp=m[op];
					if(lh==lt){
                        q[lt].mov=1;
					}
					else if(lt==tmp){
                        if(q[lt].pre!=-1)
                            q[q[lt].pre].nex=-1;
						q[lt].mov=1;
						lt=q[lt].pre;
					}
					else if(lh==tmp){
					    if(q[lh].nex!=-1)
                            q[q[lh].nex].pre=-1;
						q[lh].mov=1;
						lh=q[lh].nex;
					}
					else{
					    if(q[tmp].pre!=-1)
                            q[q[tmp].pre].nex=q[tmp].nex;
						if(q[tmp].nex!=-1)
                            q[q[tmp].nex].pre=q[tmp].pre;
                        q[tmp].mov=1;
					}
					m[op]=0;
					lenq--;
					printf("OK\n");
				}
				else{
					printf("Error\n");
				}
			}
			else{
				printf("Error\n");
			}
		}
	}
	return 0;
}

Wa

回复

0 条回复,欢迎继续交流。

正在加载回复...