试题编号: 201903-4
试题名称: 消息传递接口
时间限制: 1.0s
内存限制: 512.0MB
问题描述:
没想到第四题还是出的模拟,考试的时候没做出来,也不知为啥爆0了
看了一下别人的题解重新写了下过了
代码如下(100分):
#include <iostream>
#include <string>
#include <queue>
using namespace std;
static const int MAXN=10000;
string s;
struct Node{
int kind,num;
};
queue<Node> Q[MAXN+10];
void div(string s,int k)
{
for(int i=0;i<s.size();i++)
{
int temp;
if(s[i]=='R' || s[i]=='S')
temp=s[i]-'R';
i++;
int num=0;
while(i<s.size() && s[i]!=' ')
{
num=num*10+s[i]-'0';
i++;
}
Q[k].push(Node{temp,num});
}
}
int main()
{
//std::ios::sync_with_stdio(false);
//std::cin.tie(0),cout.tie(0);
int T,n;
cin>>T>>n;
getchar();
while(T--)
{
for(int i=0;i<n;i++)
{
while(!Q[i].empty())
Q[i].pop();
getline(cin,s);
div(s,i);
}
while(1)
{
bool flag=false;
for(int i=0;i<n;i++)
{
if(Q[i].empty())
continue;
Node now=Q[i].front();
if(Q[now.num].empty())
break;
Node next=Q[now.num].front();
if(now.kind+next.kind!=1 || next.num!=i)
continue;
else
{
Q[i].pop();
Q[now.num].pop();
flag=true;
i--;
}
}
if(!flag)
break;
}
bool flag=true;
for(int i=0;i<n;i++)
{
if(Q[i].size())
{
cout<<1<<endl;
flag=false;
break;
}
}
if(flag)
cout<<0<<endl;
}
return 0;
}