题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程。

题目思路:栈的特点是先进后出,和题意类似,还有有一种情况是:开进来立马有开出去。并用vis[]数组的0,1标记进出站情况。

具体看代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<iostream>
#define MAX 100005
using namespace std; int main()
{
int A,B,top,i,j,n,vis[MAX],stack[MAX],ok;
char str1[MAX],str2[MAX];
while(scanf("%d%s%s",&n,str1,str2)!=EOF)
{
ok=;
memset(vis,,sizeof(vis));
i=;
A=;//当前str1的位置
B=;//当前str2的位置
top=;//栈顶
while(B<n)
{
if(str2[B]==str1[A])//如果两者相同就是即进即出的情况
{
vis[i++]=;//进站
vis[i++]=;//出站
A++;
B++;
} else if(top && stack[top]==str2[B])//如果栈非空,而且栈顶元素=str2当前元素则弹出栈
{
vis[i++]=;
top--;
B++;
} else if(A <= n)
{
stack[++top]=str1[A++];//压入栈
vis[i++]=;
} else
{
ok=;
break;
}
} if(!ok)
{
printf("No.\nFINISH\n");
} else
{
printf("Yes.\n");
for(j=;j<i;j++)
{
if(vis[j])
printf("in\n");
else
printf("out\n");
}
printf("FINISH\n");
}
}
return ;
}

HDU 1022 Train Problem I 用栈瞎搞的更多相关文章

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

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

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

  3. hdu 1022 Train Problem I(栈的应用+STL)

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

  4. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  5. HDOJ/HDU 1022 Train Problem I(模拟栈)

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

  6. hdu 1022 Train Problem I(栈)

    #include<iostream> #include<vector> #include<cstring> #include<string> #incl ...

  7. HDU 1022 Train Problem I 模拟栈题解

    火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...

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

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

  9. Hdu 1022 Train Problem I 栈

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

随机推荐

  1. CentOS/RedHat rpm方式安装Apache2.2

    注:所有RPM包均从网易镜像上下载 # rpm -ivh /home/apache/apr-1.3.9-5.el6_2.x86_64.rpm warning: /home/apache/apr-1.3 ...

  2. Java8 (1)

    参考资料: <Java8 in Action> Raoul-Gabriel Urma 一.jdk8 客观的说,Java8是一次有重大演进的版本,甚至很多人认为java8所做的改变,在许多方 ...

  3. [css3动画]渐隐渐现

    测试 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  4. CentOS安装配置Tomcat7

    1.下载apache-tomcat-7.0.62.tar.gz 2.解压:tar -zxvf apache-tomcat-7.0.62.tar.gz 3.配置环境变量: 进入安装目录:(/usr/lo ...

  5. Ztree手风琴效果(第三版)

    第一版:点击一级目录展开,再点击时不能收回 第二版:点击一级目录展开,再点击时可以收回 第三版:优化样式,修复主菜单下的子菜单下级无子节点时点击无反应问题(js报错) <%@ page lang ...

  6. php 验证码类

    <?php  class Vcode {   private $width; //宽   private $height; //高   private $num;  //数量   private ...

  7. getScrollX()理解

  8. iOS拨打电话

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...

  9. VBS 自动发送邮件

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  10. 第六节,初识python和字符编码

    程序语言的发展 机器语言 程序语言,最初的计算机语言是机器语言,完全是0和1组成的二进制串  如:01010101 11010101 汇编语言 因为01010101的字符串,冗长,不利于维护,所以产生 ...