Bracket pairing problem time limit:MS | Memory limit:65535 KB Difficulty:3
-
Describe
-
now, with a sequence of parentheses, you should 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
Code:
01.
#include<stdio.h>
02.
#include<string.h>
03.
char
m[10010],n[10010];
04.
int
top;
05.
void
push(
char
x){top++;
06.
m[top]=x;
07.
}
08.
void
pop(
char
*x){*x=m[top];
09.
top--;
10.
}
11.
int
main(){
12.
int
T,i,flot;
char
temp;
13.
scanf
(
"%d"
,&T);
14.
while
(T--){i=top=0;flot=1;
15.
memset
(m,0,
sizeof
(m));
16.
scanf
(
"%s"
,n);
17.
while
(n[i]){
18.
if
(n[i]==
‘[‘
||n[i]==
‘(‘
)push(n[i]);
19.
else
{pop(&temp);
20.
if
(temp==
‘(‘
&&n[i]==
‘]‘
)flot=0;
21.
if
(temp==
‘[‘
&&n[i]==
‘)‘
)flot=0;
22.
}
23.
/* printf("%d m[%d]=%c ",i,top,m[top]);*/
i++;
24.
}
if
(top!=0)flot=0;
25.
if
(flot)
printf
(
"Yes\n"
);
26.
else
printf
(
"No\n"
);
27.
}
28.
return
0;
29.
}
time is 0;
01.
#include<stdio.h>
02.
#include<string.h>
03.
char
m[10010],n[10010];
04.
int
top;
05.
void
push(
char
x){top++;
06.
m[top]=x;
07.
}
08.
void
pop(){
09.
top--;
10.
}
11.
int
main(){
12.
int
T,i;
13.
scanf
(
"%d"
,&T);
14.
while
(T--){i=top=0;
15.
memset
(m,0,
sizeof
(m));
16.
scanf
(
"%s"
,n);
17.
while
(n[i]){
18.
if
(n[i]==
‘]‘
&&m[top]==
‘[‘
)pop();
19.
else
if
(n[i]==
‘)‘
&&m[top]==
‘(‘
)pop();
20.
else
push(n[i]);
21.
/* printf("%d m[%d]=%c ",i,top,m[top]);*/
i++;
22.
}
23.
if
(top==0)
printf
(
"Yes\n"
);
24.
else
printf
(
"No\n"
);
25.
}
26.
return
0;
27.
}
two different code time comparisons:
Brace pairing problem (stack)