hdu 1022 Train Problem I (栈的应用)

Sample Input
  
  
3 123 321 3 123 312
 

Sample Output
 
 
Yes. in in in out out out FINISH No. FINISH 考察栈的应用,给定序列str1和str2,问你能否在str1的基础上只进行入栈 出栈得到str2 用stl水就可以了 但是我水的很艰难啊大哭 各种WA 直接上代码吧:
#include <cstdio>
#include <vector>
#include <stack>
#include <string>
#include <iostream>
#include <algorithm>
#define MAXN 10010
#define ll long long
using namespace std;
vector<string> ans;
int main(void) {
    int n;
    string str1, str2;
    stack<char> st; 
    while(scanf("%d", &n) != EOF) {
        ans.clear();
        while(!st.empty())//用栈的时候千万记得清空栈
            st.pop();
        cin >> str1 >> str2;
        int len = str1.size();
        int i = 0;
        int j = 0;
        while(i<len) {
            st.push(str1[i++]);
            ans.push_back("in");
            while(!st.empty() && st.top() == str2[j]) {//每次要一直退栈到不能退为止
                st.pop();
                ans.push_back("out");
                ++j;
            }
        }

        while(j < len) {
            if(st.top() == str2[j]) {
                st.pop();
                ans.push_back("out");
            }
            ++j;
        }
        if(st.empty()) {
            cout << "Yes." << endl;
            for(int i=0; i<ans.size(); ++i) {
                cout << ans[i] << endl;
            }
        }
        else cout << "No." << endl;
        cout << "FINISH" << endl;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值