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;
    }
  • 相关阅读:
    光学字符识别OCR-6 光学识别
    光学字符识别OCR-5 文本切割
    光学字符识别OCR-4
    光学字符识别OCR-3
    leetcode 7 Reverse Integer(水题)
    leetcode 1 Two Sum(查找)
    DFS的简单应用(zoj2110,poj1562)
    Havel-Hakimi定理(握手定理)
    zoj1360/poj1328 Radar Installation(贪心)
    饶毅:做自己尊重的人
  • 原文地址:https://www.cnblogs.com/ACFLOOD/p/2013_11_29_0.html
Copyright © 2011-2022 走看看