HDU 1022 Train Problem I

HDU 1022 Train Problem I
很经典的题目吧,我把代码贴上来了。
考察队列和栈的基本操作,记得《算法艺术》上有讲解。
以下是我的代码:
#include < iostream >
#include
< string >
#include
< queue >
#include
< stack >
#include
< cstdio >
using   namespace  std;

int  main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/
    
    
int  n;
    
while (cin >> n)
    {
        
string  order1,order2;
        cin
>> order1 >> order2;
        queue
< char >  q;
        
for ( int  i = 0 ;i < order1.size();i ++ )
            q.push(order1[i]);
        stack
< char >  s;
        
int  pos( 0 ),ans[ 100 ] = { 0 },ans_len( 0 );
        
bool  success = true ;
        
while ( ! q.empty())
        {
            
if ( ! s.empty() && s.top() == order2[pos])
            {
                s.pop();
                pos
++ ;
                ans[
++ ans_len] = 0 ;
            }
            
else
            {
                s.push(q.front());
                q.pop();
                ans[
++ ans_len] = 1 ;
            }
        }
        
while ( ! s.empty())
        {
            
if (s.top() == order2[pos])
            {
                s.pop();
                pos
++ ;
                ans[
++ ans_len] = 0 ;
            }
            
else
            {
                success
= false ;
                
break ;
            }
        }
        
        
if (success)
        {
            cout
<< " Yes. " << endl;
            
for ( int  i = 1 ;i <= ans_len;i ++ )
                
if (ans[i] == 1 )
                    cout
<< " in " << endl;
                
else  cout << " out " << endl;
        }
        
else  cout << " No. " << endl;
        cout
<< " FINISH " << endl;
    }
    
return   0 ;
}

你可能感兴趣的:(HDU 1022 Train Problem I)