题目链接: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. Kinect for Windows SDK开发入门(15):进阶指引 下

    Kinect for Windows SDK开发入门(十五):进阶指引 下 上一篇文章介绍了Kinect for Windows SDK进阶开发需要了解的一些内容,包括影像处理Coding4Fun K ...

  2. IOS 中runtime 不可变数组__NSArray0 和__NSArrayI

    IOS 中runtime 不可变数组__NSArray0 和__NSArrayI 大家可能都遇到过项目中不可变数组避免数组越界的处理:runtime,然而有时候并不能解决所有的问题,因为类簇不一样 # ...

  3. iOS 火星坐标相关整理及解决方案汇总(转)

    这几天在处理定位相关的代码,彻彻底底的被火星坐标恶心到了. 恶心列表 从 CLLocationManager 取出来的经纬度放到 mapView 上显示,是错的! 从 CLLocationManage ...

  4. MySQL启动和关闭服务命令

    MySQL启动和关闭服务命令 1.启动服务命令 net start mysql 2.关闭服务命令 net stop mysql

  5. PetShop 4.0学习笔记:消息队列MSMQ

    直到今天才知道,在我们每天都在用的Window系统里还有这么好用的一个编程组件:消息队列.它能够解决在大数据量交换的情况下的性能问题,特别是BS系统的数据库性能.而且它的异步处理方式能给程序员最大的便 ...

  6. Linux 的启动流程-阮一峰

    http://www.ruanyifeng.com/blog/2013/08/linux_boot_process.html

  7. WEB网站常见受攻击方式及解决办法

    一个网站建立以后,如果不注意安全方面的问题,很容易被人攻击,下面就讨论一下几种漏洞情况和防止攻击的办法. 一.跨站脚本攻击(XSS) 跨站脚本攻击(XSS,Cross-site scripting)是 ...

  8. PHP第四章数组2

    $str =array("dd"=>"d","dc"=>"ds","dd"=>&q ...

  9. 安卓UI美化_drawable

    UI美化 drawable资源 ------------------------------------------state drawable资源在不同状态下显示不同的图片在<selector ...

  10. linux下安装apache2.4

    linux安装Apache2步骤如下 apr 下载地址 http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gz 安装过程 tar -xzvf apr- ...