码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1022 Train Problem I(栈)

时间:2015-02-05 20:11:50      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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

int f[100005];
/*
    栈的应用
    若in[i]==out[j]则先进栈后立即出栈,此时只需i++,j++;
    若in[i]!=out[j]检查栈首是否为out[j],若是,则出栈,并j++,否则将in[i]入栈,并j++
    i==n时,若栈不为空则说明任有火车未出栈,检查栈内元素==out[j++]
   f[i]: 1表示进栈,0表示出栈 。。。。期末考试时不会做,遂记之
*/ int main() { freopen("d:\\in.txt", "r", stdin); freopen("d:\\out.txt", "w", stdout); string in, out; int n; while(cin>>n) { int cur=0; cin>>in>>out; bool flag=true; int i=0, j=0; stack<char>s; while(i<n) { if(in[i]==out[j]){ i++; j++; f[cur++]=1; f[cur++]=0; }else if(!s.empty()){ char first=s.top(); if(first==out[j]){ j++; s.pop(); f[cur++]=0; }else{ s.push(in[i++]); f[cur++]=1; } }else { s.push(in[i++]); f[cur++]=1; } } while(!s.empty()){ char first=s.top(); f[cur++]=0; s.pop(); if(first != out[j++]){ flag=false; break; } } if(flag){ cout<<"Yes."<<endl; for(int i=0; i<cur; i++){ if(f[i]==1)cout<<"in"<<endl; else cout<<"out"<<endl; } cout<<"FINISH"<<endl; } else { cout<<"No.\nFINISH"<<endl; } } return 0; }

 

hdu 1022 Train Problem I(栈)

标签:

原文地址:http://www.cnblogs.com/vegg117/p/4275795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!