Input: The first line enters a number n (0<n<=100), indicating that there are n sets of test data. The next n lines enter multiple sets of input data, each set of input data is a string s (S is less than 10000, and S is not an empty string), and the number of test data groups is less than 5 groups. The data guarantees that s contains only "[", "]", "(", ")" four characters.
Output: The output of each set of input data is a row, if the string contains parentheses are paired, the output is yes, if not paired output no
Sample input:
3[(]) (]) ([[[] ()])
Sample output:
Nonoyes
This is the Nanyang Polytechnic Institute of ACM Exercises, my procedures are as follows:
In practicing this problem, I learned the knowledge and usage of ① stack; the method of opening and releasing dynamic space of② two-dimensional array;③ The most important thing is to learn this way of thinking, Mark.
#include <stdio.h>#include<stdlib.h>#include<string.h>#defineTRUE 1#defineFALSE 0#defineStackSize 10000typedefstruct{ CharData[stacksize]; inttop;} Sqstack;intNUM1 =0, num2 =0;voidInitstack (Sqstack *s);intStackempty (Sqstack *s);intStackfull (Sqstack *s);intPush (Sqstack *s,Charx);CharPop (Sqstack *s);intStacktop (Sqstack *s,Char*x);voidBracket (sqstack *s,Char*a);intMainvoid){ intN =0; inti =0, j =0, k =0; int*length; Char**s; Sqstack S; scanf ("%d",&N); //Open two-bit array dynamic memory space, n rows 10000 columnss = (Char**)malloc(sizeof(Char*)*N); for(i =0; I < n;i++) S[i]= (Char*)malloc(sizeof(Char)*10000); for(i =0; I < n;i++) { if('\ n'==GetChar ()) {scanf ("%s", S[i]); } } for(i =0; I < n;i++) {bracket (&S, s[i]); if(NUM1 = =0&& num2 = =0) {printf ("yes\n"); } Elseprintf ("no\n"); NUM1= Num2 =0; } System ("Pause"); for(i =0; I < n;i++) Free(S[i]); Free(s); return 0;}voidBracket (sqstack *s,Char*a) { intI,j,length =strlen (a); Initstack (s); for(i =0; I < length;i++) { Switch(A[i]) { Case '[':P Ush (S,a[i]); num1++; Break; Case '(':P Ush (S,a[i]); num2++; Break; Case ']': if('['= = Pop (s) | | FALSE = =Pop (s)) {NUM1--; } Break; Case ')': if('('= = Pop (s) | | FALSE = =Pop (s)) {num2--; } Break; default: Break; } }}voidInitstack (Sqstack *s) {s->top =-1;//Initialize the order stack to be empty}intStackempty (Sqstack *R) { if(S->top = =-1) returnTRUE; Else returnFALSE;}intStackfull (Sqstack *s) { if(S->top = = stacksize-1) returnTRUE; Else returnFALSE;}intPush (Sqstack *s,Charx) { if(Stackfull (s)) {//printf ("stackfull~\n"); returnFALSE; } Else{s->top++; S->data[s->top] =x; returnTRUE; }}CharPop (Sqstack *s) { if(Stackempty (s)) {//printf ("stackempty~\n"); returnFALSE; } Else { returns->data[s->top--]; }}intStacktop (Sqstack *s,Char*x) { if(Stackempty (s)) {//printf ("stackempty~\n"); returnFALSE; } Else { *x = s->data[s->top]; returnTRUE; }}
ACM Brace pairing problem