Brace pairing problemtime limit: ms | Memory limit:65535 KBDifficulty:3
-
- Describe
- now, with a sequence of parentheses, you should check that the line brackets are paired.
- input
- first line Enter 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 in the string are paired, the output is yes, and if not paired, output no
-
-
3[(]) (]) ([[] ()])
-
-
nonoyes
- source
- network
- uploader
- naonao
Ideas:This problem is a flexible use of the stack of the topic, the stack is a special linear table, it only a mouth tube in and out of the data, so it has a backward features! The first thing to understand in this question is what is called a pair of parentheses, because at first I made a big mistake when I understood it wrong, so in this point it is important to note that the parentheses are paired with an opening parenthesis in front of the closing parenthesis (I understand at first that this is true!). ), and then you need to put the parentheses from the first (element No. 0) into the stack, if the first one to satisfy a closing parenthesis is an opening parenthesis, then out of the stack, continue to move the parentheses in the original string progress until the end of the string loop, if the stack is empty, the stack is fully satisfied with the topic requirements, so output yes; !
Code:
#include <stdio.h> #include <string.h> char a[10005],b[10005]; int main () { int len,top,n,i,j,k; scanf ("%d", &n); while (n--) {scanf ("%s", &a); Len=strlen (a); if (len%2!=0) printf ("no\n"); else {if (a[0]== ') ' | | a[0]== '] ') printf ("no\n"); Top=1; B[0]=A[0]; for (i=1;i<len;i++) {b[top]=a[i]; if (top==0) top++; else {if (b[top-1]== ' (' &&b[top]== ') ') top--; else if (b[top-1]== ' [' &&b[top]== '] ') top--; else top++; }} if (top==0) printf ("yes\n"); else printf ("no\n"); }} return 0; }
Search
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
NY 2 Brace pairing problem