您的位置:首页 > 产品设计 > UI/UE

deque双端队列&&UVa210 Concurrency Simulator(并行程序模拟)的理解与解析

2015-10-27 16:35 351 查看
deque双端队列&&Concurrency Simulator(并行程序模拟)的理解与解析

deque 双端队列

支持快速随机访问。在 头尾位置 插入/删除速度很快。
deque是一种更为复杂的数据结构。与string和vector类似,deque支持快速的随机访问。与string和vector一样,在deque的中间位置添加和删除元素的代价(可能)很高。但是,在deque的两端添加或删除元素都是很快的,与list或forward_list添加删除元素的速度相当。

                                                                                                                                                                                          from   C++ Primer 5th

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line of the input file consists of seven integers separated by spaces. These integers specify(in order): the number of programs which follow, the unit execution times for each of the five statements(in the order
given above), and the number of time units comprising the time quantum. The remainder of the input consists of the programs, which are correctly formed from statements according to the rules described above.

All program statements begin in the first column of a line. Blanks appearing in a statement should be ignored. Associated with each program is an identification number based upon its location in the input data (the first
program has ID = 1, the second has ID = 2, etc.).

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Your output will contain of the output generated by the print statements as they occur during the simulation. When a print statement is executed, your program should display the program ID, a colon,a space, and the value
of the selected variable. Output from separate print statements should appear on separate lines.

Sample Input

1                                (这个是加上去的,表示测试多少组数据)

3 1 1 1 1 1 1

a = 4

print a

lock

b = 9

print b

unlock

print b

end

a = 3

print a

lock

b = 8

print b

unlock

print b

end

b = 5

a = 17

print a

print b

lock

b = 21

print b

unlock

print b

end

Sample Output

1: 3

2: 3

3: 17

3: 9

1: 9

1: 9

2: 8

2: 8

3: 21

3: 21

Solution

解决方法主要来自另一个地方http://blog.csdn.net/acvay/article/details/43054111,
自己修改了小部分,增加了自己的理解和解析:

#include <iostream>
#include <deque>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>  //  memset(,,)  是在这里面的
using namespace std;
const int N = 1005;
//这些变量都定义成全局,只要是函数里面要用到的东西比较多,总不能给函数那么多的参数吧,这么做挺好的
bool lock;
deque<int> qr;//等待队列       把每个队列,用给队列的序号来记录、处理
queue<int> qb;//阻止队列
vector<string> prg
;
string s;
int t
, p
, var[26]/*可以用26个字母代替26个数字,因为变量最多只有26个*/, lim;
//m[26]等,把二十六个字母转化为数字来进行储存和处理,很常用的

void run(int i)//每一次执行配额时间(单位时间)
{
int rt = lim, v;    //copy一份配额(单位时间),这样可以在使用的时候保留原来的标准lim配额
string cur;
while(rt > 0){
cur = prg[i][p[i]];
if(cur[2] == '='){  // 赋值
rt -= t[0];
v = cur[4] - '0';
if(cur.size() == 6) v = v * 10 + cur[5] - '0';//!常数是小于100的数,所以最多是两位数。故.size()==6就是两位了
var[cur[0] - 'a'] = v;    //!给变量赋值了   ,且把字母化为数字来存储了
}
else if(cur[2] == 'i'){   //print
rt -= t[1];
printf("%d: %d\n", i, var[cur[6] - 'a']);  //打印的只会是单个的字母 所对应 的变量 的值
}
else if(cur[2] == 'c'){   //lock
rt -= t[2];
if(lock){
qb.push(i);                      //!queue用的.push() 而不用push_back()
return;                          //直接进入下一个程序的运行
}
else lock = true;
}
else if(cur[2] == 'l'){  //unlock
lock = false;
rt -= t[3];
if(!qb.empty()){                    //阻止队列
v = qb.front();                 //的第一个
qb.pop();                       //程序进入
qr.push_front(v);
}
}
else return;  //end     如果到这里,说明它运行了 end  可以直接退出函数
++p[i];
}
qr.push_back(i);    //main()里面有qr.pop_front(i)了,这样就把它放到末尾了
}

int main()
{
int cas, n;
scanf("%d", &cas);
while(cas--){
scanf("%d", &n);
for(int i = 0; i < 5; ++i)
scanf("%d", &t[i]);
scanf("%d", &lim);

for(int i = 1; i <= n; ++i){            //队列名总不能从0开始
prg[i].clear();
while(getline(cin, s)){
if(s == "") continue;            //i记录的是队列的序号
prg[i].push_back(s);
if(prg[i].back() == "end") break;//.back()  和   .front()
}
qr.push_back(i);                     //这个不要漏掉
}

memset(p, 0, sizeof(p));
memset(var, 0, sizeof(var));
while(!qr.empty()){
int cur = qr.front();
qr.pop_front();
run(cur);
}
if(cas) puts("");
}
return 0;
}

//!@ProLights




谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: