Moderator: Board moderators
(*(*))
((*)
([)]
([ ])()(**)
*)
[
(
If the expression is not properly nested your program should determine the position of the offending bracket, that is the length of the shortest prefix of the expression that can not be extended to a properly nested expression.
(*a++(*)
(*a{+}*)
(*(*))
((*)
([)]
([ ])()(**)
*)
[
(
[
NO 6
YES
NO 3
NO 3
NO 3
YES
NO 1
NO 3
NO 11
NO 2
obayashi wrote:and what about such cases below?
()a
a()
()*
()*() <--- it seems that "*" is bracketed, but actually NOT...
my program gives
NO 3
NO 1
NO 3
NO 3
...
[cpp]
#include <stdio.h>
char stack[1000];
int top;
void push(char a)
{
stack[top++]=a;
if(top==1000)
while(1);
}
char pop(void)
{
if(top<0)
return -1;
else
return stack[--top];
}
int main()
{
int pos;
char a[3000];
int flag;
while(gets(a))
{
top=0;
pos=0;
flag=0;
int i=0;
while(a[i])
{
switch(a[i++])
{
case '(':
if(a[i]=='*')
{
push('*');
i++;
}
else
push('(');
pos++;
break;
case '<':
push('<');
pos++;
break;
case '[':
push('[');
pos++;
break;
case '{':
push('{');
pos++;
case '*':
pos++;
if(a[i]==')')
{
i++;
if(pop()!='*')
{
printf("NO %d\n",pos);
flag=1;
}
}
else
{
goto default1;
}
break;
case ')':
pos++;
if(pop()!='(')
{
printf("NO %d\n",pos);
flag=1;
}
break;
case '}':
pos++;
if(pop()!='{')
{
printf("NO %d\n",pos);
flag=1;
}
break;
case '>':
pos++;
if(pop()!='<')
{
printf("NO %d\n",pos);
flag=1;
}
break;
case ']':
pos++;
if(pop()!='[')
{
printf("NO %d\n",pos);
flag=1;
}
break;
default:
pos++;
default1:
if(top<=0)
{
printf("NO %d\n",pos);
flag=1;
}
}
if(flag)
break;
}
if(flag!=1)
{
if(top==0)
printf("YES\n");
else
printf("NO %d\n",pos);
}
}
return 0;
}
[/cpp]obayashi wrote:i think the cases above should require "NO" instead of "YES" 'coz the characters are not actually bracketed in pairs of brackets...

Users browsing this forum: No registered users and 1 guest