acm pku 1126 Simple Syntax的具体实现

本文介绍了一种用于检查Hedonian语言句子语法正确性的程序。该语言仅包含特定字符,并遵循一套简单的语法规则。文章提供了一个使用动态规划算法的具体实现案例,能够有效地判断输入句子是否符合语法规范。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Simply Syntax

Description

In the land of Hedonia the official language is Hedonian. A Hedonian professor had noticed that many of her students still did not master the syntax of Hedonian well. Tired of correcting the many syntactical mistakes, she decided to challenge the students and asked them to write a program that could check the syntactical correctness of any sentence they wrote. Similar to the nature of Hedonians, the syntax of Hedonian is also pleasantly simple. Here are the rules:


0.The only characters in the language are the characters p through z and N, C, D, E, and I.

1.Every character from p through z is a correct sentence.

2.If s is a correct sentence, then so is Ns.

3.If s and t are correct sentences, then so are Cst, Dst, Est and Ist.

4.Rules 0. to 3. are the only rules to determine the syntactical correctness of a sentence.

You are asked to write a program that checks if sentences satisfy the syntax rules given in Rule 0. - Rule 4.

Input

The input consists of a number of sentences consisting only of characters p through z and N, C, D, E, and I. Each sentence is ended by a new-line character. The collection of sentences is terminated by the end-of-file character. If necessary, you may assume that each sentence has at most 256 characters and at least 1 character.

Output

The output consists of the answers YES for each well-formed sentence and NO for each not-well-formed sentence. The answers are given in the same order as the sentences. Each answer is followed by a new-line character, and the list of answers is followed by an end-of-file character.

Sample Input

Cp
Isz
NIsz
Cqpq

Sample Output

NO
YES
YES
NO

Source

East Central North America 1994

 

       这道题目可以通过判断“0rule~4rule”来得到解决。假设用bResult[i][j]表示自第i个元素起到其后面的j个元素所组成的词是否是合法的(0i<输入单词长度,0<j<输入字符长度 ,若是则bResult[i][j]=true;否则,bResult[i][j]=false。这样,bResult[0][输入字符长度] 就是要求的结果,如果为真则输出YES,否则输出NO。关于bResult[i][j],可以按如下公式进行计算: 

bResult[i][j] =

True    if (words[i]=p~z, i=0, j = 1;)

True    if (words[i]=N, bResult[i+1][j-1]=true; j>1;)

True    if (words[i]=C,D,E,I, bResult[i+1][j-1]=true; j>1;

     存在bResult[i+1][k]=bResult[i+k][j-1-k]=true, 0<k<j)

false   otherwise

具体实现:

 

//20100530

 

#include "iostream"

using namespace std;

 

const int N = 256;

char cinput[N];

bool bResult[N][N];

 

void Judge(int begin, int num)

{

       int i, j, k;

      

       if(num == 1)

       {

              if(cinput[begin] >= 'p' && cinput[begin] <= 'z') bResult[begin][1] = true;

              return;

       }

 

       for(i = begin; i < begin+num; i++)

       {

              if(cinput[i] >= 'p' && cinput[i] <= 'z') bResult[i][1] = true;

       }

       for(i = begin+num-1 -1; i > -1; i--)

       {

              for(j = 2; j <= begin+num-i; j++)

              {

              //     if(cinput[i] >= 'p' && cinput[i] <= 'z' && bResult[i+1][j-1]) bResult[i][j] = true;

                     if(cinput[i] == 'N' && bResult[i+1][j-1]) bResult[i][j] = true;

                     else if(cinput[i] == 'C' || cinput[i] == 'D' || cinput[i] == 'E' || cinput[i] == 'I')

                     {

                            for(k = 1; k < j; k++)

                            {

                                   if(bResult[i+1][k] && bResult[i+k+1][j-1-k]) {bResult[i][j] = true;break;}

                            }

                     }

              }

       }

}

 

int main(void)

{

       int i, len;

      

       while(cin >> cinput)

       {           

              len = strlen(cinput);

              for(i = 0; i <= len; i++) memset(bResult[i], false, sizeof(bool)*N);

 

              Judge(0, len);

              if(bResult[0][len]) cout << "YES" << endl;

              else cout << "NO" << endl;

       }

       return 0;

}

执行结果:

Problem: 1126

 

User: uestcshe

Memory: 164K

 

Time: 0MS

Language: C++

 

Result: Accepted

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值