题目大意:有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. Intellij Idea web项目的部署配置[转]

    原文地址:http://blog.csdn.net/z69183787/article/details/41416189 1.前言 2.项目配置(Project Structure) 2.1 Proj ...

  2. Java JVM 类的连接与初始化 [ 转载 ]

    Java类的连接与初始化 (及2013阿里初始化笔试题解析)  转自http://www.cnblogs.com/iceAeterNa/p/4876747.html         Java虚拟机通过 ...

  3. .Net Core 常见问题整理

    1.安装时报0x80070490 找不到元素 这里应该是vs只装了web没有装c++ 下载一个 VC_redist.x64.exe 安装就行了 https://github.com/dotnet/co ...

  4. 官方解答:Vultr VPS常见问题

    VULTR VPS配置高,价格低廉,是非常优秀的vps品牌.今天我来翻译vultr官方FAQ,相信你能找到具体答案. Q 请介绍VULTR VPS机器硬件配置 Intel CPU 3+ GHz Cor ...

  5. jquery 动态生成元素 事件

    $(document).on("click",".detail",function () {});

  6. Android Lights

    Android Lights 很多Android手机上都配有LED灯,手机在充电.新来短信等时候都会有相应的指示灯提示. Android系统之中,一共定义了8个逻辑灯,包含:背光,键盘灯,按键灯,充电 ...

  7. 网络通信框架Apache MINA

    Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...

  8. Valgrind: memcheck of memleak/mem-uninitialization; massif usage

    first install valgrind, its newest ver is 3.11, and stops updating since 2015/12. in centos, yum ins ...

  9. php 分页类(2)

    <?phpinclude("connection.php");$perNumber=10; //每页显示的记录数$page=$_GET['page']; //获得当前的页面值 ...

  10. jquety选择器

    基本选择器 1.#id        根据id的属性值来获取元素 2.TagName     根据标签名来获取元素 3.selector1,selector2    匹配列表中的选择器(就是可以匹配多 ...