zoukankan      html  css  js  c++  java
  • HDOJ 1022 (栈的问题)

    //1022.火车调度
    
    //栈的问题 
    
    
    Sample Input
    3 123 321
    3 123 312
    Sample Output
    Yes.
    in
    in
    in
    out
    out
    out
    FINISH
    No.
    FINISH
    
    
    #include <iostream>
    #include <stack>
    #include <queue>
    using namespace std;
    
    int main()
    {
    
        int n;
        char a[10],b[10];
        while(cin>>n)
            {
                queue<string>s;                          //queue和stack都没有clear()操作。 
                stack<int>z;
                int i=0,j=0;                             //指向串a,b的指针 
                cin>>a>>b;
                while(i<n)                               //原字符串遍历完全时结束 
                    {
                        while(z.empty()||z.top()!=b[j])  //当栈为空或者栈顶不等于目标字符时入栈 
                            {
                                z.push(a[i]);
                                s.push("in");             //将入栈和出栈操作存储在队列queue中(也可以用vector)都可以不定长。                
                                i++; 
                            }
                        while(!z.empty()&&z.top()==b[j])//由上一个操作知道此时z.top一定满足目标字符串 
                            {                           //否则结束,因为可能新的栈顶元素符合条件,所以 
                                z.pop();                //需要用while进行可能的连续出栈 。 
                                s.push("out");          //why》!z.empty(),空栈顶和空数组可能使死循环。 
                                j++;
                            }                    
                    }
                if(!z.empty())
                    cout<<"No."<<'
    '<<"FINISH"<<endl;
                else
                    {
                        cout<<"Yes."<<'
    ';
                        while(!s.empty())
                            {
                                cout<<s.front()<<endl;
                                s.pop();
                            }
                        cout<<"FINISH"<<endl;
                    }
                     
            }
    }
  • 相关阅读:
    tp框架自带扩展分页类修改样式
    win7获取管理员权限
    Git学习手记(二)
    安卓导出安装包
    浅谈存储过程
    Java宝典
    单例设计模式
    关于Cookie的有关内容
    开辟html5和css3学习随笔(2015-3-2)
    关于面试题
  • 原文地址:https://www.cnblogs.com/biggan/p/7416715.html
Copyright © 2011-2022 走看看