首页 > ACM题库 > HDU-杭电 > Hdu 1433 Simply Syntax[解题报告] C++
2013
12-10

Hdu 1433 Simply Syntax[解题报告] C++

Simply Syntax

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

Cp
Isz
NIsz
Cqpq

样例输出:

NO
YES
YES
NO


此题用递归的话会慢很多

#include <stdio.h>
#include <string.h>
int main ()
{    
      char arr[256];    
      while (scanf ("%s",arr) != EOF)    
      {          
            getchar();          
            int len = strlen (arr) ,count = 0 ,flog = 0;          
            for (int i = len - 1 ;i >= 0 ;i--)          
            {               
                  if (arr[i] >= 'p' && arr[i] <= 'z') count++;               
                  else if (arr[i] == 'C' || arr[i] == 'D' ||arr[i] == 'E' ||arr[i] =='I') count--;               
                  else if (arr[i] == 'N'){}               
                  else flog = 1;          
            }          
            if (!flog && count)printf("YES\n");          
            else printf("NO\n");    
      }    
      return 0;
}

 


  1. Gucci New Fall Arrivals

    This is really nice to know. I hope it will be successful in the future. Good job on this and keep up the good work.

  2. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  3. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])

  4. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。

  5. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。