Poj Solution 2372

http://poj.org/problem?id=2372

#include <stdio.h>
#include <string.h>


char w[1000], *symbol="=+-*/0123456789n";

bool check_comment(int &s)
{
    do
    {
        while( w[s] && w[s] != '*' )
            s++;
        
        if( !w[s] || !w[ s+1 ] ) return false;
        else if( w[ ++s ] == ')' ) return true;

    }while(1);
    
    return true;
}

bool check_expression(int &s)
{
    do
    {
        while( w[s] && w[s] !='(' && w[s] != ')' )
        {
            if( !strrchr( symbol, w[s] ) ) return false;
            s++ ;
        }
        
        if(w[s] == ')' ) return true;
        
        if( !w[s] || !w[s+1] ) return false;
        
        if( w[ ++s ] == '*' )
        {
            s++;
            if( !check_comment( s ) ) return false;
        }
        else if( !check_expression( s ) ) return false;
        
        s++;

    }while(1);
}

bool check()
{
    int s = 0;
    while( w[s] )
    {
        if( w[s] == ')' ) return false;

        if( w[s] == '(' )
        {
            if( w[s+1] && w[s+1] == '*' )
            {
                s+=2;
                if( !check_comment( s ) ) return false;
            }
            
            else
            {
                s++;
                if( !check_expression( s ) ) return false;
            }
        }
        
        s++;
    }
    
    return true;
}

void init()
{
    int i;
    for( i=0; scanf("%1c",&w[i]) == 1 && w[i] != '#' ; i++ )
        ;

    w[i] = '';
}

int main()
{
    init();
    
    if( check() ) printf( "YESn" );
    else printf( "NOn" );
    
    return 0;
}
											
This entry was posted in poj. Bookmark the permalink.