问题如下:

问题 B: Rails

时间限制:  Sec  内存限制:  MB
提交: 解决:
[提交][状态][讨论版]
题目描述 There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track. The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= coaches numbered in increasing order , , ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station. 输入 The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of , , ..., N. The last line of the block contains just . The last block consists of just one line containing . 输出 The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains "Yes" if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains "No". In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input. 样例输入 样例输出 Yes
No Yes
提示

View question

利用递归的思想,可以这么写:

 //模拟法:搭建stack堆栈模拟a堆栈出栈顺序以判断a堆栈是否合法出栈

 #include<stdio.h>
#include<string.h>
#include<malloc.h> int main()
{
int t,n,i,len,flag;
int a[],stack[];
int cur,pos,top;
while(scanf("%d",&t)!=EOF)
{
if(t==)
return ;
while()
{
cur=;pos=;top=;
for(i=;i<t;i++)
{
scanf("%d",&n);
if(i== && n==)
{
printf("\n\n");
t=;
}
a[i]=n;
} flag=;
if(t)
{ //cur=1;pos=0;top=0;
//top代表 stack 堆栈中有几个数
//pos代表已从堆栈中取出的数目,也代表a堆栈中待取数
//cur代表欲往stack堆栈中加入的新数 stack[top] = cur;
while (pos < t && top < t)
{
if (a[pos] == stack[top])//如果stack栈顶数 等于 a堆栈中待取数
{
pos++;//则取出a堆栈中待取数
top--;//并在a堆栈中往后移一位 , 同时将 stack 堆栈前移一位 //针对 当前stack 为空时, 则在stack堆栈中添加入新数cur
if (top < )
{
top = ;
stack[top] = ++cur;
}
}
else//否则在 stack堆栈中加入新的数
stack[++top] = ++cur; //检查状态
/*printf("cur =%d pos=%d top=%d flag=%d\n",cur,pos,top,flag++);
for(i=0;i<=top;i++)
printf("%d ",stack[i]);
printf("\n");*/
}
if (top == t)
printf("No\n");
else
printf("Yes\n");
}
if(t==)
break;
}
}
return ;
}

除此之外,还有一个问题,比如要求堆栈中合法出栈顺序次数

 //摘自http://hi.baidu.com/onlys_c/item/be805c428dde9dd6c0a59213
//整理by Jeremy Wu
/*sum作为全局变量记录共可能多少种情况*/
int sum = ;
/*描述:递归计算共可能出现的出栈序列,
无返回值参数:inStack
-- 目前存放于栈中的元素个数wait
-- 目前还未进栈的元素个数out
-- 目前已经出栈的元素个数num
-- 一共有多少个元素n*/
void f(int inStack, int wait, int out, int num){ /*如果全部元素都出栈,表明有了一种新情况,总数加一*/
if (out == num)
sum++; /*否则继续递归衍生新的状态*/
else{
if (inStack > )/*衍生方法1:让一个元素出栈*/
f(inStack-, wait, out+, num);
if (wait > )/*衍生方法2:让一个元素进栈*/
f(inStack+, wait-, out, num);
} }
/*用main函数调用*/
int main()
{
/*一共有n个元素*/
int n = ;
/*刚开始时栈中有0个元素,有n个元素等待,有0个元素出了栈,共有n个元素*/
f(, n, , n);
printf ("%d\n", sum);
return ;
}

poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】的更多相关文章

  1. OpenJudg / Poj 1363 Rails

    1.链接: http://poj.org/problem?id=1363 http://bailian.openjudge.cn/practice/1363 2.题目: Rails Time Limi ...

  2. POJ 1363 Rails(栈)

    题目代号:POJ 1363 题目链接:http://poj.org/problem?id=1363 题目原题: Rails Time Limit: 1000MS   Memory Limit: 100 ...

  3. POJ 1363 Rails

    Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21728   Accepted: 8703 Descriptio ...

  4. poj 1363 Rails (【栈的应用】 刘汝佳的写法 *学习)

    Rails Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25964   Accepted: 10199 Descripti ...

  5. poj 1363 Rails 解题报告

    题目链接:http://poj.org/problem?id=1363 题意:有一列火车,车厢编号为1-n,从A方向进站,向B方向出站.现在进站顺序确定,给出一个出站的顺序,判断出站顺序是否合理. 实 ...

  6. POJ 1363 Rails(栈)

    思路:将出车站的顺序存入数组train,由于入车站的顺序是固定的,为1~N,所以用P表示进站的车,初始为1. 接下来举例说明吧: 原来入站顺序:    1 2 3 4 5 读入的出战顺序: 3 4 2 ...

  7. POJ1363 Rails 验证出栈序列问题

    题目地址: http://poj.org/problem?id=1363 此题只需验证是否为合法的出栈序列. 有两个思路: 1.每个已出栈之后的数且小于此数的数都必须按降序排列.复杂度O(n^2),适 ...

  8. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  9. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

随机推荐

  1. iOS开发项目名称修改

    前言:在IOS开发中,有时候想改一下项目的名字,都会遇到很多麻烦.直接改项目名吧,XCODE又不会帮你改所有的名字.总是有很多文件.文件夹或者是项目设置的项.而且都是不能随便改的,有时候改着改着,编译 ...

  2. oracle,如何查看视图结构,获得视图中的字段名称、字段类型、字段长度等。

    需要获得一个视图中的字段名称.字段类型.字段长度等信息,该如何编写sql语句.通过select * from user_views可以获得给定用户下所有的视图名称了,但是没找到如何获取视图结构的解决方 ...

  3. Trie三兄弟——标准Trie、压缩Trie、后缀Trie

    1.Trie导引 Trie树是一种基于树的数据结构,又称单词查找树.前缀树,字典树,是一种哈希树的变种.应用于字符串的统计与排序,经常被搜索引擎系统用于文本词频统计.用于存储字符串以便支持快速模式匹配 ...

  4. C++模板:文件操作

    freopen("demo.in","r",stdin); freopen("demo.out","w",stdout) ...

  5. SVN使用技巧

    安装 下载SVN服务端:VisualSVN Server https://www.visualsvn.com/downloads/ 安装,下一步...(更改地址,Location是安装目录,Repos ...

  6. iOS中的 SB和XIB的前世今生

    今天给大家介绍一下Apple开发中三种几种常用的应用程序编写方式:纯代码创建.使用storyboard/XIB.我们都知道,纯代码编写模式适合大型项目大规模使用,利于版本管理.追踪改动以及代码合并,代 ...

  7. asp.net中 服务器控件中onselectedindexchanged 没有反应的解决方案

    最近发现项目中一个BUG就是 DropDownList 中的onselectedindexchanged 没有反应 AutoPostBack="true"和页面中的<%@ P ...

  8. 在TextBox里面仅仅允许数字,按Enter键进入下一个TextBox

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> ...

  9. 关于php析构函数__destruct()的问题

    1.背景:在一次输出错误信息,引入Componets\下的ErrorCode的类文件报错,报错的信息是该类找不到:然而那个类文件明明存在的阿,怎么会报错呢? 2.解决过程:看类加载器如何加载该类.通过 ...

  10. 通过SSIS监控远程服务器磁盘空间并发送邮件报警

    本文直接参考了博客园软件人生的文章操作的,写在这里只为做个记录. 到公司这边先把两个报表服务器接收了. 为防止宕机,部署个磁盘警告的SSIS包. Step 1 建立两个变量来接收和写入磁盘容量 Ste ...