HDU Train Problem I (STL_栈)
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.".
栈的简单应用,本题具体解答在白书上有.
以下附上代码:用c++交会错。用G++就不会。。
。求解释
#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
using namespace std;
int a[10000],ans[20000],b[10000];
stack<int>ss;
int main()
{
int n,i,j,k;
string s1,s2;
while(scanf("%d",&n)!=EOF){
cin>>s1>>s2;
for(i=1;i<=n;i++) a[i]=s1[i-1]-'0';
for(j=1;j<=n;j++) b[j]=s2[j-1]-'0';
while(!ss.empty()) ss.pop();
k=0,j=1;
for(i=1;i<=n;i++) {
ss.push(a[i]);
ans[++k]=1;
while(!ss.empty() && ss.top()==b[j]) {
ss.pop();
ans[++k]=2;
j++;
}
}
if(k==2*n) {
printf("Yes.\n");
for(i=1;i<=2*n;i++) {
if(ans[i]==1) printf("in\n");
else printf("out\n");
}
}
else printf("No.\n");
printf("FINISH\n");
}
return 0;
}
HDU Train Problem I (STL_栈)的更多相关文章
- HDU Train Problem I 1022 栈模拟
题目大意: 给你一个n 代表有n列 火车, 第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序, 分析一下就知道这,这个问题就是栈, 先进后出吗, ...
- 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【栈的应用】【8月19】
Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...
- hdu 1022 Train Problem I(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Train Problem I--hdu1022(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- HDOJ/HDU 1022 Train Problem I(模拟栈)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- hdu 1022 Train Problem I(栈)
#include<iostream> #include<vector> #include<cstring> #include<string> #incl ...
随机推荐
- Core Dataeasy出现的错误
1.2015-08-24 15:52:17.674 Tasks[3189:144763] CoreData: error: -addPersistentStoreWithType:SQLite con ...
- linux 下password加密程序(能够用于替换shadow文件里的用户password)
源代码例如以下: #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]){ if(arg ...
- 生成静态页java代码
package com.fang.news.test; import java.io.BufferedReader; import java.io.BufferedWriter; import jav ...
- java 递归实现删除或查询指定目录下的全部文件
/** * 递归列举盘符下的全部文件的名称,如E:\HeartIsland * * @author HeartIsland * */ public class FileListDemo { /** * ...
- 多线程的join和interrupt
你可以在一个线程1里添加线程2对象thread的join方法来让线程1处于等待的状态 ,同时也可以调用thread.interrupt()来打断等待状态,此处注意 interrupt应在线程1开启st ...
- Inside ARC — to see the code inserted by the compiler
前言 这是我在我们技术团队所做的一次分享,稍作改动放到博客上来. 我们技术团队会不定期(一般一个月1~2次)做技术分享,对我们团队有兴趣的能够私信我. 下面是正文. 这里的主题是"Insid ...
- cocos2d-x之浅析Hello World
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Ruby学习笔记(二)——从管道读取数据
在对文件名修改后,今天又给自己出了新的难题,想从实验结果中提取数据,并将其作为文件夹的名称.其中,比赛的主办方提供的评估算法是用perl写的,因此读取实验结果最为简单的想法自然是使用管道命令,即 ./ ...
- ubuntu下无法将iNode绑定到侧边栏的解决办法
title: ubuntu下无法将iNode绑定到侧边栏的解决办法 toc: false date: 2018-09-01 17:43:52 categories: methods tags: ubu ...
- Python笔记(五)
# -*- coding:utf-8 -*- # 函数 # python中定义函数的规则如下:以def开头,接函数名称和(),传入的参数和变量放在圆括号中间,函数以:起始,并且缩进,return选择性 ...