消息传递口

201903-4

本题主要是利用队列进行模拟,因为一开始我没有注意到要按照顺序,所以一开始的解法错误。

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<queue>
#include<sstream>
using namespace std;
const int maxn=10004;
int t,n;
struct node{
int flag;//flag=0表示s,flag=1表示r
int to;
node(string s){
char c=s[0];
if(c=='S')
flag=0;
else if(c=='R')
flag=1;
int ans=0;
for(int i=1;i<s.length();i++){
ans=ans*10+s[i]-'0';
}
to=ans%n;
}
node(){}
};
int main(){
// ios::sync_with_stdio(false);
// cin.tie(0);
cin>>t>>n;
getchar();//------------------------------------这个很重要
while(t--){
queue<node> que[maxn];
stringstream gs;
for(int i=0;i<n;i++){
string s;
getline(cin,s);
gs<<s;
while(gs>>s){
//cout<<s<<endl;3 2
que[i].push(node(s));
}
gs.clear();
}
while(true){
bool flag=false;
for(int i=0;i<n;i++){
if(que[i].empty())
continue;
node t=que[i].front();
int j=t.to;
//cout<<j<<endl;
if(que[j].empty())//只要一个有匹配而另一个已经空了,就退出,这时候答案就是1死锁
break;
node t1=que[j].front();
int j1=t1.to;
//cout<<j1<<endl;
if(j1!=i)
continue;
if(t1.flag+t.flag!=1)
continue;
//cout<<que[i].front().flag<<que[j].front().flag<<endl;
que[i].pop();
que[j].pop();
flag=true;
i--;
//cout<<"yes"<<endl;
}
if(!flag)
break;
}
bool flag=true;
for(int i=0;i<n;i++){
if(!que[i].empty()){
flag=false;
break;
}
}
if(flag){
//puts("0");
cout<<0<<endl;
}else
{
//puts("1");
cout<<1<<endl;
}
gs.str("");
}
//system("pause");
return 0;
}

最新文章

  1. 保留password模式文本框textbox内的数据不丢失。
  2. 人工智能 启发式算法(A,A*)
  3. SignalR的Javascript客户端API使用方式整理
  4. 在web.xml注册applicationContext.xml配置文件
  5. C#知识体系(一) --- 常用的LInq 与lambda表达式
  6. Java应用程序监控JavaMelody
  7. asp.net linux 环境部署, jexus
  8. 6/17 Sprint3
  9. Logs
  10. 【UVA 11183】 Teen Girl Squad (定根MDST)
  11. Travel(HDU 5441 2015长春区域赛 带权并查集)
  12. vs2015 key
  13. Spring Boot 正常启动后访问Controller提示404
  14. Android 9.0更新
  15. H5选择颜色-前端颜色选择器
  16. 两个线程分别打印 1- 100,A 打印偶数, B打印奇数。
  17. AsyncTask使用详细说明
  18. Tcp下载文件
  19. DELL R710使用4T硬盘亮黄灯
  20. Linux Directory Structure

热门文章

  1. 9.PowerShell DSC之Pull
  2. Xtrabackup 物理备份
  3. linux内核编程入门--系统调用监控文件访问
  4. C++ part5
  5. springboot项目打war包
  6. 在利用手背扫描图像+K因子 对室内温度进行回归预测时碰到的问题
  7. 如何快速定位 Redis 热 key?
  8. CSS style color all in one
  9. ORM All In One
  10. flutter 1.5 in action