zoukankan      html  css  js  c++  java
  • ECNU-2574 Principles of Compiler

    题意:

    给出编译规则,求是否满足条件

        A:= '(' B')'|'x'.
        B:=AC.
        C:={'+'A}.

    其中{}表示里面的内容可以出现0次或者多次

    注意点见代码注释

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 const int maxn = 205;
     5 
     6 int pos;
     7 int len;
     8 char a[ maxn ];
     9 
    10 bool solveA();
    11 bool solveB();
    12 bool solveC();
    13 
    14 bool solveA(){
    15     //printf("A:%c
    ",a[pos]);
    16     //if( pos>=len ) return false;
    17     if( a[pos]=='(' ){
    18         pos++;
    19         if( solveB() ){
    20             if( a[pos]==')' ){
    21                 pos++;/*该字符后面可能还有字符*/
    22                 return true;
    23             }
    24             else{
    25                 return false;
    26             }
    27         }
    28         else 
    29             return false;
    30     }
    31     else
    32         if( a[pos]=='x' ){
    33             pos++;
    34             return true;
    35         }
    36     else{
    37         pos++;/*同理,该字符后面可能还有字符*/
    38         return false;
    39     }
    40 }
    41 
    42 bool solveB(){
    43     //printf("B:%c
    ",a[pos]);
    44     if( solveA() ){
    45         return solveC();
    46     }
    47     else return false;
    48 }
    49 
    50 
    51 bool solveC(){
    52     if( pos>=len ) return false;
    53     while( pos<len && a[pos]=='+' ){
    54         pos++;
    55         solveA();
    56     }
    57     return true;
    58 }
    59 
    60 
    61 int main(){
    62     int T;
    63     scanf("%d",&T);
    64     while( T-- ){
    65         memset( a,'',sizeof( a ) );
    66         scanf("%s",a);
    67         pos = 0;
    68         len = strlen( a );
    69         bool f = solveA();
    70         if( pos<len ) {printf("Bad
    ");continue;}
    71         /*防止出现前半部分符合条件,后半部分不合的用例*/
    72         if( f ) printf("Good
    ");
    73         else printf("Bad
    ");
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    矿Mac必备软件
    PHP第六课 使用方法数组
    I/O概述和审查操作
    应用程序配置文件
    他毕业两年,博客一年,时间
    苹果公司的回复
    CImage类的介绍与使用
    数据库移植遇到的问题
    MP算法和OMP算法及其思想
    ROR 环境的 搭建
  • 原文地址:https://www.cnblogs.com/xxx0624/p/3967442.html
Copyright © 2011-2022 走看看