CSP 201903-4 消息传递接口

试题编号: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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值