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
Pairing problem: Another array exists where the accesses than either is in both, and then uses the array (the later one of these types) to be paired with the original array (the sooner the other class appears). If the original array traversal at the end of a good match, then the pairing succeeds, or the standby array will either run out of time (j<0) or the alternate array does not die (j>0)
#include <stdio.h>
#include <string.h>
int main ()
{
Char s[10002],c[10002];
int I,n,len,j,flag;
scanf ("%d", &n);
while (n--)
{
scanf ("%s", s);
Len=strlen (s);
for (i=0,j=0,flag=1;i<len;i++)
{
if (s[i]== ' [' | | | s[i]== ' (')
{
C[j++]=s[i]; (equivalent to C[j]=s[i] J + +)//save one of the classes in an alternate array
}
else if (j>0 && (s[i]== '] ' && c[j-1]== ' [') | | (s[i]== ') ' && c[j-1]== ' ('))////
{
j--;
}
else/////////occurrences such as [with]
{
flag=0;
Break
}
}
if (flag==1&&j==0)
{
printf ("yes\n");
}
Else
{
printf ("no\n");
}
}
return 0;
}
Additional knowledge: the addition of the flag condition to the cyclic judgment condition facilitates early termination of unnecessary loops (as long as there is a non-conformance followed by no need for inspection). such as prime judgment
K=SQRT (n);
such as for (i=2;flag==1&&i<=k;i++)
{
if (n%i==0) flag=0;
}
Of course, you can also break directly;
Pairing class issues