模拟算法+栈 HDU 1022
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
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.



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.
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.
3 123 312
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的更多相关文章
- HDU 1022 Train Problem I 模拟栈题解
火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...
- HDU - 1022 Train Problem I STL 压栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1022 Train Problem I
A - Train Problem I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- Tarjan系列算法总结(hdu 1827,4612,4587,4005)
tarjan一直是我看了头大的问题,省选之前还是得好好系统的学习一下.我按照不同的算法在hdu上选题练习了一下,至少还是有了初步的认识.tarjan嘛,就是维护一个dfsnum[]和一个low[],在 ...
- JavaScript数据结构与算法-栈练习
栈的实现 // 栈类 function Stack () { this.dataStore = []; this.top = 0; // 栈顶位置 相当于length,不是索引. this.push ...
- javascript数据结构与算法---栈
javascript数据结构与算法---栈 在上一遍博客介绍了下列表,列表是最简单的一种结构,但是如果要处理一些比较复杂的结构,列表显得太简陋了,所以我们需要某种和列表类似但是更复杂的数据结构---栈 ...
- Python模拟入栈出栈操作
目标: 1.编写菜单,提示用户操作选项(push,pop,view,quit) 2.规则:定义列表,先入栈,后出栈,后入栈,先出栈 1.模拟入栈.出栈操作 >>> list1 = [ ...
- 简单用数组模拟顺序栈(c++版)适合新手
**栈是一种操作受限制的线性表,太多官方的话我也不说了,我们都知道栈元素是先进后出的,它有两种存储结构,分别是顺序存储结构和链式存储结构. **今天我先记一下顺序存储结构,后面我会加上链式存储结构的. ...
- 简单用数组模拟顺序栈(c++)
**栈是一种操作受限制的线性表,太多官方的话我也不说了,我们都知道栈元素是先进后出的,它有两种存储结构,分别是顺序存储结构和链式存储结构. **今天我先记一下顺序存储结构,后面我会加上链式存储结构的. ...
随机推荐
- Python3 动态导入模块的两种方式
动态导入模块就是只知道str类型的模块名字符串,通过这个字符串导入模块 需要导入的模块: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:C ...
- 64_p8
python2-cotyledon-tests-1.6.7-2.fc26.noarch.rpm 12-Feb-2017 10:28 23182 python2-couchdb-1.0-6.fc26.n ...
- SipDroid +miniSIPServer搭建SIP局域网语音通话(一)
最近在做语音通讯功能,参考下优秀开源软件SIPDroid好就这个了,svn check下最新的源代码 http://sipdroid.googlecode.com/svn/trunk/sipdroid ...
- GitBash、EGit、SourceTree三个Git管理工具对比
Git管理工具对比(GitBash.EGit.SourceTree) GitBash是采用命令行的方式对版本进行管理,功能最为灵活强大,但是由于需要手动输入希望修改的文件名,所以相对繁琐. EGit是 ...
- CentOS 7.1使用yum安装MySql5.6.24
http://www.cnblogs.com/yuanfeiblog/p/5276492.html
- 根据名字杀死进程Killall
Killall命令可以用来给一个特定的进程发送一个信号.这个信号默认情况下是SIGTERM,但也可以由killall命令使用参数来指定其它信号.现在让我们通过一些实际的例子来看看这个命令的实际用法. ...
- LeetCode862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- 简单优化:Zipalign
Android SDK中包含一个“zipalign”的工具,它能够对打包的应用程序进行优化.在你的应用程序上运行zipalign,使得在运行时Android与应用程序间的交互更加有效率.因此,这种方式 ...
- Python 一些 实用的包(持续更新)
line_profiler:(代码性能分析) 使用方法:链接 codecs:(Python内置的编码库) 数据分析与挖掘领域: 引自博客:这里 因为他有很多这个领域相关的库可以用,而且很好用, ...
- 【Mac】appium的环境搭建
1.下载appium并安装,进入官网下载即可 http://appium.io 2.下载安装pip,因为pip执行命令的安装,会出现某些包的下载失败,因此使用brew进行 https://pypi.o ...