[Water]Hdu 1022 Train Problem I

声明一个char类型的栈。

假设题目输入内容为n(int),a(string),b(string),a、b的下标记为index1和indexe2。

第一步肯定是入栈。

然后遍历b中每个字符:
  如果栈不为空且栈顶元素等于b[index2],出栈。否则a[index1]入栈,如果没有元素可入栈,输出No。

#include <iostream>
#include <string>
#include <cstring>
#include <stack>
#include <queue>

using namespace std;

int n;
string a,b;
string ans;

stack<char>s;
queue<int>q;

int main(){
	while(cin>>n>>a>>b){

		int index1=1,index2=0;
		bool ok=true;

		while(!q.empty())q.pop();
		while(!s.empty())s.pop();

		q.push(1);
		s.push(a[0]);

		while(index2<n){
			if(s.empty()){
				if(index1==n){
					ok=false;
					break;
				}

				q.push(1);
				s.push(a[index1]);
				index1++;
			}
			if(s.top()==b[index2]){
				q.push(0);
				s.pop();
				index2++;
			}else{
				if(index1==n){
					ok=false;
					break;
				}

				q.push(1);
				s.push(a[index1]);
				index1++;
			}
		}

		if(ok){
			cout<<"Yes."<<endl;
			while(!q.empty()){
				if(q.front()==1)cout<<"in"<<endl;
				else cout<<"out"<<endl;
				q.pop();
			}
		}
		else cout<<"No."<<endl;
		cout<<"FINISH"<<endl;
	}
}

  

posted @ 2015-06-03 18:46  纸牌  阅读(101)  评论(0编辑  收藏  举报