Uva210 Concurrency Simulator

原题:

该题是运用双端队列的一道练习题,注意的点是当lock操作不成功时,在源lock程序unlock后应当返回的是lock语句,而非其后。

只要熟练运用队列,本题还是较为简单的。

代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<sstream>
#include<queue>
#include<fstream>
#include<map>
#define endl "\n"
using namespace std;
void parse_equal(const string& s,string& left,string& right)
{
    int k=s.find("=");
    left=s.substr(0,k-1);
    right=s.substr(k+2);
}
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,t1,t2,t3,t4,t5,Q;
    bool ok=false;
    int T;
    cin>>T;
    while(T--)
    {
        int i=0;
        cin>>n>>t1>>t2>>t3>>t4>>t5>>Q;
        string s;
        vector<queue<string>> dest(n);
        deque<int> wait;//等待队列
        queue<int> anti_wait,judge;//阻止队列 lock判断
        map<string,int> val;//赋值语句映射
        cin.get();
        while(i<n)
        {
            while(getline(cin,s)&&s!="end")
            {
                dest[i].push(s);
            }
            dest[i].push(s);
            wait.push_back(i);
            i++;
        }
        if(ok)
        {
            cout<<endl;
        }
        else
        {
            ok=true;
        }
        while(!wait.empty())
        {
            int cur=wait.front();
            wait.pop_front();
            bool flag=true;
            int t=Q;
            while(t>0&&!dest[cur].empty())
            {
                string temp=dest[cur].front();
                if(temp.find("=")!=string::npos)
                {
                    string left,right;
                    parse_equal(temp,left,right);
                    val[left]=stoi(right,nullptr,10);
                    t-=t1;
                }
                else if(temp.find("print")!=string::npos)
                {
                    string right=temp.substr(6);
                    cout<<cur+1<<": "<<val[right]<<endl;
                    t-=t2;
                }
                else if(temp=="lock")
                {
                    t-=t3;
                    if(judge.empty())
                    {
                        judge.push(cur);
                    }
                    else
                    {
                        anti_wait.push(cur);
                        flag=false;
                        break;
                    }
                }
                else if(temp=="unlock")
                {
                    t-=t4;
                    if(!judge.empty()&&cur==judge.front())
                    {
                        judge.pop();
                    }
                    if(!anti_wait.empty())
                    {
                        wait.push_front(anti_wait.front());
                        anti_wait.pop();
                    }
                }
                else
                {
                    t-=t5;
                    flag=false;
                    break;
                }
                dest[cur].pop();
            }
            if(flag)
            {
                wait.push_back(cur);
            }
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值