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
 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: User: Memory: 164K Time: 0MS Language: C++ Result: Accepted

ACM PKU 题目分类(完整整理版本)

2016-03-06 17:35:40

POJ 1126 Simple Syntax

2009-09-19 16:09:00

2016.5.8 PKU校赛总结

2016-05-09 13:58:10

POJ：1126 Simply Syntax（思维）

2017-03-23 20:31:21

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

2008年03月23日 539KB 下载

ACM Poj Pku 解题报告答案 打包 下载 600多题 史上最全

2010年07月26日 4.23MB 下载

PKU 1000 A+B 详细脑残题解~~~

2010-05-18 08:52:00

pku 123 题目代码

2008年09月11日 57KB 下载

PKU OJ 类

2014-12-24 13:13:32

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

2008年03月17日 314KB 下载