uva673 (parentheses Balance)-linear table

Source: Internet
Author: User


Parentheses Balance

You are given a string consisting of parentheses () and []. A string of this type was said to being correct: (a) If it is the empty string (b) If A and B were correct, AB is correct, (c) If A is correct, (a) and [a] is correct.

Write a program, takes a sequence of strings of this type and check their correctness. Your program can assume, the maximum string length is 128.

Input The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.

Output A sequence of Yes or No on the output file.

Sample Input

3 ([]) (([()]) ([[()]))
([() [] ()] () ()

Sample Output

Yes
No
Yes

#include <stdio.h>
#include <string.h>
#define MAX
int main () {
	int n;
	Char S[max];
	int top;
	scanf ("%d", &n);
	GetChar ();
	char c;
	int flag;
	while (n--) {
		flag=0;
		Top=-1;
		while ((C=getchar ())!=eof&&c!= ' \ n ') {
			if (top!=-1) {
				if (c== '] ') {
					if (s[top]== ' [')
						top-- ;
					else
						flag=1;
				}
				else if (c== ') ') {
					if (s[top]== ' (')
						top--;
					else 
						flag=1;
				}
				else{
					top++;
					s[top]=c;
				}
			}
			else{
				top++;
				s[top]=c;
			}
		}
		if (top!=-1) {
			flag=1;
		}
		if (flag==1)
			printf ("no\n");
		else
			printf ("yes\n");
	}
	return 0;
}

parentheses Balance

You are given a string consisting of parentheses () and []. A string of this type was said to being correct: (a) If it is the empty string (b) If A and B were correct, AB is correct, (c) If A is correct, (a) and [a] is correct.

Write a program, takes a sequence of strings of this type and check their correctness. Your program can assume, the maximum string length is 128.

Input The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a Lin E.

Output A sequence of Yes or No on the output file.

Sample Input

3 ([]) (([()]) ([[()]))
([() [] ()] () ()

Sample Output

Yes
No
Yes

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.