题目大意:有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. 读书笔记之C# delegate

    c#代理的使用主要在:需要将一个方法当做参数传递到另一个方法时. 比如启动一个线程执行任务,而这个线程要执行的方法可以通过代理传递过来. 代理包括一个方法或者多个方法的地址和C++的函数指针很相似,但 ...

  2. hdu_5618_Jam's problem again(cdq分治+lowbit)

    题目链接:hdu_5618_Jam's problem again 题意: 给你n个点,每个点有一个坐标(x,y,z),找出有ans个点,3个坐标都比该点小,这个点的level就为ans,然后让你输出 ...

  3. 转:Delphi中destroy, free, freeAndNil, release用法和区别

    http://blog.sina.com.cn/s/blog_44fa172f0101cur1.html 1)destroy:虚方法 释放内存,在Tobject中声明为virtual,通常是在其子类中 ...

  4. #include 和 #pragma comment 的相对路径起点

    #include 是以当前文件所在路径为当前目录 #pragma comment 是以当前工程所在路径为当前目录 #include "../../../../ThirdParty/Inclu ...

  5. MVC5的控制器,使用HttpPost方式时,接收的参数为null的原因

    1.问题现象 POST提交时,控制的Action接收到的参数为null, 但Request.Form.Request.Params等集合其实是包含提交的所有数据的 .如下截图: 2.该问题很诡异,重新 ...

  6. mysql数据库的优化技术

    表的设计合理化(遵从3NF)<3范式> 1NF:表的列具有原子性,不可再分解(列的信息不能分解,只要是关系型的数据库就自动满足1NF) 2NF:表中的记录是唯一的,就满足2NF(通常我们设 ...

  7. DHCP 协议的 8 种消息类型及功能服务器

    1) 主机发送 DHCP Discover 广播包在网络上寻找 DHCP 服务器: 单播数据包, 地址. 2) DHCP 服务器向主机发送 DHCP Offer 单播数据包,包含 IP 地址.MAC ...

  8. DNS 域名系统 (Domain Name System)

      DNS 域名系统 (Domain Name System) 许多应用层软件经常直接使用域名系统 DNS (Domain Name System),但计算机的用户只是间接而不是直接使用域名系统. 因 ...

  9. leetcode387

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  10. sql 在将 nvarchar 值 转换成数据类型 int 时失败。

    假设有存储过程:proc_test2 create proc proc_test2 @Id int begin as declare @sql varchar(max) @sql = 'select ...