Train Problem I

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

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.".
题目大概意思:有N辆火车,以序列1方式进站,判断是否能以序列2方式出栈。进站不一定是一次性进入,也就是说中途可以出站。
 /*模拟方法:把序列一入栈,并与序列二的进行比较,判断是否应该后移指针*/
#include<iostream>
#include<cstdio>
using namespace std;
#include<cstring>
#include<stack>
#define N 10001
char str1[N],str2[N];
int n;
int k=,result[N];//储存结果
stack<char>sta;
int main()
{
while(scanf("%d%s%s",&n,str1,str2)==)
{
int i=,j=;
k=;/*别忘了k的初始化*/
while(!sta.empty()) sta.pop();
sta.push(str1[]);//先把第一个入栈
result[++k]=;
while(i<n&&j<n)
{
// char mm=sta.top();/*不要这样写,如果sta是空,会报错*/
if(!sta.empty()&&sta.top()==str2[j])/*注意要把取栈顶放在后面,语句&&前面的不符合,后面的就不执行了,不会出错*/
{
sta.pop();
result[++k]=;/*出栈即为0*/
j++;
}
else
{
++i;/*不要用i=1,i++放到后面,因为我们判断i==n是结束条件,放在后面i==n时,实际上才到了n-1*/
sta.push(str1[i]);
result[++k]=;/*入栈即为1*/
//
}
}
if(i==n)/*如果正常匹配成功的话,最后一步是i==n-1,j==n,如果匹配失败,最后一步是i==n-1后再++*/
printf("No.\n");
else{
printf("Yes.\n");
for(int i=;i<=k;++i)
if(result[i])
printf("in\n");
else printf("out\n"); }
printf("FINISH\n");
memset(result,,sizeof(result));
memset(str1,,sizeof(str1));
memset(str2,,sizeof(str2)); }
return ;
}

模拟算法+栈 HDU 1022的更多相关文章

  1. HDU 1022 Train Problem I 模拟栈题解

    火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...

  2. HDU - 1022 Train Problem I STL 压栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDU 1022 Train Problem I

    A - Train Problem I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  4. Tarjan系列算法总结(hdu 1827,4612,4587,4005)

    tarjan一直是我看了头大的问题,省选之前还是得好好系统的学习一下.我按照不同的算法在hdu上选题练习了一下,至少还是有了初步的认识.tarjan嘛,就是维护一个dfsnum[]和一个low[],在 ...

  5. JavaScript数据结构与算法-栈练习

    栈的实现 // 栈类 function Stack () { this.dataStore = []; this.top = 0; // 栈顶位置 相当于length,不是索引. this.push ...

  6. javascript数据结构与算法---栈

    javascript数据结构与算法---栈 在上一遍博客介绍了下列表,列表是最简单的一种结构,但是如果要处理一些比较复杂的结构,列表显得太简陋了,所以我们需要某种和列表类似但是更复杂的数据结构---栈 ...

  7. Python模拟入栈出栈操作

    目标: 1.编写菜单,提示用户操作选项(push,pop,view,quit) 2.规则:定义列表,先入栈,后出栈,后入栈,先出栈 1.模拟入栈.出栈操作 >>> list1 = [ ...

  8. 简单用数组模拟顺序栈(c++版)适合新手

    **栈是一种操作受限制的线性表,太多官方的话我也不说了,我们都知道栈元素是先进后出的,它有两种存储结构,分别是顺序存储结构和链式存储结构. **今天我先记一下顺序存储结构,后面我会加上链式存储结构的. ...

  9. 简单用数组模拟顺序栈(c++)

    **栈是一种操作受限制的线性表,太多官方的话我也不说了,我们都知道栈元素是先进后出的,它有两种存储结构,分别是顺序存储结构和链式存储结构. **今天我先记一下顺序存储结构,后面我会加上链式存储结构的. ...

随机推荐

  1. [MySQL] gap lock/next-key lock浅析

    当InnoDB在判断行锁是否冲突的时候, 除了最基本的IS/IX/S/X锁的冲突判断意外, InnoDB还将锁细分为如下几种子类型: record lock (RK) 记录锁, 仅仅锁住索引记录的一行 ...

  2. 批量删除.svn文件夹和.svn文件

    新建可运行文件 Windows环境 将下面的代码保存为 kill-svn.bat文件,放到要删除.svn文件的目录下,双击运行即可 @echo on @rem 删除SVN版本控制目录 @rem for ...

  3. 模板为webpack的目录结构

    目录结构 | -- build // 项目构建(webpack)相关代码 | |-- build.js // 生产环境构建代码 | |-- check-version.js // 检查node.npm ...

  4. C语言地址对齐(转)--网络编程之结构体大小的计算

    什么是地址对齐? 现代计算机中内存空间都是按照字节(byte)划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型数 ...

  5. 进程一些命令pstree,ps,pstack,top

    1. pstree pstree以树结构显示进程$ pstree -p work | grep adsshd(22669)---bash(22670)---ad_preprocess(4551)-+- ...

  6. HDU 1079 Calendar Game(博弈找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079 题目大意:给你一个日期(包含年月日),这里我表示成year,month,day,两人轮流操作,每 ...

  7. 【数据挖掘基础算法】KNN最近邻分类算法

    算法简介: 通过计算待预测样本和已知分类号的训练样本之间的距离来判断该样本属于某个已知分类号的概率.并选取概率最大的分类号来作为待预测样本的分类号 懒惰分类算法,其模型的建立直到待预测实例进行预测时才 ...

  8. IEEEXtreme 10.0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

  9. list 往指定的下标插入元素

    list 往指定的下标插入元素 import java.util.*; public class ListExample{ public static void main(String[] args) ...

  10. jquery控制元素的显示与隐藏

    比如要控制div的显示与隐藏,一句话就搞定了.$("#id").show()表示display:block,$("#id").hide()表示display:n ...