UVA 210 双端队列、模拟

模拟单道处理系统。注意不同的程序可能会使用同一变量。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e3+5;

deque<int>ls;
queue<int>ns;
vector<string>cs[N];
int nt[6],rt,p[N],val[2][N],var[26];
bool mark;

void read_time()
{
    for(int i=0; i<5; i++)
    {
        cin>>nt[i];
    }
}

void read_code(int n)
{
    string str;
    ls.clear();

    for(int i=1; i<=n; i++)
    {
        cs[i].clear();
        while(getline(cin,str))
        {
            if(str=="")
                continue;
            cs[i].push_back(str);
            if(str=="end")
                break;
        }
        ls.push_back(i);
    }

}

void run(int i)
{
    int t=rt,num;
    string str;

    while(t>0)
    {
        str=cs[i][p[i]];
        if(str[2]=='=')
        {
            t-=nt[0];
            num=str[4]-'0';
            for(int j=5;j<str.size();j++)
            {
                num*=10;
                num+=str[j]-'0';
            }

            var[str[0]-'a']=num;
        }
        else if(str[2]=='i')
        {
            t-=nt[1];
            cout<<i<<": "<<var[str[6]-'a']<<endl;
        }
        else if(str[2]=='c')
        {
            t-=nt[2];
            if(mark)
            {
                ns.push(i);
                return;
            }
            mark=true;
        }
        else if(str[2]=='l')
        {
            t-=nt[3];
            mark=false;
            if(!ns.empty())
            {
                ls.push_front(ns.front());
                ns.pop();
            }
        }
        else
        {
            return;
        }
        p[i]++;
    }
    ls.push_back(i);

}

int main()
{
    //freopen("1.in","r",stdin);
    //freopen("1.out","w",stdout);

    ios::sync_with_stdio(false);
    cin.tie();

    int T;
    cin>>T;

    while(T--)
    {
        int n;

        cin>>n;

        read_time();

        cin>>rt;

        read_code(n);

        while(!ns.empty())ns.pop();
        mark=false;
        memset(p,0,sizeof(p));
        memset(var,0,sizeof(var));
        while(ls.size())
        {
            int i=ls.front();
            ls.pop_front();
            run(i);
        }

        if(T)
            cout<<""<<endl;
    }

    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/zhangzehua/p/9984559.html
今日推荐