ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1027. D++ Again

thwomass should I print YES for the string (**)*) ? [4] // Problem 1027. D++ Again 8 Jul 2004 18:32
the problem says that comments may contain every symbol so, comments may contain *)
so, should take the *) as we like so it will match as good as we can, or should we take *) as we meet it in the text?
Vlad Veselov [PMG17, Vinnitsa - KNU, Kiev] Correct answer is NO [3] // Problem 1027. D++ Again 9 Jul 2004 17:04
#include<stdio.h>

char ch;
int sem = 0;
char str[16] = "=+-*/0123456789";

int search(char c)
{
    int i;
    for(i = 0; i < 15; i++)
        if(c == str[i]) return 0;
    return 1;
}

void getChar()
{
    scanf("%c", &ch);
}

void comment()
{
    while(1)
    {
        getChar();
        while(ch == '*')
        {
            getChar();
            if(ch == ')') return;
        }
        if(feof(stdin)) { sem = 1; return; }
    }
}

void expr()
{
    while(ch != ')')
    {
        if(search(ch))
        {
            if(ch == ')') return;
            else if(ch == '(')
            {
                getChar();
                if(ch == '*') comment();
                else expr();
            }
            else sem = 1;
        }
        getChar();
        if(feof(stdin)) { sem = 1; return; }
    }
}

int main()
{
    do
    {
        getChar();
        if(feof(stdin)) break;
        if(ch == '(')
        {
            getChar();
            if(ch == '*')
            {
                comment();
            }
            else
                expr();

        }
        else if(ch == ')')
        {
            sem = 1;
            break;
        }
    }
    while(!feof(stdin));

    if(sem) printf("NO\n");
    else printf("YES\n");
    return 0;
}

Edited by author 09.07.2004 18:43
Try test:
(1+2+3
)

Your program will print NO, but true answer is YES.
You must control '\n'...
I had same problem...