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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章