The CCF (messaging mouth: 80): the string associated queue +

Messaging port

201903-4

The main question is the use of simulation queue, because at first I did not notice in accordance with the order, so the beginning of the wrong solution.

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<queue>
#include<sstream>
using namespace std;
const int maxn=10004;
int t,n;
struct node{
    int flag;//flag=0表示s,flag=1表示r
    int to;
    node(string s){
        char c=s[0];
        if(c=='S')
            flag=0;
        else if(c=='R')
            flag=1;
        int ans=0;
        for(int i=1;i<s.length();i++){
            ans=ans*10+s[i]-'0';
        }
        to=ans%n;
    }
    node(){}
};
int main(){
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    cin>>t>>n;
    getchar();//------------------------------------这个很重要
    while(t--){ 
        queue<node> que[maxn];
        stringstream gs;
        for(int i=0;i<n;i++){
            string s;
            getline(cin,s);
            gs<<s;
            while(gs>>s){
                //cout<<s<<endl;3 2
                que[i].push(node(s));
            }
            gs.clear();
        }
        while(true){
            bool flag=false;
            for(int i=0;i<n;i++){
                if(que[i].empty())
                    continue;
                node t=que[i].front();
                int j=t.to;
                //cout<<j<<endl;
                if(que[j].empty())//只要一个有匹配而另一个已经空了,就退出,这时候答案就是1死锁
                    break;
                node t1=que[j].front();
                int j1=t1.to;
                //cout<<j1<<endl;
                if(j1!=i)
                    continue;
                if(t1.flag+t.flag!=1)
                    continue;
                //cout<<que[i].front().flag<<que[j].front().flag<<endl;
                que[i].pop();
                que[j].pop();
                flag=true;
                i--;
                //cout<<"yes"<<endl;
            }
            if(!flag)
                break;
        }
        bool flag=true;
        for(int i=0;i<n;i++){
            if(!que[i].empty()){
                flag=false;
                break;
            }
        }
        if(flag){
            //puts("0");
            cout<<0<<endl;
        }else
        {
            //puts("1");
            cout<<1<<endl;
        }
        gs.str("");
    }
    //system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/GarrettWale/p/11431463.html
80