Loading

HDU - 1022 Train Problem I 模拟 卡特兰数

给定一个火车的入站顺序,问是否可能以规定的顺序出站。

背景实际上是卡特兰数。

可以联想到用栈和队列来模拟这一过程。

如果进栈的过程中发现和队首相同就让他出栈。

string s1, s2;

int main() {
    int n;
    while (cin >> n) {
        cin >> s1 >> s2;
        stack<int> s;
        queue<int> q;
        vector<int> ans;
        for (int i = 0; i < n; i++) q.push(s2[i] - '0');
        for (int i = 0; i < n; i++) {
            s.push(s1[i] - '0');
            if (!q.empty() && q.front() == s.top()) {
                ans.push_back(0);
                while (!q.empty() && !s.empty() && q.front() == s.top())  ans.push_back(1), q.pop(), s.pop();
            }
            else ans.push_back(0);
        }
        if (!s.empty()) {
            puts("No.");
            puts("FINISH");
        }
        else {
            puts("Yes.");
            for (int i = 0; i < ans.size(); i++) if (ans[i]) puts("out"); else puts("in");
            puts("FINISH");
        }
    }
}

 

posted @ 2020-08-18 20:25  MQFLLY  阅读(131)  评论(0编辑  收藏  举报