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.\n");
            print();
            printf("FINISH\n");
        }
        else{
            printf("No.\n");
            printf("FINISH\n");
        }
    }
    return 0;
}
时间: 2024-08-02 16:19:15

HDU1022 Train Problem I 栈的模拟的相关文章

hdu1022 Train Problem I 栈的应用

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26161    Accepted Submission(s): 9886 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays

HDU1022 Train Problem I (栈)

栈+队列 1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<queue> 5 using namespace std; 6 int main() 7 { 8 int n; 9 char a[11],b[11]; 10 stack<char>s; 11 queue<int>q; 12 while(scanf("%d",&

hdu1022 train problem 栈的应用

#include #include #include using namespace std; int main() { int n; while(cin >> n) { stack one; string od1,od2; bool state[10001]; cin >> od1 >> od2; int from = 0 , to = 0; int i = 0; while(from < n) { while(od1[from] != od2[to]) one

train problem I (栈水题)

杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25276    Accepted Submission(s): 9529 Problem Description As the new term com

Train Problem I(栈)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25773    Accepted Submission(s): 9729 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays.

hdu1022 Train Problem I---模拟栈

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目大意: 车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若能先输出"Yes.",再输出出站步骤,以FINISH结束,若不能,输出"No.",仍以FINISH结束. 思路: 直接模拟栈,注意细节! 1 #include<iostream> 2 #include<cstdio> 3 #include<

hdu1022 Train Problem I

http://acm.hdu.edu.cn/showproblem.php?pid=1022 1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 #include<string.h> 5 #include<stdlib.h> 6 #include<stack> 7 using namespace std; 8 const int N=100; 9 10 int m

HDU 1022 Train Problem I 模拟栈题解

火车进站,模拟一个栈的操作,额外的栈操作,查看是否能按照规定顺序出栈. 数据量很少,故此题目很容易AC. 直接使用数组模拟就好. #include <stdio.h> const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N]; bool rs[MAX_N<<2]; int n; int main() { while (scanf("%d", &n) != EOF) { s

HDU 1022 Train Problem I (STL 栈模拟)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30420    Accepted Submission(s): 11492 Problem Description As the new term comes, the Ignatius Train Station is very busy nowaday