心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
很经典的题目吧,我把代码贴上来了。
考察队列和栈的基本操作,记得《算法艺术》上有讲解。
以下是我的代码:
#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;
}
posted on 2011-02-23 09:30 lee1r 阅读(295) 评论(0)  编辑 收藏 引用 所属分类: 题目分类:数据结构

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理