紫书UVa210

本题UVa上描述有误:
原题输入中少了一行,代表其实例数。
输出时,每个实例应相隔一行!!!

在这里插入图片描述

#include <vector>
#include <map>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstdio>
#include <sstream>
#include <string>
#include<iostream>
#include<iomanip>
#include<stack>
#include<utility>
#include<unordered_map>
#include<queue>
#include<cstring>
#include<deque>
using namespace std;
int parse_instru(const string& str,string& var,int &value)
{
	if (str.find("end")!=-1)
		return 4;
	else if (str.find("unlock")!=-1)
		return 3;
	else if (str.find("lock")!=-1)
		return 2;
	else if (str.find("print")!=-1)
	{
		var = str.substr(str.find("print") + 6);
		return 1;
	}
	else
	{
		var = str.substr(0, str.find(" = "));
		string strvalue = str.substr(str.find(" = ")+3);
		stringstream ss;
		ss << strvalue;
		ss >> value;
		return 0;
	}
}
int main()
{
	int t;
	cin >> t;
	for(int T=0;T<t;++T)
	{
		if (T != 0)
			puts("");
		//freopen("input.txt", "r", stdin);
		int n;
		scanf("%d", &n);
		int x[5];
		int t;
		for (int i = 0; i < 5; ++i)
		{
			scanf("%d", &x[i]);
		}
		scanf("%d", &t);
		cin.get();
		string read;
		unordered_map<string, int>variables;
		vector<queue<string>>programs(n);
		deque <int>wait;
		deque<int>block;
		for (int i = 0; i < n; ++i)
		{
			do
			{
				getline(cin, read);
				programs[i].push(read);
			} while (read != "end");
		}
		bool lock = 0;
		for (int i = 0; i < n; ++i)
		{
			wait.push_back(i);
		}
		while (wait.size() > 0)
		{
			int time = 0;
			string instru;
			string temp;
			int value;
			int num = wait.front();
			int order;
			wait.pop_front();
			while (time < t && instru != "end")
			{
				instru = programs[num].front();
				order = parse_instru(instru, temp, value);
				if (order == 0)
				{
					variables[temp] = value;
					programs[num].pop();
				}
				else if (order == 1)
				{
					printf("%d: %d\n", num + 1, variables[temp]);
					programs[num].pop();
				}
				else if (order == 2)
				{
					if (lock == 1)
					{
						block.push_back(num);
						break;
					}
					lock = 1;
					programs[num].pop();
				}
				else if (order == 3)
				{
					lock = 0;
					if (block.size() >= 1)
					{
						wait.push_front(block.front());
						block.pop_front();
					}
					programs[num].pop();
				}
				else if (order == 4)
				{
					programs[num].pop();
					break;
				}
				time += x[order];
			}
			if (instru != "end" && time >= t)
			{
				wait.push_back(num);
			}

		}
	}
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的程序

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值