题目大意:

给你一个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. Swift-Lesson2(下)

  2. CentOS 6.4的安装--史上最全-CRPER木木

    安装工具及软件: VmwareWorkstation9 CentOS-6.4-x86_64-LiveCD Vmware初始设置:           刚装好的VMWARE启动后,虽说默认已经设置好基础 ...

  3. [PWA] 11. Serve skeleton cache for root

    Intead of cache the root floder, we want to cache skeleton instead. self.addEventListener('install', ...

  4. IOS 获取通讯录中信息

    获取通讯录中信息 一. 我们设置一个ABAddressBookRef类型的属性addressBook. 二. 要获得通讯录中的信息,我们需要获取访问通讯录的权限. 在运行下面的获取权限的方法的时候,系 ...

  5. [转] Javascript中数组与字典(即object)的使用

    简述: 简单记录一下数据结构字典和数组, 其实在Javascript这种弱类型的脚本语言中,数组同时也就是字典,下面主要就是字典数组的简易使用 代码: 1. 数组中添加map <!DOCTYPE ...

  6. ExecuteReader执行查询实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  7. HTML案例练习一

    发现其实JS也是挺容易的,也挺好玩的,写的一个控制图片移动的小案例,对DOM机制还是不怎么熟. <html> <head> <style type = "tex ...

  8. js 终止页面加载

    var xmlhttp = new XMLHttpRequest(); xmlhttp.abort(); window.stop();

  9. 基于CANVAS与MD5的客户端生成验证码

    好久没写东西,工作太忙了!不想服务端请求太多,搞了个这玩意儿,不过项目中并不会用上,还是使用服务端生成的机制(会安全多少呢?):我就想问个问题,除了图像识别来破解这样的简单验证码外,针对我这种例子,可 ...

  10. easydialog.js

    /** * easyDialog v2.2 * Url : http://stylechen.com/easydialog-v2.0.html * Author : chenmnkken@gmail. ...