hdu1022 Train Problem I(STL 棧的應用)

#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
        stack<int> mystack;
        int i,j,t,k;
        char a[11],b[11];
        string s[1000];
        for(i=0;i<n;i++) cin>>a[i];
        for(i=0;i<n;i++) cin>>b[i];
        i=0;t=0;k=0;
        while(t<n)
        {
            if((mystack.empty()||mystack.top()!=b[t])&&i<n)
            {
                mystack.push(a[i]);s[k]="in";
                i++;
            }
            else
            {
                if(mystack.top()==b[t])
                {
                    mystack.pop();s[k]="out";
                    t++;
                }
                else break;
            }
            k++;
        }
        if(mystack.empty())
        {
            cout<<"Yes./n";
            for(j=0;j<k;j++)
                cout<<s[j]<<endl;
        }
        else cout<<"No./n";
        cout<<"FINISH/n";
    }
    return 0;
}

發佈了28 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章