EOJ 2574 Principles of Compiler [2009年研究生机试]

在大神的指点下AC了,起初用了编译原理的下推自动机,没AC掉,看来是自己把题目想复杂了……哭



#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <map>
#include <cctype>

using namespace std;

char ex[110];
int index;
bool A();
bool B();
bool C();
bool A()
{
    if(ex[index]=='x')
    {
        index++;
        while(ex[index]==' ') index++;
        return true;
    }
    if(ex[index]=='(')
    {
        index++;
        while(ex[index]==' ') index++;
        if(B()&&ex[index]==')')
        {
            index++;
            while(ex[index]==' ') index++;
            return true;
        }
    }
    return false;
}
bool B()
{
    return A()&&C();
}
bool C()
{
    while(ex[index]=='+')
    {
        index++;
        while(ex[index]==' ') index++;
        //return A();
        if (!A())
            return false;
    }
    return true;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int N;
    scanf("%d",&N);
    getchar();
    while(N--)
    {
        gets(ex);
        index=0;
        printf("%s\n",A()&&ex[index]=='\0'?"Good":"Bad");
    }
    return 0;
}

你可能感兴趣的:(EOJ 2574 Principles of Compiler [2009年研究生机试])