题目:戳这里
题意:
t组样例,每组有n个进程。每个进程有1~8个指令,进程中的指令按顺序执行,问样例中的指令是否合法。
解题思路:
用队列模拟执行指令的过程。
本人代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1e4 + 10;
queue<char> queOp[maxn];//指令类型
queue<int> queId[maxn];//指令对于的进程号
char temp[1000+10];
int T, n;
bool judg(queue<char> x[]) { //判断队列数组是否为空
for(int i = 0; i < n; ++i) {
if(!x[i].empty()) return 0;
}
return 1;
}
int fun(string x) {
int y = 0;
for(int i = 1; i < x.length(); ++i) {
y = y * 10 + x[i] -'0';
}
return y;
}
int main() {
cin>>T;
cin>>n;
getchar();
while(T--) {
for(int i = 0; i < n; ++i) {
while(!queOp[i].empty()) queOp[i].pop(), queId[i].pop();
fgets(temp, 1010, stdin);
int len = strlen(temp);
int x= -1;
for(int j = 0; j <= len; ++j) {
if(temp[j] >= '0' && temp[j] <= '9') {
if(x == -1) queOp[i].push(temp[j-1]), ++x;
x = x * 10 + temp[j] - '0';
} else {
if(x!=-1) {
queId[i].push(x);
}
x = -1;
}
}
}
int now = 0;
int f = 0;//判断在一轮中,是否至少有一次匹配成功
while(!judg(queOp)) {
for(now = 0; now < n; ++now) {
if(queOp[now].empty()) continue;
int x = queId[now].front();
if(queOp[x].empty()) break;
int y = queId[x].front();
if(queOp[x].front() != queOp[y].front()) {
if(y == now) {
queOp[x].pop();
queOp[y].pop();
queId[x].pop();
queId[y].pop();
f = 1;
--now;
}
}
}
if(f == 1) f = 0;
else break;
}
if(judg(queOp)) printf("%d\n", 0);
else printf("%d\n", 1);
}
return 0;
}
/*
3 2
R1 S1
S0 R0
R1 S1
R0 S0
R1 R1 R1 R1 S1 S1 S1 S1
S0 S0 S0 S0 R0 R0 R0 R0
2 3
R1 S1
R2 S0 R0 S2
S1 R1
R1
R1 R2 S0 R0
S1 R1
*/