-
Notifications
You must be signed in to change notification settings - Fork 0
/
week7-6.c
62 lines (58 loc) · 878 Bytes
/
week7-6.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int top=-1;
int n;
void push(char stack[],char x){
top=top+1;
stack[top]=x;
}
void pop(char stack[]){
top=top-1;
}
char peek(char stack[]){
return stack[top];
}
int isEmpty(){
if(top==-1){
return 1;
}
else{
return 0;
}
}
int size(){
return top+1;
}
int countReversals(char string[],char stack[]){
int len=strlen(string);
int i;
for(i=0;i<len;i++){
if(string[i]==')'&&!isEmpty()){
if(peek(stack)=='('){
pop(stack);
}
else{
push(stack,string[i]);
}
}
else{
push(stack,string[i]);
}
}
int red_len=size();
int n=0;
while(!isEmpty()&&peek(stack)=='('){
pop(stack);
n++;
}
return ((red_len/2)+n%2);
}
int main(){
char string[100];
scanf("%s",string);
n=strlen(string);
char stack[1000];
printf("%d\n",countReversals(string,stack));
return 0;
}