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 ...
随机推荐
- 解决MacOS升级后出现xcrun: error: invalid active developer path, missing xcrun的问题
升级了系统 命令行不能用了 xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), mis ...
- 【sql server常用操作{增删改查}】
use DB_x go drop database DB_y create database DB_y --创建数据库 on primary --指定主数据文件 ( name= ...
- 「PHP」观察者模式模式
引言 所属:行为型模式,常用设计模式之一 学习资料: <大话设计模式>程杰 模式概述 观察者模式定义了一种一对多的依赖关系,让多个观察者对象监听某一个主题对象.这个主题 ...
- 泛型List集合转化为DateTable
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; name ...
- Flink+Kafka整合的实例
Flink+Kafka整合实例 1.使用工具Intellig IDEA新建一个maven项目,为项目命名为kafka01. 2.我的pom.xml文件配置如下. <?xml version=&q ...
- hadoop的自定义分组实现 (Partition机制)
hadoop开发中我们会遇到类似这样的问题,比如 如何将不同省份的手机号分别输出到不同的文件中,本片文章将对hadoop内置的Partition类进行重写以解决这个问题. MapReduce的使用者通 ...
- 【NXP开发板应用—智能插排】2.初步解析example之GPI
[前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...
- python学习——面对对象进阶
一.isinstance和issubclass isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo: pass a = Foo() print(isins ...
- Old ST-LINK firmware detected.do you want to upgrade it?已解决,stlink升级
错误提示: 原因: stlink固件太旧了, 解决办法: 升级stlink固件,升级方法: 1. 下载st link utility工具. 官网下载:https://www.st.com/conten ...
- HCA数据下载
HCA data downloads HCA data downloads PeRl` 还记得去年看的时候还是什么都没有,今年已经有数据可以下载了.