Determine whether the order of the output stack of a sequence is legal

Source: Internet
Author: User
/***************** Determine whether pop is a legal sequence of the SRC stack ************ * ***************/bool ispopseq (string SRC, string pop) {stack <char> Sch; int Len = pop. length (); If (SRC. length ()! = Pop. length () return false; For (INT I = 0; I <Len; I ++) {int idx = SRC. find (POP [I]); If (idx = string: NPOs & Sch. empty () // return false if the initial sequence cannot be found and the stack is empty; else if (idx! = String: NPOs) {// if it is found in the initial sequence, the subsequent characters will be added to the stack in sequence, and the top element will be thrown out for (Int J = 0; j <= idx; j ++) Sch. push (SRC [J]); SRC. erase (SRC. begin (), SRC. begin () + idx + 1); Sch. pop ();} else if (idx = string: NPOs &&! Sch. Empty () {// if the initial sequence cannot be found and the stack is not empty, compare whether the top element of the stack is equal to the elements of the output stack sequence. If they are not equal, the result is invalid. Char CH = Sch. top (); If (CH = pop [I]) Sch. pop (); elsereturn false ;}} return true ;}# define judgeseq (SRC, pop) \ If (ispo1_q (SRC, pop )) cout <"is a valid seq" <Endl; \ else cout <"is a invalid seq" <Endl;

Related Article

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.