poj 1363
来源:互联网 发布:免费制作网站软件 编辑:程序博客网 时间:2024/05/17 22:25
终于考试完了,又可以学习啦!
poj1363,这道题是数据结构与算法中的经典问题,给定一组进栈顺序,问栈有多少种出去的顺序,而这个题是选择题中常考“下列出栈顺序中不合法的是?”。
拿到之后最直接的思路就是模拟进出栈,代码AC了,但是时间为94ms。感觉不应该啊,这么简单的题,必定有更高效的算法。
可是怎么更高效呢?
/* * ===================================================================================== * * Filename: 1363.c * * Description: * * Version: 1.0 * Created: 2011年12月27日 16时09分08秒 * Revision: none * Compiler: gcc * * Author: MaZheng (blog.csdn.net/mazheng1989), mazheng19891019@gmail.com * Company: Dalian University Of Technology * * ===================================================================================== */#include<stdio.h>#defineNUM 1005/* *///please declare parameters here.char input[NUM];int output[NUM];int stack[NUM];//please declare functions here.int main(){ if(freopen("input.txt","r",stdin)==NULL) perror("Can not open the input file!"); //input your ...int N;int i,j;int top;while(scanf("%d",&N)&&N!=0){while(scanf("%d",&output[1])&&output[1]!=0){for(i=0;i<=N;i++)input[i]=0;top=0;for(i=2;i<=N;i++){scanf("%d",&output[i]);}/*for(i=1;i<=N;i++)printf("%d ",output[i]);printf("\n");*/for(i=1;i<=N;i++){for(j=1;j<=output[i];j++){if(input[j]==0){stack[top++]=j;input[j]=1;}}if(top<=0||stack[--top]!=output[i]){break;}}if(i!=N+1){printf("No\n");//printf("%d %d\n",i,top);}else{printf("Yes\n");}}printf("\n");}return 0;}
- POJ 1363
- poj 1363
- POJ 1363
- poj 1363
- POJ 1363
- poj 1363
- POJ 1363
- poj 1363
- poj 1363
- POJ 1363
- POJ-1363
- POJ-1363
- POJ 1363
- Poj 1363 Rails
- 【poj】1363-Rails
- POJ 1363 Rails
- POJ 1363(栈)
- [poj] 1363 Rails
- Java 基础一些代码练习笔记(ArrayEquals)
- C#格式化数值结果表
- Class.create(),Object.extend()用法小结
- Struts2的中文乱码解决
- 阻止分段攻击HotWC3网站注册散列数的依据
- poj 1363
- 深入详解保护模式下的内存分页机制
- linux ubuntu上如何安装pyWebQQ
- 关于struts2 中result type="stream"
- hibernate对应的ORDBMS的方言
- 数据库迁移大总结。。(sqlserver2000迁移到-2008-mysql5.0)
- 重构第十一章学习
- [AIX] ksh中的命令行编辑
- 字符编码详解及由来