标签:
#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; }
标签:
原文地址:http://www.cnblogs.com/vegg117/p/4275795.html