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 ...
随机推荐
- 在Eclipse中使用JUnit4进行单元測试(0基础篇)
本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,须要写成千上万个方法或函数,这些函数的功能可能 ...
- Linux 套接字编程中要注意的细节
隐患 1.忽略返回状态 第一个隐患很明显,但它是开发新手最容易犯的一个错误.如果您忽略函数的返回状态,当它们失败或部分成功的时候,您也许会迷失.反过来,这可能传播错误,使定位问题的源头变得困难. 捕获 ...
- Android群英传》读书笔记 (1) 第一章 Android体系与系统架构 + 第二章 Android开发工具新接触
第一章 Android体系与系统架构 1.Dalvik 和 ARTDalvik好比是一辆可折叠的自行车,平时是折叠的,只有骑的时候,才需要组装起来用.ART好比是一辆组装好了的自行车,装好就可以骑了. ...
- vim中选择匹配文本删除技巧
试举几例如下: 如何只保留匹配内容行而删除其他行? :v/pattern/d :help :v 如何对每行只保留匹配内容而删除这一行中的其它内容 :%s/^.pattern.$/\1/g 删除包含特定 ...
- 没有懂的leetcode
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【开源java游戏框架libgdx专题】-05-模块描述与上下文
模块描述(Modules overview) Input:为所有的平台提供一个统一的输入模型和处理程序. 获取触摸示例: if (Gdx.input.isTouched()) { System.out ...
- js星级评分点击星级评论打分效果--收藏--转载
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android LayoutInflater和findViewById 源码详解
LayoutInflater大家很熟悉,简单点说就是布局文件XML解析器,setContentView函数也是调用了LayoutInflater 用法: View view = LayoutInfla ...
- `~!$^*()[]{}\|;:'",<>/?在英文怎么读?
`~!$^*()[]{}\|;:'",<>/?在英文怎么读? 'exclam'='!' 'at'='@' 'numbersign'='#' 'dollar'='$' 'perce ...
- 二、C# 数据类型
C#语言的基本类型包括8种整数类型.2种用于科学计算的二进制浮点类型.1种用于金融计算的十 进制浮点类型.1种布尔类型以及1种字符类型. 2.1 基本数值类型 C#中的基本数据类型都有关键字和它们关联 ...