Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5551 | Accepted: 2481 |
Description
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
Output
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"); } } } }