I recently posted a question on OJ of Nanyang sci-tech. I found that most of the answers to a question that is interesting on the Internet use stacks. Unfortunately, I have not learned the data structure yet, therefore, it can only be solved in a simple way.
Question link on this http://acm.nyist.net/JudgeOnline/problem.php? Pid = 2
The questions are as follows:
Description:
Now, there is a sequence of parentheses. Please check whether this line of parentheses is paired.
Input:
Enter N (0 <N <= 100) in the first row, indicating that N groups of test data exist. The next N rows Input Multiple groups of input data, each group of input data is one
String S (S length is less than 10000, and S is not an empty string), the number of test data groups is less than five. Data guarantee S only contains "[", "]", "(", ")" 4
.
Output:
The output of each group of input data occupies one row. If the brackets contained in the string are paired, Yes is output. If the string is not paired, No is output.
Example:
3
[(])
(])
([[] ()])
No
No
Yes
The Code is as follows:
#include<stdio.h>#include<string.h>#include<stdlib.h>int main(){ int n; char str[10000]; void deal( char str[]); scanf("%d",&n); while(n--) { scanf("%s",str); deal( str); }}void deal( char str[]){ int len = strlen( str); int i,j,n,flag; if( len%2 != 0 || str[0] == ')' || str[0] == ']' ) { printf("No\n"); } else { for( i = 0; i < len; i++) { if( str[i] == ')' || str[i] == ']') { for( j = i-1; j > -1; j--) { if( str[j] == '0' ) continue; if( (int)(str[i] - str[j]) != 1 && (int)(str[i] - str[j]) != 2) { printf("No\n"); return; } else { str[i] = str[j] = '0'; break; } } } } if( str[ len-1] == '0' ) printf("Yes\n"); }}