题解 P4896 【Oier们的烦恼】

· · 题解

原理为stringstream ,第一次输入时先刷新缓冲区,将打名字时的回车扔掉。

cin.ignore(INT_MAX, '\n');
//但是为什么不用flush();
//ios::sync_with_stdio(false)
//取消同步stdin,使用flush会失效。

再用getline读。

INT_MAX在climits里,此头文件标记了各类型的最大/小值

#include<iostream>
#include<string>
#include<sstream>
#include<climits>
#include<algorithm>
using namespace std;
string sta;
string S1,S2,S3,S4,S5;
bool Mark[5];
bool Play[5];
bool came;
int N;
string Stu[5];
string Tea[3];
bool cmp(string a,string b) {
    return a<b;
}
//排个序

inline void print() {
    for(int i=0; i<5; i++)if(Mark[i])Stu[i]+="1";
    sort(Stu,Stu+5,cmp);
    for(int i=0; i<5; i++)if(Stu[i].at(Stu[i].length()-1)=='1') {
            for(int sd=0; sd<Stu[i].length()-1; sd++)cout<<Stu[i].at(sd);
            cout<<" ";
        }
    cout<<endl;
}
inline bool check() {
    for(int i=0; i<5; i++)if(Mark[i]==0)return 0;
    return 1;    //全体
}
int main() {
    ios::sync_with_stdio(false);
    cin>>N;
    for(int i=0; i<3; i++)cin>>Tea[i];
    for(int i=0; i<5; i++)cin>>Stu[i];
    cin.ignore(INT_MAX, '\n');
    for(int D=0; D<N; D++) {
        getline(cin,sta);
        istringstream is(sta);
        S1=S2=S3=S4=S5="";
        is>>S1>>S2>>S3>>S4>>S5; //S5防止"!"后还有数据

        //teacher
        if(S2=="came!"&&S3=="") {
            for(int tn=0; tn<3; tn++)if(Tea[tn]==S1) {
                    came=1;
                    for(int mk=0; mk<5; mk++)if(Play[mk])Mark[mk]=1;
                    continue;
                }
        }
        if(S2=="left!"&&S3=="") {
            for(int tn=0; tn<3; tn++)if(Tea[tn]==S1) {
                    came=0;
                    continue;
                }
        }

        //student
        if(S2=="started"&&S3=="playing"&&S4=="games!"&&S5=="") {
            for(int sn=0; sn<5; sn++)if(Stu[sn]==S1) {
                    if(came)Mark[sn]=1;
                    else Play[sn]=1;
                    continue;
                }
        }
        if(S2=="stopped"&&S3=="playing"&&S4=="games!"&&S5=="") {
            for(int sn=0; sn<5; sn++)if(Stu[sn]==S1) {
                    Play[sn]=0;
                    continue;
                }
        }
        if(check()) {
            print();
            cout<<"How Bad Oiers Are!"<<endl;
            return 0;
        }
    }
    for(int i=0; i<5; i++)if(Mark[i]) {
            print();
            return 0;
        }
    cout<<"How Good Oiers Are!"<<endl;
    return 0;
    //蒟蒻首次发题解,如有不足请dalao们多多指教
}