题目链接: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. 【解决】Microsoft Visual Studio 2012 打开2008下编译的silverlight3项目

    最近因为项目需要,老师要我搞一发流程设计器,毫无头绪呀妈蛋 .. 我考虑是用silverlight呢还是jquery .. 上网找了找  .. 有一个用silverlight3写的 貌似IDE用的是V ...

  2. ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph

    ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091 题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1 ...

  3. $1200元 设计数据挖掘模型及对应RESTful Web Service

    利用MongoDB和NodeJS/Java设计数据挖掘模型以及对应的RESTful Web Service.要求:1. 精通MongoDB集群的配置2. 精通MongoDB Sehll的使用3. 精通 ...

  4. java 关于多态的一点总结

    http://www.cnblogs.com/wenruo/p/5352683.html 一直不是很理解多态,今天看了两遍<think in java>第八章,试着总结一下. 多态的本质就 ...

  5. 8-14-Exercise

    8-14-小练 这次是我这组出题......我出的是B.C.D[虽然本来是想出的很难......╮(╯▽╰)╭但是,没找到AC1000+同时又让我想出的难题......SO...我出的真的不难= =] ...

  6. Android UI--自定义ListView(实现下拉刷新+加载更多)

    Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...

  7. JS好的学习网站

    1.JavaScript 秘密花园 2.W3CSchool 3.js严格模式 4.js核心之原型 5.尼古拉斯怎么面试前端工程师 6.zepto中文版api 7.jquery API

  8. PAT 1034. Head of a Gang (30)

    题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1034 此题考查并查集的应用,要熟悉在合并的时候存储信息: #include <iostr ...

  9. Android Fragment 生命周期

    启动的事件触发顺序 F Fragmeent A Activity onAttach(F) onAttachFragment(A) onCreate(F) onCreateView(F) onActiv ...

  10. linux命令行模式下实现代理上网

    有些公司的局域网环境,例如我们公司的只允许使用代理上网,图形界面的很好解决就设置一下浏览器的代理就好了,但是linux纯命令行的界面就....下面简单几步就可以实现了! 一.命令行界面的一般代理设置方 ...