Note: Empty line to output yes, to use get Read function, this problem let me learn to get and scanf function in addition to the space problem or
have a the difference between, scanf put the space, Tab, enter all as the end flag, will not be read into them, the back automatically add ' \ ', so space, enter, Tab
are in buffer, which can be read with GetChar (), but the Get () function will also treat the carriage return as the end flag, but they will read the
into, read The string is ' GetChar ', so they will not exist in the buffer, that is, they can not be read with the ...
Release code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stack>using namespace std; Char A[130];int main () {stack <char> s;int n,flag,i;char ch;scanf ("%d", &n); GetChar (); while (n--) {flag=1;gets (a);//SCANF ("%s", a), if (a[0]== ') {puts ("yes"); continue;} Flag=0;int len = strlen (a); for (i=0; i<len; i++) {if (a[i]== ' (' | | a[i]== ' [') {S.push (a[i]);} Else{if (S.empty ()) {puts ("No"); flag=1;break;} Else{ch = S.top (); S.pop (); if (a[i]== ') ') {if (ch!= ' (') {puts ("No"); flag=1;break;}} Else{if (ch!= ' [') {puts ("No"); flag=1;break;}}}} if (!flag) { if (S.empty ()) puts ("Yes"); Elseputs ("No"); } while (!s.empty ()) { s.pop (); }
UVA 673 Bracket Matching problem