Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 20521    Accepted Submission(s): 7712

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

栈的水题一道。就直接对进出栈进行模拟。和之前做的那道题类似,用来练练STL还是不错的,直接用STL水过。

http://blog.csdn.net/whjkm/article/details/38455291 能够看看之前的那道题。

用来vector 和 stack容器。用vector来存储字符串;

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main()
{
int n,i,j;
string str1,str2;
while(cin>>n>>str1>>str2)
{
stack<int>s;
vector<string>v;
for(i=0,j=0;i<n&&j<n;i++)
{
s.push(str1[i]);//把元素进栈
v.push_back("in");//压进vector容器
while(s.top()==str2[j])//进行匹配
{
if(!s.empty())
{
s.pop();
v.push_back("out");
}
j++;
if(s.empty()) break;
}
}
if(j==n)
{
printf("Yes.\n");
for(i=0; i< v.size();i++)
cout<< v[i] <<endl;
printf("FINISH\n");
}
else
printf("No.\nFINISH\n");
}
return 0;
}



hdu 1022 Train Problem I(栈的应用+STL)的更多相关文章

  1. Hdu 1022 Train Problem I 栈

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

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

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

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

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

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

  5. HDU - 1022 Train Problem I STL 压栈

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

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

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

  7. HDU 1022 Train Problem I[给出两个长n的串,入栈和出栈顺序,判断入栈顺序是否可以匹配出栈顺序]

    Train Problem I 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 As the new term comes, the Ignatius Train Sta ...

  8. HDU 1022 Train Problem I

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

  9. hdu 1022 Train Problem

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

随机推荐

  1. PHP语言的优势?

    稳定性: 毫无疑问,PHP已经是目前互联网服务端使用最广泛的编程语言之一,目前PHP在互联网应用领域的占有率位居全球第一.试问,如果本身不够成熟和稳定,如何能占有如此大的市场呢? 易用性: 简单实用, ...

  2. g++ 链接*.a静态库 方法

    g++在链接*.a静态库时,直接作为object使用,不需要使用-l ,但是需要注意调整顺序,被依赖的文件放在后面. 如: g++  -g  -o0 -Wno-deprecated  -I$(INCL ...

  3. Eclipse maven构建springmvc项目

    原文地址: http://www.cnblogs.com/fangjins/archive/2012/05/06/2485459.html 一.背景介绍 对于初学者,用maven构建项目并不是一件容易 ...

  4. 用 Nokitjs 解决前端开发中的跨域问题

    问题 在开发一些「单页应用」时,通常会使用 Ajax 和服务器通讯,比如 RESTful API,通常「前端」和「服务端 API」可能是有不同人员在负责,也不在同一个工程下,那么开发过程中就可能会遇到 ...

  5. OpenShift负载分区策略(Router Shading)

    在很多场景下,单靠几个在Infra节点上的Router进行服务请求的转发是不够的,项目中很多时候都有流量隔离的需求,主要场景在于: 一个集群中的不同的环境的流量隔离需求,比如开发走几个Router,生 ...

  6. 惠普HP compaq康柏系列 CQ40笔记本电脑拆机除尘

    工具:两用螺丝刀(一字口的拆CPU,十字口的拆其它所有螺丝)    散热硅胶和CPU上的散热贴 正面照(A面) 反面照(D面) 第一步:拆掉电池,不要忘记了红圈这里的两颗螺丝.共6颗小螺丝. 第二步: ...

  7. C/C++ 中头文件相互包含引发的问题

    转自:http://blog.csdn.net/hazir/article/details/38600419 今天下午遇到一个头文件相互包含而导致的编译问题,花了我不少时间去调试没找到问题,最后晚上跟 ...

  8. Orchard运用 - 学习资源分享

    这段时间研究Orchard,看到一些资源十分有用. 比如国内还是有些牛人在分享心得,在此也广播一下给新手: JustRun对Orchard的一些代码分析 Orchard源码分析 Orchard中文社区 ...

  9. uva 10994 - Simple Addition(规律)

    题目链接:uva 10994 - Simple Addition 题目大意:给出l和r,求∑(l≤i≤r)F(i), F(i)函数题目中有. 解题思路:由两边向中间缩进,然后l和r之间的数可以按照1~ ...

  10. 【Python】Django用户、认证、鉴权模块使用

    此文是总结Django官方网站里面的Document的文章 User authentication in Django http://www.djangoproject.com/documentati ...