Problem D: 栈小游戏

时间:2022-02-20 06:53:05
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
class Operation
{
public:
    vector<int>  data;
    stack <int>  iStack;
    vector<char> opers;
    vector<int>  result;
    void inputData()
    {
        int t;
        while(cin>>t)
        {
            if(t==0)
                break;
            data.push_back(t);
        }
    }
    void  inputOpers()
    {
        char ch;
        while(cin>>ch)
        {
            if(ch=='e'||ch=='E')
                break;
            else if(ch=='i'||ch=='I'||ch=='O'||ch=='o')
            opers.push_back(ch);
            else
                continue;
        }
    }
    void  clearData()
    {
        while(!iStack.empty())
            iStack.pop();
        while(opers.begin()!=opers.end())
            opers.erase(opers.begin());
        while(data.begin()!=data.end())
            data.erase(data.begin());
        while(result.begin()!=result.end())
            result.erase(result.begin());
    }
    void showResult()
    {
        int t=0;
        for(int i=0;i<opers.size();i++)
        {
            if(opers[i]=='i'||opers[i]=='I')
            {
                if(t<data.size())
                 iStack.push(data[t++]);
            }

            else if(opers[i]=='o'||opers[i]=='O')
            {
            if(!iStack.empty())
                {
                result.push_back(iStack.top());
                iStack.pop();
                }
            }
        }
        cout<<"The pop sequence is:";
        if(!result.empty())
        {
            for(int i=0;i<result.size();i++)
                cout<<" "<<result[i];
        }
        else
            cout<<" Empty.";
        cout<<endl;
    }
};
int main()
{
    Operation myOp;
    int cases;
    cin>>cases;
    for (int i = 0; i < cases; i++)
    {
        myOp.clearData();
        myOp.inputData();
        myOp.inputOpers();
        myOp.showResult();
    }
    return 0;
}

注意空格,血的教训!