杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25276    Accepted Submission(s): 9529

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
 
Recommend
We have carefully selected several similar problems for you:  1026 1023 1004 1032 1010 
 
 
这个题要是手写栈的话一定要注意范围各个参数的范围
 
题意: 火车进站,出站能否按所给顺序出站  标准栈。
 

  代码1:

 #include<cstdio>
#include<string>
#include<iostream>
#define N 20
using namespace std;
string sc[N];
string in,out;
char str[N];
int main()
{
int t ,i , j ,c , k ;
while(~scanf("%d",&t))
{
in.clear();out.clear();
cin>>in>>out;
j = ;c = ; k =;
for( i = ; i < t ; i++ )
{
str[c]=in[i];
sc[k++]="in";
while(c!=-&&str[c]==out[j])//这儿有点问题,c=0这儿有歧义,有时候栈为空,有时候不空,最好别这么写
//也就是,str有变化的时候,第一时间修改c的值
//这儿就是用混了,写成了c != 0,事实上,这儿c=0的时候,栈里还有一个元素
{
sc[k++] = "out";
c--;j++;
}
c++;
}
if(j==t) {printf("Yes.\n");
for( i = ;i < k ; i++)
printf("%s\n", sc[i].c_str());} //string是不可以printf的,要用c_str函数,转为字符数组
else printf("No.\n");
printf("FINISH\n");//少了换行
}
//system("PAUSE");
return ;
}

代码2:

 #include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
int n, i, j, k, flag[];
char s1[], s2[];
stack <char> s;
while(~scanf("%d %s%s",&n,s1,s2))
{
while(!s.empty()) s.pop(); //也可以不写这一句,把 stack <char> s; 就可以了
memset(flag,-,sizeof(flag));
j = k = ;
for(i = ; i < n; i++)
{
s.push(s1[i]);
flag[k++] = ;
while(!s.empty() && s.top() == s2[j])
{
flag[k++] = ;
s.pop();
j++;
}
}
if(j == n)
{
printf("Yes.\n");
for(i = ; i < k; i++)
{
if(flag[i])
printf("in\n");
else
printf("out\n");
}
}
else
printf("No.\n");
printf("FINISH\n");
}
return ;
}

代码3:(感谢提供代码的伟大帅气的松哥~)

 #include <cstdio>
using namespace std; int main()
{
int n;
char o1[], o2[];
int stack[];
bool ans[];
while(~scanf("%d", &n))
{
scanf("%s %s", o1, o2);
int top = , cur = , c = ;
for(int i = ; i < n; i++)
{
stack[top++] = o1[i]-'';
ans[c++] = ;
while(top > && stack[top-] == o2[cur]-'')
{
top--;
cur++;
ans[c++] = ;
}
}
if(top > ) puts("No.");
else
{
puts("Yes.");
for(int i = ; i < c; i++)
{
if(ans[i] == ) puts("out");
else puts("in");
}
}
puts("FINISH");
}
return ;
}

train problem I (栈水题)的更多相关文章

  1. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  2. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】

    CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...

  3. 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 栈

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

  5. Train Problem I (HDU 100题纪念)

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

  6. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  7. Train Problem(栈的应用)

    Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...

  8. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem C. Contest 水题

    Problem C. Contest 题目连接: http://codeforces.com/gym/100714 Description The second round of the annual ...

  9. Gym 100646 Problem E: Su-Su-Sudoku 水题

    Problem E: Su-Su-Sudoku/center> 题目连接: http://codeforces.com/gym/100646/attachments Description By ...

随机推荐

  1. 开发中关于Git那些事(续:Git变基)

    其实上一篇写的内容仅仅是Git的冰山一角,如果你认为Git就是简简单单的几行命令,那只能说明你还没有真正了解Git这个强大的内容寻址文件系统.这篇文章,还是接着介绍一些实用但是很少有人知晓的一些命令, ...

  2. jquery图片延迟加载 及 serializeArray、serialize用法记录

    1.使用jquery实现 图片延迟加载 由于用户访问页面需要加载很多的图片,延迟加载技术在电子商务网站领域越来越普及,淘宝商城,京东商城,凡客等访问量巨大的电子商务站点为了增加用户用户体验,访问速度以 ...

  3. javascript字符串与数组转换汇总

    本文给大家分享的是Js中字符串转换成数组,数组转换成字符串的函数,十分的简单实用,有需要的小伙伴可以参考下. 数组转字符串 1.join()方法 ? 1 2 3 4 var s= ["a&q ...

  4. npm 项目更换目录后无法启动

    问题描述: 使用 Vue-cli 创建的项目,当文件移动到其他目录后,无法正常启动,报错信息如下: 分析原因: npm 项目,在安装依赖(node_modules)的时候,会记录当前的文件路径.当路径 ...

  5. Linux入门篇(一)——基本命令

    这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...

  6. Runtime那些事

    Runtime 前言 从字面意思看,就是运行时.但是这个运行时究竟什么意思?可以把它理解成:不是在编译期也不是在链接期,而是在运行时.那究竟在运行期间做了什么呢?按照苹果官方的说法,就是把一些决策(方 ...

  7. CSS预编译器less简单用法

    1.变量 定义变量 @变量名:值; @test_width:100px; 使用变量 .box{ width:@test_width; height:@test_width; background-co ...

  8. 阻止form空表单提交----JavaScript

    网上看到很不错的阻止form空表单提交 第一种方法 <div class="warp"> <h2>登录到pfan空间</h2> <p> ...

  9. CDN 边缘规则,三秒部署、支持定制、即时生效,多种规则覆盖常用业务场景

    2017年的最后一周,又拍云进行了一次重要升级,将自定义 Rewrite 升级为"边缘规则".互联网应用场景的日益多样化,简单.方便.快速的根据不同应用场景实现不同的功能变得越来越 ...

  10. shell脚本的if语句,判断某程序是否存在,不存在启动该程序!

    想必大家都知道 "如果......那么......" 这种语法的应用吧! 当然呢,linux下对于这种用法也是有所考虑的,很多时候我们都需要写一个shell脚本,难免会避免if语句 ...