HDU Train Problem I 1022 栈模拟
题目大意:
给你一个n 代表有n列 火车, 第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序,
分析一下就知道这,这个问题就是栈, 先进后出吗, 就是问你这个编号有没有可能出现, 有可能的话输出顺序,没可能直接输出No
题目分析:
我们用栈进行模拟, 先判断是否有这种出栈的可能, 假如有这种可能的话我们在模拟 一遍出栈的顺就行了。
每一次有新元素入栈我们就判断他是否雨 第二个字符串的第一个元素是否相等, 假如一样, 就让次元素出栈。
代码
- #include <iostream>
- #include <queue>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <stack>
- using namespace std;
- #define maxn 15
- bool OK(char str1[], char str2[],int n)//判断是否存在这种出栈的可能
- {
- stack<char> P;
- P.push('#');//因为有元素要删除赋值, 不能为空, 我们让第一个元素为‘#’
- char ans;
- int i, j;
- for(i=, j=; i<n; i++)
- {
- P.push(str1[i]);
- ans = P.top();
- while(ans == str2[j] )
- {
- P.pop();
- j ++;
- ans = P.top();//此处是要赋值判断的, 假如为空的话就无法赋值, 因此我们让第一个元素为‘#’
- }
- }
- if(j == n)
- return true;
- return false;
- }
- void Putt(char str1[], char str2[],int n)
- {
- stack<char> P;
- P.push('#');
- for(int i=, j=; i<n; i++)
- {
- P.push(str1[i]);
- printf("in\n");
- char ans = P.top();
- while(ans == str2[j])
- {
- P.pop();
- printf("out\n");
- j ++;
- ans = P.top();
- }
- }
- }
- int main()
- {
- int n;
- char str1[maxn], str2[maxn];
- while(cin >> n >> str1 >> str2)
- {
- if(OK(str1, str2, n) )
- {
- printf("Yes.\n");
- Putt(str1,str2,n);
- }
- else
- printf("No.\n");
- printf("FINISH\n");
- }
- return ;
- }
HDU Train Problem I 1022 栈模拟的更多相关文章
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- 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(栈的应用+STL)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 ...
- Train Problem I--hdu1022(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu Train Problem I
这道题是道简单的栈模拟题,只要按照真实情况用栈进行模拟即可: #include<stdio.h> #include<string.h> #include<stack> ...
- hdu 1022 Train Problem I(栈)
#include<iostream> #include<vector> #include<cstring> #include<string> #incl ...
- HDU 1022 Train Problem I 用栈瞎搞
题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
随机推荐
- Linux如何关闭防火墙和查看防火墙的具体情况
1.Linux下关闭和开启防火墙 1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: ser ...
- When does layoutSubviews get called?
转自:http://blog.logichigh.com/2011/03/16/when-does-layoutsubviews-get-called/ It’s important to optim ...
- NSDateFormatter 格式说明
格式化参数如下: G: 公元时代,例如AD公元 yy: 年的后2位 yyyy: 完整年 MM: 月,显示为1-12 MMM: 月,显示为英文月份简写,如 Jan M ...
- java多态---内存关系
在该列中,a.lookDoor()会报错,因为azhong没有lookDoor这个方法,同理,a.playGame()也会报错. 注意!!! 最后一句Dog dd=(Dog)a: 这句话非常错误! 在 ...
- 【IOS】 XML解析和xml转plist文件(GDataXML)
iOS对于XML的解析有系统自带的SDK--NSXMLParser,鄙人愚拙,只会用GDataXML进行解析,这里仅介绍GData的使用.(以下图为例) 1.对于一个xml文件,先读取出来 NSDat ...
- 地址栏访问Action,后来方法执行两次
SSH框架,在地址栏输入URL访问Action,后台访问会访问两次.很奇怪. 经排查,最终问题在于方法名称写错了.将getOpinionByPN()修改成queryOpinionByPN(),没有问题 ...
- c#将Excel数据导入到数据库的实现代码(OleDb)
sing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web ...
- 多个显示器, window.open的定位
// Pops a window relative to the current window position function popup(url, winName, xOffset, yOffs ...
- C# 判断字符串是否可以转化为数字
C# 判断字符串是否可以转化为数字 /// <SUMMARY> /// 判断字符串是否可以转化为数字 /// </SUMMARY> /// <PARAM name=&qu ...
- Subversion 1.7 Eclipse integration in Ubuntu12(转载)
原文链接:http://steveliles.github.io/subversion_1_7_eclipse_integration_in_ubuntu.html Getting Subversio ...