火车进站,模拟一个栈的操作,额外的栈操作,查看能否依照规定顺序出栈。

数据量非常少,故此题目非常easyAC。

直接使用数组模拟就好。

#include <stdio.h>
const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N];
bool rs[MAX_N<<2];
int n; int main()
{
while (scanf("%d", &n) != EOF)
{
scanf("%s %s", inOrder, outOrder); int j = 0, out = 0, i = 0, st = 0;
bool possible = true;
while (possible && !(st == 0 && out == n))
{
for (; i < n && inOrder[i] != outOrder[out]; i++)
{
rs[j++] = true;
stk[st++] = inOrder[i];
}//push in
i++;//Watch out: don't forget while inOrder[i]==outOrder[out]!
rs[j++] = true;
rs[j++] = false;
out++; while (st > 0 && stk[st-1] == outOrder[out])
{
st--; out++;
rs[j++] = false;
}//pop back int k = 0;//check possible
for (; k < st && stk[k] != outOrder[out]; k++);
if (k < st) possible = false;
}
if (possible)
{
puts("Yes.");
for (int i = 0; i < j; i++)
{
if (rs[i]) puts("in");
else puts("out");
}
}
else puts("No.");
puts("FINISH");
}
return 0;
}

解法二:

#include <stdio.h>
const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N];
bool rs[MAX_N<<2];
int n; int main()
{
while (scanf("%d", &n) != EOF)
{
scanf("%s %s", inOrder, outOrder); int j = 0, out = 0, i = 0, st = 0;
while (i<n && !(st == 0 && out == n))
{
for (; i < n && inOrder[i] != outOrder[out]; i++)
{
rs[j++] = true;
stk[st++] = inOrder[i];
}//push in
i++;//Watch out: don't forget while inOrder[i]==outOrder[out]!
rs[j++] = true;
rs[j++] = false;
out++; while (st > 0 && stk[st-1] == outOrder[out])
{
st--; out++;
rs[j++] = false;
}//pop back
}
if (st == 0 && out == n)
{
puts("Yes.");
for (int i = 0; i < j; i++)
{
if (rs[i]) puts("in");
else puts("out");
}
}
else puts("No.");
puts("FINISH");
}
return 0;
}

HDU 1022 Train Problem I 模拟栈题解的更多相关文章

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

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

  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. HDU 1022 Train Problem I(栈模拟)

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

  6. hdu 1022 Train Problem I(栈)

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

  7. HDU 1022 Train Problem I 用栈瞎搞

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

  8. hdu 1022 Train Problem I【模拟出入栈】

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

  9. Hdu 1022 Train Problem I 栈

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

随机推荐

  1. 1、koa的安装,get和post方法的基础使用

    koa是干什么:koa是用来解决回调嵌套的方案,减少异步回调,提高代码的可读性和可维护性同时也改进了错误处理( Express的错误处理相当糟糕) koa相比express的优点在哪里1.更加优雅简单 ...

  2. Vue-router入门

    1.npm install vue-router --save-dev 安装路由包,在安装脚手架时实际上可以直接安装 2.解读核心文件 router/index.js文件 import Vue fro ...

  3. 【codeforces 190C】STL

    [题目链接]:http://codeforces.com/problemset/problem/190/C [题意] 让你根据去掉标点符号的; pair 以及 int这两种类型; 确定出一种类型; 使 ...

  4. POJ——T1679 The Unique MST

    http://poj.org/problem?id=1679 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30120   ...

  5. test文件夹,测试类是放在src目录下的,test测试代码是代码啊,当然要放在代码文件夹下

    test文件夹,测试类是放在src目录下的,test测试代码是代码啊,当然要放在代码文件夹下 Maven的标准工程结构 Maven的标准工程结构如下: |-- pom.xml(maven的核心配置文件 ...

  6. 安装MYSQL错误“conflicts with file from package mysql-libs-*” 解决方法

    安装MYSQL的时候时: 错误现象: [root@localhost opt]# rpm -ivh MySQL-server-5.5.32-1.el6.x86_64.rpm Preparing... ...

  7. 根据数据库表结构生成java类

    import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWrit ...

  8. cocos2d-x-lua基础系列教程一(hello lua)

    myscene.lua function ERROR_TRACBACK(msg) print (==========) print ("lua error is "..tostri ...

  9. OpenGL ES 3.0 Graphics Pipeline

    一:OpenGL ES 3.0 Graphics Pipeline  渲染管道如下图 1.Vertex Buffer/Arrays Objects的数据由应用程序传进来 2.由上图可以看到Textur ...

  10. [bzoj 1398] Vijos1382寻找主人 Necklace 解题报告(最小表示法)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1398 题目: Description 给定两个项链的表示,判断他们是否可能是一条项链. ...