ZOJ 1259 Rails

stack的应用

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<stack>
 4 using namespace std;
 5 int goal[1005];
 6 int d[1005];
 7 stack<int>s;
 8 int main()
 9 {
10     freopen("input.txt","r",stdin);
11     int n;
12     for(int i = 0; i <= 1000; i++)
13         d[i] = i;
14     while(cin>>n && n)
15     {
16         while(cin>>goal[1] && goal[1])
17         {
18             while(!s.empty())
19                 s.pop();
20             for(int i = 2; i <= n; i++)
21                 cin>>goal[i];
22             int i = 2;
23             int pos = 1;
24             s.push(d[1]);
25             while(pos <= n && i <= n+1)
26             {
27                 if(s.empty() || s.top() != goal[pos])
28                 {
29                     s.push(d[i]);
30                     i++;
31                 }
32                 else if(s.top() == goal[pos])
33                 {
34                     s.pop();
35                     pos++;
36                 }
37                 else break;
38             }
39             if(s.empty())
40                 cout<<"Yes"<<endl;
41             else cout<<"No"<<endl;
42
43         }
44         cout<<endl;
45     }
46     return 0;
47 }

ZOJ 1259 Rails

时间: 2024-08-04 18:00:12

ZOJ 1259 Rails的相关文章

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ1363 Rails 验证出栈序列问题(转)

题目地址: http://poj.org/problem?id=1363 此题只需验证是否为合法的出栈序列. 有两个思路:1.每个已出栈之后的数且小于此数的数都必须按降序排列.复杂度O(n^2),适合人脑. //思路 1 不对!!! 例如 数据 ,               3 5 2 4 1              --------                正确答案为 no 2.另一个思路就是直接模拟入栈出栈过程.虽然模拟毫无技巧可言,但复杂度O(n),优于算法1.适合电脑. 代码如

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

RailsCast26 Hackers Love Mass Assignment rails中按params创建、更新model时存在的安全隐患

Mass assignment是rails中常用的将表单数据存储起来的一种方式.不幸的是,它的简洁性成了黑客攻击的目标.下面将解释为什么及如何解决. 上述表单为一个简单的注册表单.当用户填入name,点击提交时,一个新用户被创建.用户模型被如下定义: ruby create_table :users do |t| t.string :name t.boolean :admin, :default => false, :null => false end 当用户点击提交时,如下的action被执

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n