题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1022

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 Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH Hint
Hint For the first Sample Input, we let train get in, then train and train .
So now train is at the top of the railway, so train can leave first, then train and train .
In the second Sample input, we should let train leave first, so we have to let train get in, then train and train .
Now we can let train 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.".

题意:给出火车在进站口的顺序,火车进站,出站遵循先进后出的规律问能实现出站顺序如给出的顺序吗?能输出步骤,不能输出No

方法:用数组v[i]表示进站的第i个火车是v[i],用vis[i]表示火车i是否进站

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define N 30
#define ll long long
#define met(a,b) memset(a,b,sizeof(a));
vector<vector<int> >Q;
int vis[N],v[N];
char dis[][]= {"in","out"};
int ans[N];
int a[N],b[N];
int main()
{
int n;
char s[N],sub[N];
while(scanf("%d %s %s",&n,s,sub)!=EOF)
{
int f=;met(v,);
for(int i=; i<n; i++)
a[i]=s[i]-'';
for(int i=; i<n; i++)
b[i]=sub[i]-'';
int k=,l=;
for(int i=; i<n; i++)
{
if(!v[b[i]])///如果没进站,则把这辆火车前的火车进站
{
for(int j=; j<n; j++)
{
if(a[j]==b[i])
{
ans[l++]=;
ans[l++]=;
v[a[j]]=;
break;
}
if(!v[a[j]])
{
vis[k++]=a[j];
v[a[j]]=;
ans[l++]=;
}
}
}
else ///已进站
{
if(vis[--k]==b[i])///判断是否在最上面
{
ans[l++]=;
}
else
f=;
} }
if(f)
printf("No.\nFINISH\n");
else
{
printf("Yes.\n");
for(int i=;i<l;i++)
puts(dis[ans[i]]);
printf("FINISH\n");
}
}
return ;
}

(hdu)1022 Train Problem I 火车进站问题的更多相关文章

  1. HDU 1022 Train Problem I(栈的操作规则)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...

  2. HDU 1022 Train Problem I

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

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

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

  4. HDU - 1022 Train Problem I STL 压栈

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

  5. hdu 1022 Train Problem I【模拟出入栈】

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

  6. Hdu 1022 Train Problem I 栈

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

  7. 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 ...

  8. hdu 1022 Train Problem I(栈的应用+STL)

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

  9. hdu 1022:Train Problem I(数据结构,栈,递归,dfs)

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

随机推荐

  1. HDU2196 - Computer(树形DP)

    题目大意 给定一颗n个结点的树,编号为1~n,要求你求出每个结点能到达的最长路径 题解 用动态规划解决的~~~~把1 当成树根,这样就转换成有根树了.我们可以发现,对于每个结点的最长路,要么是从子树得 ...

  2. Script.NET Perl解释器代码已经在GitHub开源发布

    Script.NET Perl解释器的代码已经提交到GitHub网站.GitHub项目地址: https://github.com/blueantst/Script.NET Perl解释器代码在Src ...

  3. 安装、配置JDK的步骤

    1.配置环境变量,打开我的电脑--属性--高级--环境变量,新建系统变量JAVA_HOME .变量值:jdk的目录,比如d:/java.选择“系统变量”中变量名为“Path”的环境变量双击该变量,把J ...

  4. C# 读取 timestamp 时间戳 值为byte[] 类型时转换为时间戳字符串

    C# 中如何读取出来的时间戳为 byte[] 类型的话,用以下方式转换成 16进制字符串 string tmpUfts = "0x"+ BitConverter.ToString( ...

  5. C# LINQ详解(一)

    原文标题:How does it work in C#?-Part 3 (C# LINQ in detail),作者:Mohammand A Rahman. 目录 LINQ 基础 扩展方法-幕后的工作 ...

  6. ListBox获取行字符串

    ListBox获取行字符串 关键点 获取ListBox第1行的字符串 获取ListBox第2行的字符串 获取ListBox第n行的字符串 CListBox::GetText int GetText( ...

  7. 5 Things They Never Tell You About Making iPhone Apps

    http://blog.teamtreehouse.com/5-things-they-never-tell-you-about-making-iphone-apps So, you've decid ...

  8. phpcms 源码分析一: common.inc.php

      其实就是从网上找到的的逆雪寒的分析, 我下来之后发现格式和错字的问题,非常影响阅读,现在我就是做了下搬运工的角色, 同时将格式调整到可读性提高点而已,让各位看官稍微舒心点: 下面进入整体: < ...

  9. Struts,Spring,Hibernate的作用

    Spring的作用: 在SSH框假中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因 为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL ...

  10. java学习笔记--1_常见输入输出语句熟悉篇章

    今天上oj,想来是准备做做算法和数据结构的.看了看以前做的基础题目,想着就先熟悉一下java的语言吧! 以下是今天做的10道题目. 备注:oj有时候对格式要求非常严格,因为不在格式上纠结太久,只要ec ...