Hdu 1022 Train Problem I 程序参考

本文介绍了一个使用栈数据结构来模拟并验证字符串操作的过程。通过输入两个字符串s和t,程序将尝试用一系列的入栈(in)和出栈(out)操作,使得栈顶元素与字符串t匹配。如果成功,则输出Yes及操作序列;反之则输出No。此程序适用于算法学习和栈操作的实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://acm.hdu.edu.cn/showproblem.php?pid=1022

#pragma warning(disable : 4786)

#include <iostream>

#include <stack>

#include <string>

#include <vector>

using namespace std;



bool run(int now)

{

    int n;

    if (!(cin>>n)) return false;



    string s,t;

    cin>>s>>t;

    stack<int> st;

    vector <string> op;



    int i=0, j=0;

 

    while(i<=n && j<n)

    {

        if (st.empty()==true || st.top()!=t[j])

        {

			st.push(s[i]);

			op.push_back("in");

			i++;

		}

		else

		{

			st.pop();

			op.push_back("out");

			j++;

		}

    }



    if (st.empty()==true)

    {

        cout << "Yes."<<endl;



        for(i=0;i<op.size();i++)

		{

            cout<<op[i]<<endl; 

		}

    }

    else

	{

        cout << "No."<<endl;

	}



    cout << "FINISH"<<endl;



    return true;

}

 

int main()

{    

    int now=1;

    while(run(now++));

    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值