Sword refers to the offer series source code-the stack containing the Min function

Source: Internet
Author: User

Topic 1522: Stack time limit with min function: 1 seconds Memory limit: 128 Mega Special: No submission: 1495 resolution: 476 Title Description: Define the data structure of the stack, and implement a min function that can get the minimum stack element in the type. Input: The input may contain multiple test samples, and the input ends with EOF. For each test case, the first behavior of the input is an integer n (1<=n<=1000000), and N represents the number of steps for the operation to be entered. Next there are n lines, and each line begins with a letter ci. Ci= ' s ', then there is a number k, which means the k is pressed into the stack. Ci= ' O ' when the top element of the stack pops up. Output: corresponds to each action in each test case, and if the stack is not empty, the smallest element in the corresponding stack is output. Otherwise, the output is null. Sample input: 7s 3s 4s 2s 1oos 0 sample output: 3321230

#include <stdio.h> #include <iostream> #include <stack>using namespace Std;int main () {int n;        while (Cin>>n) {stack<int> s;            for (int i=0;i<n;i++) {char ch;            int num;            cin>>ch;                if (ch== ' s ') {cin>>num;                if (S.empty ()) {s.push (num);                }else if (s.top () <=num) {S.push (S.top ());                }else {s.push (num);            } printf ("%d\n", S.top ());                }else {if (S.empty ()) {printf ("null\n");                    }else{if (!s.empty ()) S.pop ();                    if (S.empty ()) {printf ("null\n");                    }else{printf ("%d\n", S.top ()); }                }            }        }    }}
OJ Address

Sword refers to the offer series source code-the stack containing the Min function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.