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

help me with test #1, please
Posted by Squid 3 Jan 2006 21:29
i've tested my program on all tests contained in the forum.
but still i got WA on test #1

#include <iostream>

enum State {ERROR, TEXT, BRACKET, ARITHMETIC, COMMENT, STAR};

int main() {
    State state = TEXT;
    char ch;
    int count = 0;    // number of nested brackets

    while (!std::cin.eof()) {
        std::cin >> ch;
        if (ch == '#') // for testing only
            break;
        if (state == TEXT) {
            if (ch == ')') {
                state = ERROR;
                break;
            } else if (ch == '(') {
                state = BRACKET;
                count++;
            }
        } else if (state == BRACKET) {
            if (ch == ')') {
                count--;
                if (count == 0)
                    state = TEXT;
            } else if (ch == '(') {
                count++;
            } else if ((ch >= '0' && ch <= '9') || (ch == '=') || (ch == '+') ||
                (ch == '-') || (ch == '/') || (ch == '\n') || (ch == '\r')) {
                    state = ARITHMETIC;
            } else if (ch == '*') {
                count--;
                state = COMMENT;
            } else {
                state = ERROR;
                break;
            }
        } else if (state == ARITHMETIC) {
            if (ch == ')') {
                count--;
                if (count == 0)
                    state = TEXT;
            } else if (ch == '(') {
                count++;
                state = BRACKET;
            } else if ((ch >= '0' && ch <= '9') || (ch == '=') || (ch == '+') ||
                (ch == '-') || (ch == '*') || (ch == '/') || (ch == '\n') || (ch == '\r')) {
                //nothing
            } else {
                state = ERROR;
                break;
            }
        } else if (state == COMMENT) {
            if (ch == '*') {
                state = STAR;
            }
        } else if (state == STAR) {
            if (ch == ')') {
                state = (count > 0) ? ARITHMETIC : TEXT;
            } else if (ch == '*') {
                // nothing
            } else {
                state = COMMENT;
            }
        }
    }

    if (state == TEXT) {
        std::cout << "YES";
    } else {
        std::cout << "NO";
    }

    return 0;
}
Re: help me with test #1, please
Posted by Ras Misha [t4ce] 16 Jan 2006 14:14
(123
321)

asnwer - YES
Re: help me with test #1, please
Posted by Squid 19 Jan 2006 02:56
On your test my program worked correctly.
But anyway, you help me to find my bug.
Thanks a lot.
PS
I used std::cin and it ignores spaces... :-/