[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;
	}
}

  

时间: 2024-12-12 12:28:32

[Water]Hdu 1022 Train Problem I的相关文章

HDU 1022 Train Problem I 模拟栈题解

火车进站,模拟一个栈的操作,额外的栈操作,查看是否能按照规定顺序出栈. 数据量很少,故此题目很容易AC. 直接使用数组模拟就好. #include <stdio.h> const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N]; bool rs[MAX_N<<2]; int n; int main() { while (scanf("%d", &n) != EOF) { s

HDU 1022 Train Problem I (数据结构 —— 栈)

Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes

HDU 1022 Train Problem I(栈的操作规则)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 45375    Accepted Submission(s): 16966 Problem Description As the new term come

HDU 1022 Train Problem I (STL 栈模拟)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30420    Accepted Submission(s): 11492 Problem Description As the new term comes, the Ignatius Train Station is very busy nowaday

hdu 1022 Train Problem

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30588    Accepted Submission(s): 11561 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays

hdu 1022 Train Problem I(栈的应用+STL)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20521    Accepted Submission(s): 7712 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays

HDU 1022.Train Problem I【栈的应用】【8月19】

Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^).

HDU 1022 Train Problem I

描述As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there i

hdu 1022 Train Problem I(stack)

用数组模拟栈 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char str1[30],str2[30]; int sta[30]; int main() { int n; int i,j,k; int ans[100]; while(scanf("%d",&n)!=EOF) { scan