Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 46887    Accepted Submission(s): 17593

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 a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
 
Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
 
Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 
Sample Input
3 123 321
3 123 312
 
Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

Hint

Hint

For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".

 
 
栈的应用,就根据栈先进后出的这一特点,依次判断每一个栈顶的元素,看是不是符合给定的出栈顺序

#include<iostream>
#include<stack>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int n;
char a[],b[];
int c[];
while(cin>>n)
{
cin>>a;
cin>>b;
stack<char>s;
while(!s.empty())
s.pop();
int j=,num=;
s.push(a[]);
c[j++]=;
for(int i=;i<n;i++)
{
if(s.empty())
{
s.push(a[num+]);
num++;
c[j++]=;
}
while(s.top()!=b[i]&&num<n-)
{
s.push(a[num+]);
num++;
c[j++]=;
}
if(s.top()==b[i])
{
c[j++]=-;
s.pop();
} }
if(s.empty())
{
cout<<"Yes."<<endl;
for(int i=;i<j;i++)
{
if(c[i]==)
cout<<"in"<<endl;
if(c[i]==-)
cout<<"out"<<endl;
}
}
else
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
}
}

,按照这个顺序,如果栈顶的符合,则出栈,如果不符合,继续进栈,直到找到符合的(应注意的是,时刻保持栈内有元素,否则访问栈顶的时候会出现,访问越界的错误)

hdu1022 模拟栈的更多相关文章

  1. ACM/ICPC 之 用双向链表 or 模拟栈 解“栈混洗”问题-火车调度(TSH OJ - Train)

    本篇用双向链表和模拟栈混洗过程两种解答方式具体解答“栈混洗”的应用问题 有关栈混洗的定义和解释在此篇:手记-栈与队列相关 列车调度(Train) 描述 某列车调度站的铁道联接结构如Figure 1所示 ...

  2. java 16 - 5 LinkedList模拟栈数据结构的集合

    请用LinkedList模拟栈数据结构的集合,并测试 题目的意思是: 你自己的定义一个集合类,在这个集合类内部可以使用LinkedList模拟. package cn_LinkedList; impo ...

  3. hdu 4699 Editor 模拟栈

    思路:刚开始用STL中的栈,一直RE……,之后改为手动模拟栈操作,在注意点细节就可以了!!! 代码如下: #include<cstdio> #include<cstring> ...

  4. 【DataStructure In Python】Python模拟栈和队列

    用Python模拟栈和队列主要是利用List,当然也可以使用collection的deque.以下内容为栈: #! /usr/bin/env python # DataStructure Stack ...

  5. 第一回写的用arraylist模拟栈操作

    package hashMap; import java.util.ArrayList; import d.Student; /** * 用ArrayList模拟栈操作 * @author zhuji ...

  6. HDOJ/HDU 1022 Train Problem I(模拟栈)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  7. c语言学习,模拟栈操作

    1.stack.c模拟栈操作函数的实现 #include<stdio.h> #include<stdlib.h> ; static char *stack;//数据栈 ;//栈 ...

  8. KEILC51可重入函数及模拟栈浅析

    MARK:文章中的红色部分是个人的理解. KEILC51可重入函数及模拟栈浅析 关键字:keilc51,模拟堆栈,可重入函数调用,参数传递,C?XBP,C?ADDXBP 摘要:本文较详细的介绍了kei ...

  9. 使用golang的slice来模拟栈

    slice(切片):底层数据结构是数组 stack(栈):一种先进后出的数据结构 普通版的模拟写入和读取的栈 package main import "fmt" //栈的特点是先进 ...

随机推荐

  1. javascript nonono

    javascript实现的点击元素实现当前轮换展现效果 javascript实现的点击元素实现当前轮换展现效果:本章节分享一段代码实例,它实现了点击元素实现当前展现效果,并且具有比较人性化的变换效果. ...

  2. Part4_lesson1---Bootloader设计蓝图

    1.bootloader的作用 2.u-boot是bootloader业界的老大 u-boot分为自主模式和开发模式 3.建立U-boot工程 uboot不能在window下面进行解压,因为在wind ...

  3. VMware下Ubuntu虚拟机NAT模式 连接Xshell

    1.Edit->Virtual NetWork Editor 2.选择NAT->NAT Settings 配置端口映射关系 很重要! Host port: 真实主机 一般是Windows端 ...

  4. [GO]数组做函数参数

    package main import "fmt" //数组为函数参数,实际上是值传递//实参数据里的每个元素,给形参数组拷贝一份//这里形参的数组其实就是实参的复制品 func ...

  5. Oracle数据库之日期函数

    今天给大家介绍一下oracle数据中的日期函数的用法.废话不多说,我们看一下oracle给我们提供了那些函数? 1.sysdate 用途:获取当前系统时间. 2.to_date('字符类型','日期类 ...

  6. Java Thread.join()详解--父线程等待子线程结束后再结束

    目录(?)[+] 阅读目录 一.使用方式. 二.为什么要用join()方法 三.join方法的作用 join 四.用实例来理解 打印结果: 打印结果: 五.从源码看join()方法   join是Th ...

  7. <%@ include > 与< jsp:include >

    include指令表示在JSP编译时插入一个包含文本或者代码的文件,把文件中的文本静态地包含过去.也就是说,会把被包含的页面拷贝到包含的页面中指令所在的位置. 语法格式:<%@ include ...

  8. 使用Jenkins远程部署war包到tomcat container

    Jenkins首先使用maven将源代码进行编译打包,之后需要将war包传送到tomcat服务器上进行部署. 来看一下Jenkins的基本配置,首先需要安装插件"Deploy to cont ...

  9. wp7启动+幻灯片效果

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Wi ...

  10. poj2154(polya定理+欧拉函数)

    题目链接:http://poj.org/problem?id=2154 题意:n 种颜色的珠子构成一个长为 n 的环,每种颜色珠子个数无限,也不一定要用上所有颜色,旋转可以得到状态只算一种,问有多少种 ...