Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include char stack[300]; int top=-1; int ans=0; void push(char c){ top++; stack[top]=c; } char pop(){ char r; r=stack[top]; stack[top]='#';//set the orginal one invalid top--;
#include
#include
char stack[300];
int top=-1;
int ans=0;
void push(char c){
top++;
stack[top]=c;
}
char pop(){
char r;
r=stack[top];
stack[top]='#';//set the orginal one invalid
top--;
return r;
}
void initiate(){
for(int u=0;u
stack[u]='c';
}
}
int main() {
// Write C code here
int num,x;
printf("number of cases ");
scanf("%d",&num);
getchar();/ewline
for(int i=0;i
printf("enter num of brackets ");
scanf("%d",&x);
getchar();
if(x==0){
printf("%d",0);
break;}
printf("enter the brackets ");
char s[x];
gets(s);
for(int j=0;j
Exercise 2.29 Making Parentheses String Balanced Given a string S that only consists of open parentheses "(" and close parentheses ")". you are asked to determine the minimum number of additional parentheses noeded to make the parentheses string S balanced. Note that the additional parentheses can be added to any position of string S, and can be either open parentheses "(" or close parentheses ")". Formally speaking, a parentheses string S is balanoed if and only if - S is an empty string, or - S=AB, where A and B are both balanced parentheses strings, or - S=(A), where A is a balanced parentheses string. Input The first line is an integer N(1N103), indicating the number of test cases. Each test case contains two lines. The first line is an integer m(0m200), the length of the parentheses string. The second line is a potentially unbalanced parentheses string. Output The minimum number of additional parentheses ("(" or ")") needed to make the corresponding parentheses string balanced, by adding them to any position of the string. Sample Input 4 3 (() 3 ((( 2 0 6 ))((0) Sample Output 1304 Explanation There are \& test cases. For ((), adding one ) to the end to make it balanced: (()). For ((C, adding three ) to the end to make it balanced: ((())). For O, it is already balanced, no parenthesis is needed. For ))((O), adding two ( and two ) to make it balanced: (())(())() if(s[j]=='('){
push(s[j]);
continue;
}
if(top!=-1){
if(stack[top]=='('){
pop();
continue;
}else if(stack[top]==')'){
push(s[j]);
continue;
}
}
if(top==-1){
push(s[j]);
}
}
for(int m=0;m
if(stack[m]=='('||stack[m]==')'){
ans++;
}
}
printf("%d ",ans);
initiate();
top=-1;
ans=0;
}
return 0;
} want to ask what's wrong with my code . It passes the given test cases but the pc^2 team system still says it is wrong.i have tried edge cases and it still works
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started