首页 > 其他 > 详细

[Water]Hdu 1022 Train Problem I

时间:2015-06-03 19:33:48      阅读:131      评论:0      收藏:0      [点我收藏+]

声明一个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;
	}
}

  

[Water]Hdu 1022 Train Problem I

原文:http://www.cnblogs.com/bruce27/p/4549977.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!