02-Linear Structure 4 Pop Sequence

Source: Internet
Author: User

Given a stack which can keepM numbers at the most. Pushn numbers in the order of 1, 2, 3, ...,  < Span class= "Katex" >n and pop randomly. You were supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, If  m is 5 and  n is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, and not 3, 2, 1, 7, 5, 6, 4.

Input Specification:

Each input file contains the one test case. For each case, the first line contains 3 numbers (all no more than 1000):M (the maximum capacity of the stack),N   (the length of push sequence), And < Span class= "Strut bottom" >k  (the number of Pop sequences to be checked). Then k lines follow, each contains a pop sequence of < Span class= "Katex" >n numbers. All the numbers in a line is separated by a space.

Output Specification:

For each pop sequence, print on one line "YES" if it is indeed a possible pops sequence of the stack, or "NO" if not.

Sample Input:
5 7 51 2 3 4 5 6 73 2 1 7 5 6 47 6 5 4 3 2 15 6 4 3 7 2 11 7 6 5 4 3 2
Sample Output:
YESNONOYESNO
1#include <stdio.h>2#include <stdlib.h>3 structsnode{4     int*data;5     inttop;6     intmaxsize;7 };8typedefstructSNode *Stack;9 Ten //initializing the Stack function OneStack Createstack (intmaxsize) { AStack S = (stack)malloc(sizeof(structSNode)); -S->data = (int*)malloc(sizeof(int)*maxsize); -S->top =-1; theS->maxsize =maxsize; -     returnS; - } -  + //determine if the stack is an empty function - BOOLIsempty (Stack S) { +     return(S->top = =-1); A } at  - //into the Stack function - voidPush (Stack S,inttemp) { -s->data[++ (s->top)] =temp; - } -  in //Out Stack function - voidpop (Stack S) { to     if(s->top==-1)return; +     Else { -s->top--; the     } * } $ Panax Notoginseng intMain () { -     intM, N, K;  thescanf"%d%d%d", &m, &n, &K); +Stack S =Createstack (M); A      while(k--){ the         BOOLFlag =true;  +         inttemp =1, num; -          for(intI=0; i<n; i++) {//Loop reads each number of push sequence $scanf"%d", &num); $              while(s->top<s->maxsize && flag)//When the stack is not satisfied, and the preceding elements are possible out of the stack sequence -             { -                 if(Isempty (S) | | S->data[s->top]!=num)//if the stack is empty, or the top element of the stack is not equal to Num the                 { -Push (S, temp++);//Press temp into the stack in ascending orderWuyi                 } the                 Else if(S->data[s->top]==num)//until the top of the stack element equals num -                 { WuPop (S);//put elements out of the stack -                      Break;  About                 } $             } -             if(s->top>=s->maxsize)//If temp has been pressed to stack full, the out-of-stack elements that meet Num are still not found -             { -Flag =false;//indicates that the sequence is not a possible stack sequence, setting flag to False A             } +         } the         if(flag) printf ("yes\n");  -         Elseprintf"no\n"); $          while(! Isempty (S))//after each sequence is judged, the stack is emptied the         { the pop (S); the         } the     } -     return 0;  in}

02-Linear Structure 4 Pop Sequence

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.