天天看点

uva 540(线性表)

题解:用一个map存储每个号码的队伍号,然后再用二维队列存放每个队伍的进场号码,最后再用一个队列存储已经在排队的队伍号,队伍空了就pop掉,下一个队伍补上。

#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
using namespace std;

int main() {
	int t, n, a, cases = 1, flag;
	queue<int> q[1005];
	queue<int> q2;
	char str[10];
	while (scanf("%d", &t) && t) {
		map<int,int> m;
		for (int i = 1; i <= t; i++) {
			scanf("%d", &n);
			for (int j = 1; j <= n; j++) {
				scanf("%d", &a);
				m[a] = i;
			}
		}
		printf("Scenario #%d\n", cases++);
		while (scanf("%s", str)) {
			if (str[0] == 'S')
				break;
			else if (str[0] == 'E') {
				scanf("%d", &a);
				if (q[m[a]].empty())
					q2.push(m[a]);
				q[m[a]].push(a);
			}
			else {
				flag = q2.front();
				printf("%d\n", q[flag].front());
				q[flag].pop();
				if (q[flag].empty()) {
					q2.pop();
				}
			}
		}
		printf("\n");
		for (int i = 1; i <= t; i++)
			while (!q[i].empty())
				q[i].pop();
		while (!q2.empty())
			q2.pop();
	}
	return 0;
}
           

继续阅读