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

数据量非常少,故此题目非常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. 封装HttpClient进行http请求与https请求

    一.https忽略证书 /** * 用于进行Https请求的HttpClient * * @author joey * */ public class SSLClient { public stati ...

  2. Redis-server在windows下闪退

    在win7下使用Redis(windows版)很简单,只需要去Git上下载一个压缩包,解压运行即可.但是前段时间发现win10下双击redis-server既然闪退.非常不解... 在观察了错误日志才 ...

  3. word上怎么打钩

    原文:http://www.wordlm.com/html/2291.html 有时我们在Word中制作一份特殊的表格时,可能会用到这样一些特殊符号,“在方框里打钩”或打叉.这些符号虽看起来简单不起眼 ...

  4. Git学习总结(10)——git 常用命令汇总

    1.git 基本概念: 工作区:改动(增删文件和内容) 暂存区:输入命令:git add 改动的文件名,此次改动就放到了'暂存区'(新增的文件) 本地仓库(简称:本地):输入命令:git commit ...

  5. Jquery-ajax错误分析

    当我把cshtml中的js代码移出到js文件中,将js代码作为文件引入cshtml时,出现了下面的这样的错误 网上的不少人说是通过在\(.ajax参数中加上async:true解决的,但\).ajax ...

  6. pcapy-0.10.8 安装

    (1)下载 http://corelabs.coresecurity.com/index.php?module=Wiki&action=view&type=tool&name= ...

  7. POJ 3696

    这里面的一个转换的小技巧很重要,把888...8转换成(10^x-1)/9*8.神来之笔,佩服. 这样有(10^x-1)/9*8=L*p得10^x-1=L*p*9/8,设m=9*L/gcd(L,8). ...

  8. 机器学习之&amp;&amp;Andrew Ng课程复习--- 聚类——Clustering

    第十三章.聚类--Clustering ******************************************************************************** ...

  9. [博弈] hdu 3683 Gomoku

    题意: 两个人下五子棋.给你现有棋盘,推断在三步之内的胜负情况. 输出分为几种. 1.棋盘不合法 2.黑或白在第一步赢下在(x,y)点,多个输出x最小的.y最小的. 3.输在第二步 4.黑或白在第三步 ...

  10. Post请求方式长度參数过长导致參数为空

    Post提交方式本身对于參数的长度没有限制,HTTP协议也没有限制. 可是今天在做一个web项目的时候碰到一个问题,当要提交的表单内容达到一定大小时,发现后台代码接收到的參数为空. 查询了一下.发现是 ...