Pairing class issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.