题目大意:

给你一个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 栈模拟的更多相关文章

  1. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  2. HDU 1022 Train Problem I(栈的操作规则)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Ot ...

  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【栈的应用】【8月19】

    Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy n ...

  5. Train Problem I--hdu1022(栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. hdu Train Problem I

    这道题是道简单的栈模拟题,只要按照真实情况用栈进行模拟即可: #include<stdio.h> #include<string.h> #include<stack> ...

  7. hdu 1022 Train Problem I(栈)

    #include<iostream> #include<vector> #include<cstring> #include<string> #incl ...

  8. HDU 1022 Train Problem I 用栈瞎搞

    题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种 ...

  9. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

随机推荐

  1. Android定时器功能实现方法

    在Android开发中,定时器一般有以下3种实现方法: 1.采用Handler与线程的sleep(long)方法 2.采用Handler的postDelayed(Runnable, long)方法 3 ...

  2. 关于Linux下面msyql安装后并未设置初始密码,但是登录报错“Access denied for user 'root'@'localhost' (using password: NO)”的解决方案

    如上图:首先我安装mysql的时候并没有设置密码,但是就是登不进去,百度了一下,解决方案如下: 解决方案地址:http://zhidao.baidu.com/link?url=7QvuOKtfRdMT ...

  3. android-'Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8'

    解决的方案是将jdk1.7制定的版本定制为jdk.6.即 在eclipse中,右键项目->Properties->Java Compiler->enable "projec ...

  4. angularJS学习手册(1)

    欢迎来到augularJS的学习 一.什么是angularJS? angular的英文意思是角,为什么要叫angular我也不知道. AngularJS是一个MV*(Model-View-Whatev ...

  5. [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

    For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...

  6. 【USACO 2.2.2】集合

    [题目描述] 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每 ...

  7. IE layout详解

    引言: Internet Explorer 中有很多奇怪的渲染问题可以给他一个”layout”得到解决,John Gallant 和 Holly Bergevin把他归类为“dimensional b ...

  8. HTML语义化标签(二)

    为了保证网页去样式后的可读性,并且又符合web标准,应该注意一下几点: 1  尽可能少的使用无语义的标签div和span: 2  在语义不明显时,既可以使用div或者p时,尽量用p, 因为p在默认情况 ...

  9. hadoop+eclipse集群搭建及测试

    前段时间搭了下hadoop,每次都会碰到很多问题,也没整理过,每次搜索都麻烦,现在整理下 一.准备工作 1.准备俩计算机,安装linux系统,分别装好jdk(虚拟机操作一样) nano /etc/ho ...

  10. PAT - IO - 螺旋方阵

    所谓“螺旋方阵”,是指对任意给定的N,将1到N*N的数字从左上角第1个格子开始,按顺时针螺旋方向顺序填入NxN的方阵里.本题要求构造这样的螺旋方阵. 输入格式: 输入在一行中给出一个正整数N(< ...