Simply Syntax
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5551   Accepted: 2481

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

题解:

0,p~z每个字母都是合法的句子

1, 如s是一个合法的句子,那么Ns也是一个合法的句子

2,如果s与t都是合法的一个句子,那么Cst, Dst, Est, and Ist.

3,只有满足以上的才是一个合法的句子

想的太多了,其实就按照所说的递归就好:

代码:

package com.hbc.week3;

import java.util.Scanner;

public class SimplySyntax {
private static Scanner cin = null;
static{
cin = new Scanner(System.in);
}
static boolean judge(String s){
char c = s.charAt(0);
if(s.length() == 1){
if(c >= 'p' && c <= 'z')
return true;
else
return false;
} if(c == 'N'){
if(judge(s.substring(1))){
return true;
}
return false;
}
if(c == 'C'
|| c == 'D'
|| c == 'E'
|| c == 'I'){
for(int i = 2; i < s.length(); i++){
if(judge(s.substring(1, i)) && judge(s.substring(i, s.length()))){
return true;
}
}
return false;
}
return false;
}
public static void main(String[] args) {
//System.out.println(isSimpleSentence("pqp"));
while(cin.hasNext()){
String s = cin.next();
if(judge(s)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}

Simply Syntax(思维)的更多相关文章

  1. POJ 1126:Simply Syntax

    Simply Syntax Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5264   Accepted: 2337 Des ...

  2. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  3. 【Static Program Analysis - Chapter 2】 代码的表征之抽象语法树

    抽象语法树:AbstractSyntaxTrees 定义(wiki): 在计算机科学中,抽象语法树(abstract syntax tree或者缩写为AST),或者语法树(syntax tree),是 ...

  4. Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)

    1, http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c 2, Introduction ...

  5. Nginx - Configuration File Syntax

    Configuration Directives The Nginx configuration file can be described as a list of directives organ ...

  6. 13.1.17 CREATE TABLE Syntax

    13.1.17 CREATE TABLE Syntax 13.1.17.1 CREATE TABLE ... LIKE Syntax 13.1.17.2 CREATE TABLE ... SELECT ...

  7. ES6 new syntax of Arrow Function

    Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...

  8. Active Directory: LDAP Syntax Filters

    LDAP syntax filters can be used in many situations to query Active Directory. They can be used in VB ...

  9. vue源码逐行注释分析+40多m的vue源码程序流程图思维导图 (diff部分待后续更新)

    vue源码业余时间差不多看了一年,以前在网上找帖子,发现很多帖子很零散,都是一部分一部分说,断章的很多,所以自己下定决定一行行看,经过自己坚持与努力,现在基本看完了,差ddf那部分,因为考虑到自己要换 ...

随机推荐

  1. Selenium (4) —— Selenium是什么? WebDriver是什么?做什么?(101 Tutorial)

    Selenium (4) -- Selenium是什么? WebDriver是什么?做什么?(101 Tutorial) selenium版本: v2.48.0 (Standalone Seleniu ...

  2. 十大要避免的Ext JS开发方法

    原文地址:http://www.sencha.com/blog/top-10-ext-js-development-practices-to-avoid/ 作者:Sean LanktreeSean i ...

  3. Shell数组遍历

    #!/bin/bash emp_info=( aaa aaa@qq.com bbb bbb@qq.com ccc ccc@qq.com ddd ddd@qq.com eee eee@qq.com ) ...

  4. QueenPuzzle-N皇后问题

    详见-算法之美-p180. #include <iostream> #include <memory.h> #include <conio.h> #include ...

  5. 使用javascript实现浏览器全屏

    HTML 5中的full screen,目前可以在除IE和opera外的浏览器中使用 ,有的时候用来做 全屏API,游戏呀,等都很有用.先看常见的API 1 element.requestFullSc ...

  6. 自然语言交流系统 phxnet团队 创新实训 项目博客 (九)

    项目技术总结: VoiceToText的具体使用方法: 语音转文本部分是调用的科大讯飞的在线语音,它的激发方式是按键,通过按钮触发开启安卓设备的录音,此部分需要在源码中写入关于安卓权限的要求,来调用安 ...

  7. java 正则表达式 验证字符串 只包含汉字英文数字

    String content = “testContent”; String regex="^[a-zA-Z0-9\u4E00-\u9FA5]+$"; Pattern patter ...

  8. 关于Unity中的3D拾取

    3D拾取 3D游戏实际上看到的是2D画面,我们在屏幕上点击,想要找到哪个3D物体,我们实际上是在一个2维平面内做3D拾取. 3D拾取实际上是,当玩家点击屏幕的时候,会从显示屏幕的摄像头发射一条射线,射 ...

  9. 【转】【Python】Python网络编程

    Socket简介 在网络上的两个程序通过一个双向的通信连接实现数据的交换,这个链接的一端称为一个Socket(套接字),用于描述IP地址和端口. 建立网络通信连接至少要一对端口号(Socket),So ...

  10. HTML5实现摇一摇的功能(实测后)--转

    eviceMotionEvent(设备运动事件)返回设备有关于加速度和旋转的相关信息.加速度的数据将包含三个轴:x,y和z(示意如下图所 示,x轴横向贯穿手机屏幕或者笔记本键盘,y轴纵向贯穿手机屏幕或 ...