SDU CSPT3模拟

#include<bits/stdc++.h>
using namespace std;
struct window{
    long long u;
    bool top;
    long long cnt;
    bool operator == (const window &t)const{
        return u ==t.u && top == t.top;
    }
};
string tostring(long long u){
    string res = "";
    vector<char>v;
    while(u){
        v.push_back(char(u%10+'0'));
        u/=10;
    }
    for(int i=int(v.size())-1;i>=0;i--){
        res =  res+v[i];
    }
    if(v.empty())res = "0";
    return res;
}

vector<window>q;
void addtofirst(window u){
    vector<window> qq=q;q.clear();
    q.push_back(u);
    for(long long i=0;i<qq.size();i++){
        q.push_back(qq[i]);
    }
}
void Drop(long long id){
    vector<window> qq;
    for(long long i=0;i<q.size();i++){
        if(i!=id)qq.push_back(q[i]);
    }
    q = qq;
    qq.clear();
}
bool Find(long long u){
    for(long long i=0;i<q.size();i++)if(q[i].u==u)return true;
    return false;
}
bool Find(window temp){
    for(long long i=0;i<q.size();i++)if(q[i] == temp)return true;
    return false;
}
long long Getid(long long u){
    for(long long i=0;i<q.size();i++)if(q[i].u==u)return i;
    return -1;
}
bool hastop(){
    for(long long i=0;i<q.size();i++){
        if(q[i].top == true)return true;
    }
    return false;
}
long long top(){
    for(long long i=0;i<q.size();i++)
        if(q[i].top)return i;
        return -1;
}
long long maxu(){
    long long res = 0;
    for(long long i=0;i<q.size();i++)res = max(res,q[i].u);
    return res;
};
void puttohead(long long id){
    window temp = q[id];
    Drop(id);
    addtofirst(temp);
}
queue<string>ans;
void add(long long u){
    if(Find(u)){
        ans.push("same likeness.");
        return;
    }
    window t;t.u =u;t.top=false;t.cnt=0;
    q.push_back(t);
    ans.push("success.");
    return;
}
void Close(long long u){
    if(Find(u)){
        string str="close " + tostring(u)+" with "+tostring(q[Getid(u)].cnt)+".";
        Drop(Getid(u));
        ans.push(str);
    }else{
        ans.push("invalid likeness.");
    }
}
void chat(long long u){
    if(hastop()){
        q[top()].cnt+=u;
        ans.push("success.");
    }else{
        if(q.empty()){
            ans.push("empty.");
            return;
        }else{
            q[0].cnt+=u;
            ans.push("success.");
        }
    }
}
void Rotate(long long x){
    if(x>(long long)q.size() || x<1){
        ans.push("out of range.");
        return;
    }else{
        puttohead(x-1);
        ans.push("success.");
    }
}
void Prior(){
    if(q.empty()){
        ans.push("empty.");
    }else{
        long long id = Getid(maxu());
        puttohead(id);
        ans.push("success.");
    }
}
void Choose(long long u){
    if(Find(u)){
        puttohead(Getid(u));
        ans.push("success.");
    }else{
        ans.push("invalid likeness.");
    }
}
void settop(long long u){
    if(Find(u)){
        for(long long i=0;i<q.size();i++)q[i].top=false;
        q[Getid(u)].top=true;
        ans.push("success.");
    }else{
        ans.push("invalid likeness.");
    }
}
void Untop(){
    if(hastop()){
        for(long long i=0;i<q.size();i++)q[i].top=false;
        ans.push("success.");
    }else{
        ans.push("no such person.");
    }
}
void Clearwindow(){
    string str;
    if(hastop()){
        str = "Bye " + tostring(q[top()].u) +": "+tostring(q[top()].cnt)+".";
        if(q[top()].cnt!=0)ans.push(str);
        Drop(top());
    }
    for(long long i=0;i<q.size();i++){
        str = "Bye " + tostring(q[i].u) +": "+tostring(q[i].cnt)+".";
        if(q[i].cnt!=0)ans.push(str);
    }
}
int main()
{
     long long t,n;
     cin>>t;
     while(t--){
         q.clear();
         cin>>n;
        string str;long long u;
        for(long long i=1;i<=n;i++){
            cin>>str;
            if(str=="Add"){
                cin>>u;
                add(u);
            }
            if(str=="Close"){
                cin>>u;
                Close(u);
            }
            if(str=="Chat"){
                cin>>u;
                chat(u);
            }
            if(str=="Rotate"){
                cin>>u;
                Rotate(u);
            }
            if(str=="Prior"){
                Prior();
            }
            if(str=="Choose"){
                cin>>u;
                Choose(u);
            }
            if(str=="Top"){
                cin>>u;
                settop(u);
            }
            if(str=="Untop"){
                Untop();
            }
        }
        Clearwindow();
        long long cnt=0;
        while (!ans.empty()){
            cout<<"OpId #"<<++cnt<<": "<<ans.front()<<endl;
            ans.pop();
        }
     }
    return 0;
}
/*
30
Add 4
Add 3
Chat 4
Add 3
Rotate 2
Chat 5
Prior
Top 3
Choose 4
Rotate 1
Add 2
Close 4
Chat 7
Choose 2
Chat 7
Add 3
Top 2
Add 4
Choose 3
Chat 7
Prior
Top 3
Rotate 1
Rotate 3
Chat 7
Top 4
Add 2
Close 2
Prior
Add 4


 1
10
Add 1
Add 2
Add 3
Rotate 1
Rotate 10
Rotate -1
Rotate 1
Prior
Choose 12
Choose 11
*/
原文地址:https://www.cnblogs.com/yesuweiYYYY/p/14687508.html