02-Linear Structure 4 Pop Sequence (25 min)

Source: Internet
Author: User

02-Linear Structure 4 Pop Sequence (25 min)

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),< Span class= "Mord mathit" >n (the length of push sequence), And 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



usingSystem;usingSystem.Collections.Generic;usingSystem.Text;classt{Static voidMain (string[] args) {        varline = Console.ReadLine (). Split (New[] {" "}, Stringsplitoptions.removeemptyentries); varMaxnum =int. Parse (line[0]); varLineNum =int. Parse (line[1]); varLineCount =int. Parse (line[2]); List<string> list =Newlist<string>();  for(inti =0; i < LineCount; i++) {list.        ADD (Console.ReadLine ()); }        foreach(varIteminchlist) {                       stringY ="YES"; varNUMSS = Item. Split (New[] {" "}, Stringsplitoptions.removeemptyentries); List<int> nums =Newlist<int>(); foreach(varItem2inchNUMSS) {nums. ADD (int.            Parse (ITEM2)); }             for(inti =0; I < Nums. Count; i++)            {  if(I >0)                {                    if(Nums[i-1]-Nums[i] >1) {Y="NO";  Break; }                }                if(Nums[i] >maxnum) {Y="NO";  Break; }                Else                {                     for(intj = i +1; J < Nums. Count; J + +)                    {                        if(Nums[i] <Nums[j]) {Nums[j]--;        }}}} Console.WriteLine (Y); }    }}

 

02-Linear Structure 4 Pop Sequence (25 min)

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.