HDU1022 Train Problem I

本文介绍了一个使用栈数据结构来模拟字符串输入输出操作的过程。通过分析输入字符串和输出字符串,确定字符的正确进出栈顺序,并验证是否能通过栈操作实现从输入到输出的转换。该程序实现了栈的基本操作,并通过一系列指令输出了详细的栈操作步骤。

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

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1022

None.gif #include < iostream >
None.gif#include
< string >
None.gif#include
< stack >
None.gif#include
< vector >
None.gif
using namespace std;
None.gif
None.gif
string strIn,strOut;
None.gifstack
< char > strTmp; // 临时栈
None.gif
vector < string > strInfo;
None.gif
None.gif
int main( int argc, char * argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
intn,i,curPos;
InBlock.gif
charch;
InBlock.gif
while(cin>>n)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcin
>>strIn>>strOut;
InBlock.gifcurPos
=0;//out指针
InBlock.gif
while(!strTmp.empty())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifstrTmp.pop();
ExpandedSubBlockEnd.gif}

InBlock.gif
while(strInfo.size()!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifstrInfo.pop_back();
ExpandedSubBlockEnd.gif}

InBlock.gif
for(i=0;i<strIn.length();++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(strTmp.empty()==false)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//栈不空
InBlock.gif
ch=strTmp.top();
InBlock.gif
while(strOut[curPos]==ch&&(strTmp.empty()==false))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//相等,出栈
InBlock.gif
strTmp.pop();
InBlock.gifstrInfo.push_back(
"out");
InBlock.gifcurPos
++;//当前指针后移
InBlock.gif
if(strTmp.empty())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gifch
=strTmp.top();
ExpandedSubBlockEnd.gif}

InBlock.gifstrTmp.push(strIn[i]);
InBlock.gifstrInfo.push_back(
"in");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//栈空
InBlock.gif
strTmp.push(strIn[i]);//入栈
InBlock.gif
strInfo.push_back("in");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
while(!strTmp.empty())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifch
=strTmp.top();
InBlock.gifstrInfo.push_back(
"out");
InBlock.gif
if(ch!=strOut[curPos])
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcout
<<"No."<<endl;
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gifstrTmp.pop();
InBlock.gifcurPos
++;
ExpandedSubBlockEnd.gif}

InBlock.gif
if(strTmp.empty())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcout
<<"Yes."<<endl;
InBlock.gif
for(i=0;i<strInfo.size();++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifcout
<<strInfo[i]<<endl;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifcout
<<"FINISH"<<endl;
ExpandedSubBlockEnd.gif}

InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值