CCF201903-4 消息传递接口(C++)

1 篇文章 0 订阅
#include "stdafx.h"
#include<unordered_map>
#include <iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
using namespace std;
struct thread_node
{
	int source;
	char command;
	int aim;
};
void message_interface()
{
	int t,n;
	string s,token;
	queue<string> **thread_command;
	stack<thread_node> thread_stack;
	thread_node current_thread,next_thread;
	int *flag;
	bool *sock;
	cin>>t>>n;
	thread_command=new queue<string> *[t];
	for(int i=0;i<t;++i)
		thread_command[i]=new queue<string>[n];
	flag=new int[t];
	sock=new bool[n];
	getline(cin,s);
	for(int i=0;i<t;++i)
	{
		for(int j=0;j<n;++j)
		{
			getline(cin,s);
			for(int k=0;k<s.length();++k)
			{
				token.clear();
				for (; s[k] != ' '; ++k)
				{
					if (k == s.length())
						break;
					token.push_back(s[k]);
				}
				thread_command[i][j].push(token);
			}
		}
	}
	for(int i=0;i<t;++i)
	{
		flag[i]=0;
		while (!thread_stack.empty())
			thread_stack.pop();
		for(int j=0;j<n;++j)
			sock[j]=false;
		while (true)
		{
			int j;
			for(j=0;j<n;++j)
			{
				if(!thread_command[i][j].empty())
				{
					current_thread.source=j;
					s=thread_command[i][j].front();
					sock[j]=true;
					thread_command[i][j].pop();
					current_thread.command=s[0];
					current_thread.aim=0;
					for(int k=1;k<s.length();++k)
					{
						current_thread.aim*=10;
						current_thread.aim+=(s[k]-'0');
					}
					thread_stack.push(current_thread);
					break;
				}
			}
			if(j==n)
			{
				break;
			}
			else
			{
				
				while(!thread_stack.empty())
				{
					current_thread=thread_stack.top();
					next_thread.source=current_thread.aim;
					if(sock[next_thread.source]||thread_command[i][next_thread.source].empty())
					{
						flag[i]=1;
						break;
					}
					s=thread_command[i][next_thread.source].front();
					sock[next_thread.source]=true;
					thread_command[i][next_thread.source].pop();
					next_thread.command=s[0];
					next_thread.aim=0;
					for(int k=1;k<s.length();++k)
					{
						next_thread.aim*=10;
						next_thread.aim+=(s[k]-'0');
					}
					thread_stack.push(next_thread);
					if( (current_thread.source==next_thread.aim)
					&&(current_thread.command=='R')&&(next_thread.command=='S')||
					(current_thread.command=='S')&&(next_thread.command=='R') )
					{
						sock[current_thread.source]=sock[current_thread.aim]=false;
						thread_stack.pop();
						thread_stack.pop();
					}

				}
				if(flag[i]==1)
					break;
			}		
		}
	}
	for(int i=0;i<t;++i)
	{
		cout << flag[i] << endl;
	}
	delete []sock;
	delete []flag;
	for(int i=0;i<t;++i)
		delete []thread_command[i];
	delete []thread_command;
}
int main()
{
	message_interface();
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值