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

数据量非常少,故此题目非常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. 页面关闭或刷新时触发javascript的事件

    当页面在关闭或刷新时提示 window.onbeforeunload(function(){ //判断是关闭还是刷新 1.满足关闭,否则是刷新 if(event.clientX>document ...

  2. VUE:渐进式JavaScript框架(小白自学)

    VUE:渐进式JavaScript框架 一.官网 英文 https://vuejs.org/ 中文 https://cn.vuejs.org/ 二:渐进式 即有一个核心库,在需要的时候再逐渐添加插件的 ...

  3. UNIX基础【UNIX入门经典】

    最早在学校很流行.学生毕业以后就会为公司购买操作系统.导致UNIX流行 UNIX内核: Shell:sh csh ksh 其他组件:

  4. 练练脑,继续过Hard题目

    http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Diffi ...

  5. [Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular

    By default the new Angular Http client (introduced in v4.3.1) uses JSON as the data format for commu ...

  6. 广播BroadcastReceiver(2)

    有序广播的优先级:        发送有序广播的方法有:    public void sendOrderedBroadcast(Intent intent,String receiverPermis ...

  7. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)

    如无书面授权,请勿转载 Larger Projects Until now, we have been looking at single plays in one playbook file. Th ...

  8. nodejs中require的路径是一个文件夹时发生了什么

    node中使用require的时候如果路径是一个文件夹时,或者特殊的情况require('..');require('.'); 这是node实战这本书里说的情况,但是我在node6.9版本中发现不完全 ...

  9. 7.boostUDP通信

    客户端 #include <iostream> #include<string> #include <boost/asio.hpp> #include <st ...

  10. javascript对象的相关操作

    Window对象 我们知道浏览器对象模型(BOM)是javascript的组成之一,它提供了独立于内容与浏览器窗口进行交互的对象.其分层结构如下: window对象是整个BOM的核心其有documen ...