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;
}

你可能感兴趣的:(HDU)