1260 文法語言

來源:互聯網
上載者:User
 
描述

一個文法G[Z]有如下五條規則:
(1)Z→(U)
(2)Z→aUb
(3)U→dZ
(4)U→bU
(5)U→e
由此文法可以產生語言:比如:ad(be)b就是此文法的語言,因為有如下推導序列:
Z→aUb→adZb→ad(U)b→ad(bU)b→ad(be)b

輸入

有n+1行,第一行為測試資料的組數n。 下面有n行,每行為一個字串。

輸出

對每一組測試資料有一行輸出,如果語言Σ是G[Z]產生的語言,則輸出"Yes."如果不是則 輸出"No".

範例輸入
2abcsdf324sdad(be)b
範例輸出
No.Yes.
提示

運用遞迴。

 

按照提示遞迴實現即可

#include <stdio.h>#include <string.h>char s[10001];int i;int flag;int flag1;void U();void Z();void Z(){    if(s[i]=='(')    {         i++;        U();        if(s[i]==')')        {            i++;        }        else            flag=1;    }    else if(s[i]=='a')    {        i++;        U();        if(s[i]=='b')            i++;        else            flag=1;    }    else        flag=1;}void U(){    if(s[i]=='d')    {        i++;        Z();    }    else if(s[i]=='b'&&s[i+1]!='\0')    {        i++;        U();    }    else if(s[i]=='e')    {         i++;    }    else        flag=1;}int main(){    int t,number,j;    int length;    scanf("%d",&number);    for(t=1;t<=number;t++)    {        i=0;        flag=0;        scanf("%s",&s);        length=strlen(s);        for ( j=0;j<strlen(s);j++)        {            if (s[j]!='e'&&s[j]!='a'&&s[j]!='b'&&s[j]!='d'&&s[j]!='('&&s[j]!=')')            {                flag=1;                break;            }         }        if(flag==0)            Z();        if(flag==0)            printf("Yes.\n");        else            printf("No.\n");    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.