HDU 1022 Train Problem I 用栈瞎搞
题目大意:有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 用栈瞎搞的更多相关文章
- 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【栈的应用】【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(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- HDOJ/HDU 1022 Train Problem I(模拟栈)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- hdu 1022 Train Problem I(栈)
#include<iostream> #include<vector> #include<cstring> #include<string> #incl ...
- HDU 1022 Train Problem I 模拟栈题解
火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈. 数据量非常少,故此题目非常easyAC. 直接使用数组模拟就好. #include <stdio.h> const i ...
- 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 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- POJ 2313 Sequence#贪心
(- ̄▽ ̄)-* 找规律 //初始化为B[i]=A[i] //然后由V=|A[1]-B[1]|+|A[2]-B[2|+|A[3]-B[3]| // +|B[1]-B[2]|+|B[2]-B[3]| / ...
- 浙大pat 1031题解
1031. Hello World for U (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Giv ...
- asp.net正则表达式去除a标签
if (drr["allow_a"].ToString() == "False") { cont = dr["news_Content"]. ...
- JVM 几个重要的参数
<本文提供的设置仅仅是在高压力, 多CPU, 高内存环境下设置> 最近对JVM的参数重新看了下, 把应用的JVM参数调整了下. 几个重要的参数 -server -Xmx3g -Xms3 ...
- Objective-C中的instancetype与id的区别
一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...
- maven入门(上)
Apache Maven 入门篇 ( 上 ) 作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法.这 ...
- beginBackgroundTaskWithExpirationHandle
[[UIApllication sharedApplication] beginBackgroundTaskWithExpirationHandle:^{}];这个方法在app进入后台时,可以做一些事 ...
- elicpse之tomcat配置
环境:eclipse4.5.0,tomcat7.0.57 部署描述:按照一般的部署,把tomcat部署到webapps的下面,server options 下面选的是 Modules auto rel ...
- iOS中的触摸事件,手势识别,摇晃事件等
在iOS中,事件可以划分为以下几类: 1.触摸事件:通过触摸,手势进行触发(手指点击.缩放等) 2.运动事件:通过加速器触发(例如手机晃动) 3.远程控制事件:通过其他远程设备触发(例如耳机控制按钮) ...
- tableView左滑按钮
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsFo ...