Simple implementation of bracket matching algorithm without using stack

Source: Internet
Author: User

Just do the data structure work when there is such a book exercise, the title is as follows:

3.2 assumes that I and O are respectively represented in the stack and the stack operation, the initial and final stack of the stack are empty, the sequence of the stack and the stack can be expressed as a sequence consisting of only I and O.

(1) What is legal in the sequence shown below?

A. ioiioioo B. Iooioiio c. iiioioio D. iiiooioo

(2) Through the analysis of (1), write an algorithm to determine whether the given sequence of operations is legal. If the law returns 1, 0 is returned. (assuming that the sequence of operations determined has been deposited into an array).

The first question thinks about it, AD

The second question originally wanted to use the stack to do, but did not seem necessary ... I used an int variable count instead of the stack, and the code is as follows (C + +):

1#include <iostream>2#include <cstring>3 /*4 Name:stack operate sequence legal judge5 Copyright:guoji6 Author:guoji7 DATE:11/10/15 18:408 Description:9 */Ten  One using namespacestd; A  - BOOLJudgeChar*arr,intN) - { the     intCount =0; -      for(inti =0; I < n; i++) { -         if(Arr[i] = ='I') { -count++; +         } -         Else if(Arr[i] = ='O') { +             if(Count = =0) A                 return 0; at             Else -count--; -         } -         Else{//illegal Character -             return 0; -         } in     } -      to     return(Count = =0);  + } -   the  * intMain () $ {Panax Notoginseng     stringinput; -          theCIN >>input; Input from keyboard +      A     Char*operate =New Char[Input.length ()];  the strcpy (Operate, input.c_str ()); String to char array +      -cout <<"Result:"<< judge (operate, Input.length ()) <<Endl;  $     //Print the Judge Redult $  -     return 0;  -}

Test the data with the first question for nothing. After finishing the homework, I want to write the parentheses to match, and so on, just the second question how a bit ... Familiar with? It's a little bit like a brace match. The parentheses match another version: "I-o" matches!!! Then changed the function of the judgment, "I" with the left parenthesis "(" substitution, "O" with the right parenthesis ")" substitute, remove the illegal character judgment ...

1 BOOLJudgeChar*arr,intN)2 {3     intCount =0;4      for(inti =0; I < n; i++) {5         if(Arr[i] = ='(') {6count++;7         }8         Else if(Arr[i] = =')') {9             if(Count = =0)Ten                 return 0; One             Else Acount--; -         } -     } the      -     return(Count = =0);  -}

Write a few expressions to test a bit, can judge.

Thought analysis: Because only need to carry out the stack operation and do not involve the specific elements, the result is based on whether the stack is empty to judge, so you can use an int variable instead of the stack, with self-addition and self-subtraction instead of the stack and the stack operation, the variable is 0 to indicate that the stack is empty. But this kind of writing can only recognize one kind of parentheses (character), and how to feel not to use the stack so rigorous, to play with the homework should still be able to ...

  

Simple implementation of bracket matching algorithm without using stack

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.