#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判断是否集合为空