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声卡基本信息[转载]
查看系统版本~$ uname -aLinux laptop 2.6.27-7-generic #1 SMP Tue Nov 4 19:33:20 UTC 2008 i686 GNU/Linux 查看型 ...
- RT: TCP connection close
CLOSE is an operation meaning "I have no more data to send." The notion of closing a full- ...
- Java EE的十三种核心技术
1. JDBC: Java Database Connectivity 2. JNDI: Java Name and Directory Interface 3. EJB: Enterprise Ja ...
- Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec: 0x01030200(转)
反编译时遇到标题中的异常,根据描述,原因是找不到资源文件,最有可能的原因是apk中使用了系统资源. 解决办法如下: 从手机中导出framework-res.apk文件,该文件在/system/fram ...
- Android MediaCodec 使用例子
Android MediaCodec 使用例子 下面的例子是使用MediaCodec 录制到文件的例子. 1 public class AvcEncoder { private MediaCodec ...
- Canvas transform浅析
没有前奏,直接进入主题 transform调用方法: ctx.transform(a,b,c,d,e,f);如下 var ctx = document.getElementById("myC ...
- js异步的理解---千呼万唤始出来啊!
编译完成后(先分配给变量空间和function(){}命名的函数,var = function(){}这种函数也仅仅只是分配了个空间,还没有赋值个函数给他!),调用了若不是undefined就执行, ...
- Android模拟器访问本地的localhost失败及解决方案
在开发手机网站是,使用Android模拟器测试,在手机浏览器中输入localhost访问本地服务器失败! 原因: 在Android系统中localhost就是127.0.0.1 在Windows系统中 ...
- TSQL Challenge 2
和之前发布的TSQL Challenge 1是同一系列的文章,看到那篇学习哪篇,没有固定的顺序,只为锻炼下思维. Compare rows in the same table and group th ...
- sqlcode、sqlerrm
Oracle里 非常有用的 两个变量,很少有人用. 标记一下