URAL_1027

    可以将若干种状态用整数标记一下,然后依次扫描一遍text的字符,如果在当前状态下满足某些条件就跳转到下一状态,如果跳入到了非法状态就可以直接break了,最后看扫描完text之后是否处于一个正常的状态即可。

#include<stdio.h>
#include<string.h>
#define MAXD 10010
int N;
char b[MAXD], *ch = "=+-*/0123456789";
void change(int &i, int &pat, int &pre)
{
    if(pat == 0)
    {
        if(b[i] == '(')
        {
            if(b[i + 1] == '*')
                ++ i, pre = 0, pat = -1;
            else
                pat = 1;
        }
        else if(b[i] == ')')
            pat = -2;
    }
    else if(pat > 0)
    {
        if(b[i] == '(')
        {
            if(b[i + 1] == '*')
                ++ i, pre = pat, pat = -1;
            else
                ++ pat;
        }
        else if(b[i] == ')')
            -- pat;
        else if(strchr(ch, b[i]) == NULL && b[i] != '\n')
            pat = -2;
    }
    else
    {
        if(b[i] == '*' && b[i + 1] == ')')
            ++ i, pat = pre;
    }
}
int check()
{
    int i, pre = 0, pat = 0;
    b[N] = '\0';
    for(i = 0; i < N; i ++)
    {
        change(i, pat, pre);
        if(pat == -2)
            return 0;
    }
    return pat == 0;
}
int main()
{
    N = 0;
    while(scanf("%c", &b[N]) == 1)
        ++ N;
    if(check())
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}
posted on 2012-05-02 21:10  Staginner  阅读(290)  评论(0编辑  收藏  举报