HDU 1022.Train Problem I【栈的应用】【8月19】
Train Problem I
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 railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
3 123 321
3 123 312
Yes.
in
in
in
out
out
out
FINISH
No.
FINISHFor the first Sample Input, we let train 1 get in, then train 2 and train 3.HintHint
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.".
栈的应用:给定1个出栈序列,看当前入栈序列是否能通过入栈出栈达到这个出栈序列.代码例如以下:
#include<cstdio>
#include<stack>
using namespace std;
int main(){
int n;
char f1[1010],f2[1010];//f1记录入栈顺序,f2记录出栈顺序
while(scanf("%d%s%s",&n,f1,f2)!=EOF){
int g[1010],i=0,j=0,k=0;//g[]记录进出情况
stack<char> t;
while(i<n){
if(t.empty()||t.top()!=f2[i]&&j<n){
t.push(f1[j++]);
g[k++]=1;//1为in
}
else if(t.top()==f2[i]){
t.pop();
g[k++]=0;//0为out
i++;
}
else break;
}
if(t.empty()){//栈t为空,则能够生成当前出栈序列
printf("Yes.\n");
for(int i=0;i<k;i++){
if(g[i]) printf("in\n");
else printf("out\n");
}
printf("FINISH\n");
}
else printf("No.\nFINISH\n");
}
return 0;
}
HDU 1022.Train Problem I【栈的应用】【8月19】的更多相关文章
- Hdu 1022 Train Problem I 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1022 Train Problem I(栈的操作规则)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1022 Train Problem I【模拟出入栈】
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU - 1022 Train Problem I STL 压栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022 Train Problem I(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1022:Train Problem I(数据结构,栈,递归,dfs)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1022 Train Problem I[给出两个长n的串,入栈和出栈顺序,判断入栈顺序是否可以匹配出栈顺序]
Train Problem I 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 As the new term comes, the Ignatius Train Sta ...
- HDU 1022 Train Problem I
A - Train Problem I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 1022 Train Problem
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- php(apache)切换版本
php(apache)切换版本 1.brew link php@7.1 2.上两行写入 ~/.bash_profile文件 3.source ~/.bash_profile 4.sudo vi /et ...
- sql语句start with connect by prior语法解析
prior分两种放法: 1 放在子节点端 表示start with 指定的节点作为根节点,按照从上到下的顺序遍历 2 放在父节点端 表示start with指定的节点作为最底层节点,按照从下到上的顺序 ...
- Windows 自动启动 bat
创建文件,然后把这个文件放到window开机自动执行的目录中,之后的每次开机都会重新启动这个脚本 cd /d %~dp0 %1 start "" mshta vbscript:cr ...
- CSS-变量
为什么使用 css variables 借用Scrimba上的: easier to get started (no transpiling) have access to the DOM 1.loc ...
- MySQL数据库基本用法-聚合-分组
聚合 为了快速得到统计数据,提供了5个聚合函数 count(*)表示计算总行数,括号中写星与列名,结果是相同的 查询学生总数 select count(*) from students; max(列) ...
- Xamarin Essentials教程安全存储SecureStorage
Xamarin Essentials教程安全存储SecureStorage 在实际应用中,应用程序会将一些数据保存在用户设备中,避免用户重复操作.但是为了防止因设备丢失或者感染病毒导致数据泄漏,需 ...
- Jmeter测试http+JSON配置相关
1.添加HTTP信息头管理器 Content-Type application/json Accept application/json 2.添加http请求(方法.编码.路径.body)
- Android系统定制——Download Android System 及加载system镜像文件
定制android系统(配置及相关系统的镜像文件),具体可参考:Driver_All_in_One_V1.0——MT6735_6753.pdf文档,特别需要理解的是Download部分. 与之对应的软 ...
- php商品条件筛选功能你是怎么做出来的?
php商品条件筛选功能你是怎么做出来的? php按条件筛选商品的功能,还是比较简单的.其实就是根据不同的条件组成SQL查询条件,从数据库里查出不同的商品出来.举个例子:用户可以按价格范围.按品牌.按商 ...
- [P4994]终于结束的起点 (递推)
终于结束的起点 终于写下句点 终于我们告别 终于我们又回到原点 …… 一个个 OIer 的竞赛生涯总是从一场 NOIp 开始,大多也在一场 NOIp 中结束,好似一次次轮回在不断上演. 如果这次 NO ...