2019-3-4 消息传递接口

#include<bits/stdc++.h>
using namespace std;
int T,n;

int main()
{
    scanf("%d %d", &T, &n);
    getchar();
    while(T--)
    {
        list<pair<queue<int>,int> > q;
        unordered_set<int> commands;
        for(int i = 0; i < n; ++i)
        {
            string s;
            getline(cin,s);
            int len = s.size();
            queue<int> currentQueue;
            for(int j = 0, k; j < len; j=k+1)
            {
                k = s.find(' ',j);
                if(k==string::npos)
                    k=len;
                int index=0;
                for(int w = j+1; w < k; w++){
                    (index*=10)+=s[w]-'0';
                }
                if(s[j]=='S')
                    currentQueue.push(10000*i+index);
                else
                    currentQueue.push(-index*10000-i);
            }
            q.push_back({currentQueue,INT_MAX});
        }
        for(bool f = true; !q.empty(); f = true)
        {
            for(auto i = q.begin(); i!=q.end(); ++i)
            {
                if(commands.find(i->second) != commands.end())
                    continue;
                if((i->first).empty())
                {
                    i = q.erase(i);
                    continue;
                }
                while(!(i->first).empty())
                {
                    int currentFigure = (i->first).front();
                    (i->first).pop();
                    auto j = commands.find(-currentFigure);
                    if(j != commands.end())
                    {
                        commands.erase(j);
                        f = false;
                    }else{
                        i->second = currentFigure;
                        commands.insert(currentFigure);
                        break;
                    }
                }
            }
            if(f)break;
        }
        puts(q.empty()?"0":"1");

    }
}

代码参考:

https://blog.csdn.net/richenyunqi/article/details/89195878

 

string s;

读取一行 getline(cin,s);

查找字符串匹配的位置s.find("要查找的字符串");

s.find("要查找的字符串",从某索引处开始查找);

反向查找rfind

没有找到时,返回该类型的最大值,即string::npos

 

pair内有两个元素的结构体, 获取元素方式x->first    x->second

list   push_back()添加   erase()删除

auto  c++11自动匹配类型

unordered_set    insert添加  erase删除   find查找是否存在,若没有找到返回值为x.end()

 

.empty判断是否集合为空

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值