• 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;
    }
  • 相关阅读:
    efwplus框架
    注册区域
    社招面试记录与总结
    验证码 Captcha 之大插件
    发生内存泄漏?
    Flume+LOG4J+Kafka
    协议如何保证可靠传输
    oracle之spool详细使用总结(转)
    SSH协议详解(转)
    oracle nologging用法(转)
  • 原文地址:https://www.cnblogs.com/ACFLOOD/p/2013_11_29_0.html
Copyright © 2020-2023  润新知