Bracket pairing problem time limit:MS | Memory limit:65535 KB Difficulty:3description now, with a sequence of parentheses, please check that the line brackets are paired.
-
-
Input
-
-
the first line enters a number n (0<n<=100), which indicates 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. Data guarantee S contains only "[", "]", "(", ")" Four characters
-
-
Output
-
The
-
output of each set of input data is one row, if the parentheses contained in the string are paired, the output is yes, and if you do not pair the output no
-
-
Sample input
-
-
3[(]) (]) ([[[] ()])
-
-
Sample output
-
-
Nonoyes
The first time I was using the array and then traverse the search, to consider a number of situations, today, two seniors to tell us the stack, the feeling of Meng Meng da!
-
1#include <stdio.h>2#include <string.h>3 Charstack[10001],a[100001];//After the array is defined as far as possible outside the function4 intMain ()5 {6 intn,i,j;7scanf"%d",&n);8 GetChar (); absorbing carriage return characters9 while(n--)Ten { One gets (a); A inttop=1; -stack[0]=a[0]; - for(i=1; I<strlen (a); i++) the { -stack[top]=A[i]; - if(stack[top-1]=='['&&stack[top]==']') -top--; + Else if(stack[top-1]=='('&&stack[top]==')') -top--; + Else Atop++; at } - if(top==0) -printf"yes\n"); - Else -printf"no\n"); - } in return 0; -}
Bracket pairing Problem--nyoj-Topic 2