zoukankan      html  css  js  c++  java
  • HDU1022 Train Problem I 栈的模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042

    栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理。 需要考虑到各种情况, 分类处理。

    常见错误:使用前未清空栈

    使用STL思路较为清晰

    代码附上, 欢迎各位大神指点~~

    #include <cstdio>
    #include <stack>
    #include <iostream>
    #include <vector>
    using namespace std;
    stack<char> s;
    const int maxn = 1000 + 100;
    char in[maxn], out[maxn]; //记录栈的原始次序, 出栈次序
    vector<string> str; //用以记录元素出入过程
    int solve(int n)
    {
        int A = 0, B = 0;
        while(B < n){
            if(in[A] == out[B]){
                s.push(in[A++]);
                str.push_back("in");
                s.pop(); B++;
                str.push_back("out");
            }
            else if(!s.empty()&&s.top() == out[B]){
                    s.pop();
                    str.push_back("out");
                    B++;
            }
            else if(A < n){
                s.push(in[A++]);
                str.push_back("in");
            }
            else return 0;
        }
        if(s.empty()) return 1;
        else return 0; 
    }
    
    void print()
    {
        for(vector<string>::iterator i = str.begin(); i != str.end(); i++){
            cout << *i << endl;
        }
    }
    
    int main()
    {
        int n;
        while(~scanf("%d", &n)){
            getchar();
            str.clear(); 
            memset(in, 0, sizeof(in));
            memset(out, 0, sizeof(out));
            while(!s.empty()) s.pop();
            scanf("%s%s", in, out);
            if(solve(n)){
                printf("Yes.
    ");
                print();
                printf("FINISH
    ");
            } 
            else{
                printf("No.
    ");
                printf("FINISH
    ");    
            } 
        }
        return 0;
    }
  • 相关阅读:
    鱼眼相机畸变矫正资料
    异常值检测算法三:3sigma模型
    五:瑞芯微RV1109
    四:海思Hi3516CV500/Hi3516DV300
    三:瑞芯微OK3399-C开发板
    二:飞凌嵌入式FCU1201
    一:芯片概述
    六:大数据架构
    五:大数据架构回顾-LambdaPlus架构
    四:大数据架构回顾-IOTA架构
  • 原文地址:https://www.cnblogs.com/ACFLOOD/p/2013_11_29_0.html
Copyright © 2011-2022 走看看