Problem Type : Stack
#include<stdio.h>
#include<string.h>
char stack[2000];
int top=0,size=100000;
void push(char data)
{
stack[top]=data;
top++;
}
void pop()
{
top=top-1;
stack[top]='\0';
}
int main()
{
int i,j,k,len,n,m;
char ch[100];
memset(stack,'\0',sizeof(stack));
memset(ch,'\0',sizeof(ch));
scanf("%d",&n);
getchar();
for(j=0; j<n; j++)
{
gets(ch);
len=strlen(ch);
for(i=0; i<len; i++)
{
if(top==0)
{
push(ch[i]);
}
else if(stack[top-1]=='('&& ch[i]==')')
{
pop();
continue;
}
else if(stack[top-1]=='['&& ch[i]==']')
{
pop();
continue;
}
else
push(ch[i]);
}
if(top==0)
printf("Yes\n",top);
else
printf("No\n",top);
top=0;
}
return 0;
}
#include<stdio.h>
#include<string.h>
char stack[2000];
int top=0,size=100000;
void push(char data)
{
stack[top]=data;
top++;
}
void pop()
{
top=top-1;
stack[top]='\0';
}
int main()
{
int i,j,k,len,n,m;
char ch[100];
memset(stack,'\0',sizeof(stack));
memset(ch,'\0',sizeof(ch));
scanf("%d",&n);
getchar();
for(j=0; j<n; j++)
{
gets(ch);
len=strlen(ch);
for(i=0; i<len; i++)
{
if(top==0)
{
push(ch[i]);
}
else if(stack[top-1]=='('&& ch[i]==')')
{
pop();
continue;
}
else if(stack[top-1]=='['&& ch[i]==']')
{
pop();
continue;
}
else
push(ch[i]);
}
if(top==0)
printf("Yes\n",top);
else
printf("No\n",top);
top=0;
}
return 0;
}
No comments:
Post a Comment