HDOJ/HDU 1022 Train Problem I(模拟栈)
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
HintHint
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.”.
火车进站问题,输入首先包含一个整数N,表示要进站的火车数,然后是依次输入进站火车的序号,然后再输入出站火车的序号,看是否符合出站顺序。
当符合顺序的时候输出”Yes.”依次输入每趟车进站出站的状态进站表示”in” 出站表示”out”,
当不符合顺序的时候输出”No.”程序结束后输出”FINISH”
分析:
模拟栈的输入输出!
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int t = sc.nextInt();
String in = sc.next();
String out = sc.next();
int a=0;
int b=0;
boolean ok=true;
int top=0;//表示栈里面还有元素
char stack[] = new char[50];
boolean flag[] = new boolean[50];
int i=0;
while(b<t){
if(top!=0 && ( stack[top]==out.charAt(b) )){
top--;
b++;
flag[i]=false;
i++;
}else if(a<t && in.charAt(a)==out.charAt(b)){
a++;
b++;
flag[i]=true;
i++;
flag[i]=false;
i++;
}else if(a<t){
top++;
stack[top]=in.charAt(a);
a++;
flag[i]=true;
i++;
}else{
ok=false;
break;
}
}
if(ok){
System.out.println("Yes.");
for(int k=0;k<i;k++){
System.out.println( flag[k]?"in":"out" );
}
}else{
System.out.println("No.");
}
System.out.println("FINISH");
}
}
}
HDOJ/HDU 1022 Train Problem I(模拟栈)的更多相关文章
- HDU 1022 Train Problem I 模拟栈题解
火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...
- 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(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- hdu 1022 Train Problem I(栈)
#include<iostream> #include<vector> #include<cstring> #include<string> #incl ...
- HDU 1022 Train Problem I 用栈瞎搞
题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...
- 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) ...
随机推荐
- 初次使用JFinal
刚进公司的时候是经过了学员期培训,不断的学习. 终于有机会可以进入了项目组,一个给予.NET开发的项目,框架是MVC,三个多月的项目在一个团队的合作下完成了一个正式成为程序员后参与的第一个大型项目. ...
- [001] winnie the pooh - 读后记
winnie the pooh 我是在伍君仪透析英语视频培训班,获得这本书的,PDF格式的(排版不是很好,和当当上的相比有部分章节缺失) 这是我第一本采用透析法读完的英文书. 今天(2015年10月2 ...
- 01_JavaMail_04_带附件邮件的发送
[工程截图] [代码实例] package com.Higgin.mail.demo; import java.io.File; import java.util.Properties; import ...
- Django 创建第一个项目(转)
转自(http://www.runoob.com/django/django-first-app.html) 前面写了不少python程序,由于之前都是作为工具用,所以命令行就足够了,最近写的测试用例 ...
- Java---Hibernate>>Can't create table './xxx/#sql-b2c_1a.frm' (errno: xxx)解决方法
通用方案:删除相关表,重新生成. 1.关联表之间数据引擎不一致导致: 修改相关表的引擎设定,保持一致. 2.关联表索引字段的引用类型不一样(如A表关联字段是int,B表索引是char): 修改相关表的 ...
- html定义对象
<object>定义一个对象<param>为对象定义一个参数 参数的名称:name = "" 参数的值:value=""classid: ...
- css3动画使用技巧之—JQ配合css3实现轮播之animation-delay应用
<!DOCTYPE html> <html> <head> <title>css3动画使用技巧之—JQ配合css3实现轮播之animation-dela ...
- linux正确重启MySQL的方法
修改了my.cnf,需要重启MySQL服务,正确重启MYSQL方法请看下面的文章 由于是从源码包安装的Mysql,所以系统中是没有红帽常用的servcie mysqld restart这个脚本 只好手 ...
- node论坛练手
当时学node,自己写了个论坛练手,现在看还是有很多问题,有时间好好改改 https://github.com/hitbs228/countdown
- 上传文件格式控制的困惑(application/octet-stream 限制不了BAT等格式上传)问题解决
允许上传类型部分代码 $uptypes=array( //上传文件类型列表 'image/gif', 'image/jpg', 'image/jpeg', 'image/pjpeg', 'image ...