试题编号: | 201903-4 |
试题名称: | 消息传递接口 |
时间限制: | 1.0s |
内存限制: | 512.0MB |
问题描述: | |
#include "pch.h"
#include <iostream>
#include<iomanip>
using namespace std;
struct note {
char ch;
int ID;
};
int str2num(string s)
{
int num;
stringstream ss(s);
ss >> num;
return num;
}
string num2str(int num)
{
stringstream ss;
ss << num;
return ss.str();
}
queue<note> Room[10001]; //每次创建时间很长
int fun(int M)
{
bool Flag = 1;
while (Flag)
{
Flag = 0;
for (int j = 0; j < M; j++)
{
if (!Room[j].empty())
{
note com_a = Room[j].front();
if (!Room[com_a.ID].empty()) //不为空
{
note com_b = Room[com_a.ID].front();
if (com_b.ch != com_a.ch && com_b.ID == j)
{
Room[j].pop();
Room[com_a.ID].pop();
Flag = 1;
}
else if (com_b.ID == j)
return 0;
}
else
return 0;
}
}
}
return 0;
}
int main()
{
int i, j = 0, N, M;
cin >> N >> M; //N个样例,M个进程
cin.ignore();
stringstream SS;
string S;
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
while (!Room[j].empty()) Room[j].pop();
for (j = 0; j < M; j++)
{
getline(cin, S);
SS.clear();
SS.str(S);
int num = count(S.begin(), S.end(), ' ');
string S_save;
for (int k = 0; k < num + 1; k++)
{
SS >> S_save;
note no;
no.ch = S_save[0];
no.ID = str2num(S_save.substr(1));
Room[j].push(no);
//cout << S_save << endl;
}
}
fun(M);
int Flag = 0;
for (int k = 0; k < M; k++) {
if (!Room[k].empty()) {
Flag = 1;
break;
}
}
cout << Flag << endl;
}
return 0;
}