Train Problem(栈的应用)
Description



Input
Output
Sample Input
Sample Output
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.". 题目意思:给了火车进站和出站的顺序,问是否匹配,同时也要给出进出站的步骤。 解题思路:火车进出站实际上也模拟了栈的使用,我们可以对比两个字符串,s1串为进站顺序,s2串为出站顺序,对s1中每一个字符(火车)
比较s2的出站顺序,不相同则是进站,相同就要出站,以此类推,用一个数组标记进站和出站即可。 上代码:
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
int n,i,j,k,flag[];
char s1[],s2[];
while(scanf("%d %s%s",&n,s1,s2)!=EOF)
{
stack<char>s;
memset(flag,-,sizeof(flag));
j=;
k=;
for(i=;i<n;i++)
{
s.push(s1[i]);
flag[k++]=;
while(!s.empty()&&s.top()==s2[j])
{
s.pop();
flag[k++]=;
j++;
}
}
if(j==n)
{
printf("Yes.\n");
for(i=;i<k;i++)
{
if(flag[i]==)
printf("in\n");///1为进栈
else
printf("out\n");///0为出栈 }
}
else
printf("No.\n");
printf("FINISH\n");
}
return ;
}
Train Problem(栈的应用)的更多相关文章
- Train Problem I(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Train Problem I--hdu1022(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- train problem I (栈水题)
杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...
- Hdu 1022 Train Problem I 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1022.Train Problem I【栈的应用】【8月19】
Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...
- 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(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022:Train Problem I(数据结构,栈,递归,dfs)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1022 Train Problem I(栈的操作规则)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1022 Train Problem I[给出两个长n的串,入栈和出栈顺序,判断入栈顺序是否可以匹配出栈顺序]
Train Problem I 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 As the new term comes, the Ignatius Train Sta ...
随机推荐
- Visual Studio中添加API断点
如:添加 PostMessageA 断点 {,,USER32.DLL}_PostMessageA@16 //判断为WM_CLOSE消息*(int*)(esp + 8) == 0x0010
- WebService 学习笔记(一、概念及定义)
定义 WebService是一种服务导向架构(SOA service-oriented architecture)的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作. Web ...
- 怎么将oracle的sql文件转换成mysql的sql文件
怎么将sql文件导入PowerDesigner中的方法(将oracle的sql文件转换成mysql的sql文件)呢? 怎么将xx.sql文件的数据库结构导入powerdesigner 的方法呢? 现讲 ...
- 虚拟机下linux 系统网卡配置、固定IP地址
1.进入该目录下修改内容 vi /etc/sysconfig/network-scripts/ ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static DEF ...
- Macbook下安装memcached
参考文献: https://blog.csdn.net/weixin_41827162/article/details/82049520 感谢大佬 安装memcached需要Homebrew 注意点: ...
- 让pandas的输出结果中显示全部数据
import pandas as pd pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) ...
- FIFO队列 ADT接口 链表实现
FIFO.h (接口) #include "Item.h" void QUEUinit(int); int QUEUempty(void); void QUEUput(Item); ...
- C指针(4)——数据结构中指针的应用(非常重要)
5-1动态内存分配,分配的是堆内存的空间 分配内存函数 (都集中在库函数 stdlib.h 中) void *malloc (unsigned int num_bytes); //指定分配内存空间大 ...
- (数据科学学习手札46)Scala中的面向对象
一.简介 在Scala看来,一切皆是对象,对象是Scala的核心,Scala面向对象涉及到class.object.构造器等,本文就将对class中的重点内容进行介绍: 二.Scala中的类 2.1 ...
- P1803 凌乱的yyy
P1803 凌乱的yyy 题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) ...