本题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);
}
}
}
}