HDU1022 Train Problem I

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

#include <iostream>
#include 
<string>
#include 
<stack>
#include 
<vector>
using namespace std;

string strIn,strOut;
stack
<char> strTmp;//临时栈
vector<string> strInfo;

int main(int argc,char* argv[])
{
    
int n,i,curPos;
    
char ch;
    
while(cin>>n)
    
{
        cin
>>strIn>>strOut;
        curPos 
= 0;//out指针
        while(!strTmp.empty())
        
{
            strTmp.pop();
        }

        
while(strInfo.size()!=0)
        
{
            strInfo.pop_back();
        }

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

                    ch 
= strTmp.top();
                }

                strTmp.push(strIn[i]);
                strInfo.push_back(
"in");
            }

            
else
            
{//栈空
                    strTmp.push(strIn[i]);//入栈
                    strInfo.push_back("in");
            }

        }

        
while(!strTmp.empty())
        
{
            ch 
= strTmp.top();
            strInfo.push_back(
"out");
            
if(ch!=strOut[curPos])
            
{
                cout
<<"No."<<endl;
                
break;
            }

            strTmp.pop();
            curPos
++;
        }

        
if(strTmp.empty())
        
{
            cout
<<"Yes."<<endl;
            
for(i=0;i<strInfo.size();++i)
            
{
                cout
<<strInfo[i]<<endl;
            }

        }

        cout
<<"FINISH"<<endl;
    }

    
return 0;
}

posted on   Phinecos(洞庭散人)  阅读(1115)  评论(2编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库

导航

统计

点击右上角即可分享
微信分享提示