题目链接: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. Spark Pipe使用方法(外部程序调用方法)

    写在前面: 1.我们使用的是Hadoop2.2.0,Spark 1.0. 2.这里使用的样例是经典的求pai程序来演示这个开发过程. 3.我们暂时使用java程序来开发,按照需要后面改用scala来开 ...

  2. 【转】终于知道为什么我的mysql总是卸载的不干净以及老是找不到my.ini文件

    感谢博主: http://blog.sina.com.cn/s/blog_6fc5bfa90100qmr9.html 如果你的电脑里装过MySQL,想再重新安装MySQL的时候可能就会因为前一版本卸载 ...

  3. 导入excel数据

    前提条件:先要安装好EXCEL软件. 程序中经常要用到导入excel数据的功能.其实通过ole操作excel就简单的几行代码,但记性不好,经常要用经常要找, 还是作篇笔记吧. var ExcelApp ...

  4. Javascript 高阶函数等

    高阶函数 函数可以接受另一个函数作为参数 称为 高阶函数. map : arr.map(pow); 数组.map(函数); reduce :arr.reduce(function(){ }); 数组. ...

  5. HDU 2517 棋盘分割

    题意:n刀切割棋盘 下面是8*8的棋盘,每个数字代表棋盘对应点的权值,问切割n刀后,每一块的和  的均方差最小是多少 均方差的公式需要先化简: 由上式得,均方差最小 显然是要 Xi^2 最小 d[k] ...

  6. 解决div和父div不上对齐

    加一个vertical-align: top;就好了.原因就是inline-block会使元素向下对齐.这和padding-top,margin-top没有关系的.使用浮动就不会有这种情况了,当然会带 ...

  7. [Javascript] Fetch API

    fetch() does the same thing as XHR, but fetch return a promise. fetch('password.txt', { 'method': 'P ...

  8. 将View兑换Bitmap

    /** * 中间View保罗转化成Bitmap * */ private Bitmap saveViewBitmap(View view) { // get current view bitmap v ...

  9. iOS开发中一些常用的方法

    1.压缩图片 #pragma mark 处理图片 - (void)useImage:(UIImage *)image { NSLog(@"with-----%f heught-----%f& ...

  10. phpmailer,smtp发送邮件实例(转)

    一,用phpmailer发送邮件 查看复制打印? <?php   include "class.phpmailer.php";    //包函邮件发送类      //邮件发 ...