题目链接: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. HW1.3

    public class Solution { public static void main(String[] args) { System.out.println(" J A V V A ...

  2. Casio普通计算器编程

    用xelatex写了个奇怪的东西……欢乐向 PDF  http://files.cnblogs.com/htfy/calc.pdf TEX http://files.cnblogs.com/htfy/ ...

  3. JavaScript删除数组重复元素的5个高效算法

    之前一段时间一直在准备面试, 因而博客太久没更新: 现在基本知识点都复习完毕, 接下来就分享下 面试的一些常见问题: 去正规的互联网公司笔试.面试有很大的概率会碰到 使用javascript实现数组去 ...

  4. hdoj 2802 F(N)【递推 规律】

    F(N) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. 对比iOS网络组件:AFNetworking VS ASIHTTPRequest(转载)

    在开发iOS应用过程中,如何高效的与服务端API进行数据交换,是一个常见问题.一般开发者都会选择一个第三方的网络组件作为服务,以提高开发效率和稳定性.这些组件把复杂的网络底层操作封装成友好的类和方法, ...

  6. Node.js学习(14)----EJS模板引擎

    这个入门教程将从以下几个方面来讲解: 1. 引入EJS 2. 创建一个模板 3. 使用视图工具组件 4. 使用错误处理组件 5. 什么情况下应使用EJS 引入EJS 在我们正式开始前,我们先来做点准备 ...

  7. Linux守护进程的编程实现

    Linux 守护进程的编程方法 守护进程(Daemon)是执行在后台的一种特殊进程.它独立于控制终端而且周期性地执行某种任务或等待处理某些发生的事件.守护进程是一种非常实用的进程.Linux的大多数s ...

  8. ODB 下载与安装 (Linux)

    http://www.codesynthesis.com/products/odb/download.xhtml Installing ODB on UNIX Introduction This gu ...

  9. 在不同平台上CocosDenshion所支持的音频格式

    在大多数平台上,cocos2d-x调用不同的SDK API来播放背景音乐和音效.CocosDenshion在同一时间只能播放一首背景音乐,但是能同时播放多个音效. 背景音乐 Platform supp ...

  10. 开启URL伪静态的方法

    ## 开启URL伪静态的方法.txt# 1. 请确认您服务器的类型. ThinkSNS的伪静态规则支持Apache.IIS.Nginx. 2. 请确认您的服务器支持URL Rewrite(可从服务器提 ...