acm pku 1126 Simple Syntax的具体实现

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

 

阅读更多
个人分类: acm
想对作者说点什么? 我来说一句

poj经典动态规划题目解题报告

2008年03月23日 539KB 下载

pku 123 题目代码

2008年09月11日 57KB 下载

poj经典数据结构题目解题报告

2008年03月17日 314KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭