Description 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
Problem Analysis: The beginning may not start, careful analysis may have a variety of ideas, my train of thought is to find a variable initial 0, encountered ' (', ' [' Plus one, encounter ') ' or ') ' and compared with the front minus one, if the pairing will eventually get 0,
The answers are as follows:
#include <stdio.h>
#include <string.h>
#define P printf
#define S scanf
Main ()
{
int i,j,k,n, m,f;
Char s[10000];
S ("%d", &f); GetChar ();
While (f--) {
gets (s);
K=strlen (s); j=0;n=0;
int c=0,d=0;
For (i=0;i<k;i++)
{
if (s[i]== ' (')
j + +;
else if (s[i]== ' [')
n++;
else if (s[i]== ') ')
C + +;
Else
d++;
}int w=0,a[10000],v=0;
if (j==c&&n==d)//Determine if the corresponding number of symbols is equal
{
For (i=0;i<k;i++)
{
if (s[i]== ' | | | s[i]== ' [')
{
a[w]=s[i];w++;v++;
}
else if (s[i]== ') ')
{
if (a[w-1]== ' (')
{
w--;v--;
}
else {
puts ("No");
}
else if (s[i]== '] ')
{
if (a[w-1]== ' [')
{
w--;v--;
}
else {
puts ("No");
}
}if (v==0)
puts ("Yes");
}
Else
P ("no\n");}
}
Brace pairing problem