-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBalanced Brackets
More file actions
90 lines (82 loc) · 1.51 KB
/
Copy pathBalanced Brackets
File metadata and controls
90 lines (82 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
struct node{
int data;
struct node *link;
};
struct node *head=NULL;
void push(char x)
{
struct node* p=(struct node*) malloc (sizeof(struct node));
if(p==NULL)
{
printf("overflow");
return;
}
p->data=x;
// p->link=NULL;
p->link=head;
head=p; //here head acts like top
}
char pop()
{
char item;
struct node *p;
if(head==NULL){
//printf("underflow");
return -1;
}
item=head->data;
p=head;
head=head->link;
free(p);
return item;
}
int match(char x,char y)
{
if((x=='{'&&y=='}')||(x=='['&&y==']')||(x=='('&&y==')')){
return 1;
}
else
return 0;
}
int check()
{
char value,arr[10000];
scanf("%s",arr);
int i=0;
while(arr[i])
{
value=arr[i];
if(value=='('||value=='['||value=='{')
push(value);
if(value==')'||value==']'||value=='}'){
//printf("P%c \n",value);
if(head==NULL)
return 0;
else if(match(pop(),value)==0)
return 0;
}
i++;
}
if(head==NULL)
return 1;
else
return 0;
}
int main()
{
int x,T,t=0,n,i;
scanf("%d",&n);
while(n--){
if(check())
printf("YES");
else
{ printf("NO");while(head!=NULL){pop();}};
printf("\n");
}
return 0;
}