hdu 1022 Train Problem I(栈)

题目链接:https://vjudge.net/problem/HDU-1022

  非常经典的stack模拟,wa了n次才过....

#pragma G++ optimize(2)
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<stdio.h>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a))
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll ll_INF = 0x3f3f3f3f3f3f3f;
const int maxn = 1e3+10;
int mark[maxn];
int main(void) {
    //std::ios::sync_with_stdio(false);
    int n;
    char _in[maxn], _out[maxn]; 
    //in存储入栈顺序 out存储出栈顺序
    while(~scanf("%d%s%s", &n, _in, _out)) {
        mst(mark);
        int c1 = 0, c2 = 0;
        stack<char> trains;
        for (int i = 0; i<n; ++i) {
            trains.push(_in[i]);
            mark[c1++] = 1;
            while(!trains.empty() && trains.top() == _out[c2]) { 
                //如果栈首元素和出栈的元素相对应就弹出
                trains.pop();
                ++c2;
                mark[c1++] = -1;
            }
        }
        if (!trains.empty())
        //如果剩余的还有火车,说明顺序不对,无法完成对应操作
            printf("No.\n");
        else {
            printf("Yes.\n");
            for (int i = 0; i<c1; ++i) {
                if (mark[i] == 1)
                    printf("in\n");
                else if (mark[i] == -1)
                    printf("out\n");
                else 
                    break;
            }
        }
        printf("FINISH\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shuitiangong/p/12210444.html