码迷,mamicode.com
首页 > 其他好文 > 详细

2792. Grammar Lessons

时间:2018-11-18 14:58:05      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:art   char   inter   spl   over   boolean   rar   目的   support   

2792. Grammar Lessons

  Petya got interested in grammar on his third year in school. He invented his own language called Petya‘s. Petya wanted to create a maximally simple language that would be enough to chat with friends, that‘s why all the language‘s grammar can be described with the following set of rules:

  • There are three parts of speech: the adjective, the noun, the verb. Each word in his language is an adjective, noun or verb.
  • There are two genders: masculine and feminine. Each word in his language has gender either masculine or feminine.
  • Masculine adjectives end with -lios, and feminine adjectives end with -liala.
  • Masculine nouns end with -etr, and feminime nouns end with -etra.
  • Masculine verbs end with -initis, and feminime verbs end with -inites.
  • Thus, each word in the Petya‘s language has one of the six endings, given above. There are no other endings in Petya‘s language.
  • It is accepted that the whole word consists of an ending. That is, words "lios", "liala", "etr" and so on belong to the Petya‘s language.
  • There aren‘t any punctuation marks, grammatical tenses, singular/plural forms or other language complications.
  • A sentence is either exactly one valid language word or exactly one statement.

  Statement is any sequence of the Petya‘s language, that satisfy both conditions:

  • Words in statement follow in the following order (from the left to the right): zero or more adjectives followed by exactly one noun followed by zero or more verbs.
  • All words in the statement should have the same gender.

  After Petya‘s friend Vasya wrote instant messenger (an instant messaging program) that supported the Petya‘s language, Petya wanted to add spelling and grammar checking to the program. As Vasya was in the country and Petya didn‘t feel like waiting, he asked you to help him with this problem. Your task is to define by a given sequence of words, whether it is true that the given text represents exactly one sentence in Petya‘s language.

Input

  The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105.

It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that given words do not belong to the Petya‘s language.

Output

  If some word of the given text does not belong to the Petya‘s language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes).

Examples
Input
  petr
Output
  YES
Input
  etis atis animatis etis atis amatis
Output
  NO
Input
  nataliala kataliala vetra feinites
Output
  YES

说明:前面提交了几次都没有完全通过,所以我在想是不是我漏掉了什么条件,或者是不是我考虑的还不够全面。后面静下来仔细想了一想,原来是我考虑的不周到。题目的意思很明确,合法声明定义为,含有零个以上的形容词+一个名词+零个以上的动词(按形容词名词动词的顺序且所有的词要么全是男性要么全是女性),这样我的判断条件只需要是,首先判断这个句子是男性还是女性说的(后部分还要检查整个句子是不是全是同一个性别的人说的),然后检查句子形容词名词动词的个数,其中名词不能大于1,名词要比动词先出现。

import java.util.Scanner;

public class Test2792 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] strArr = str.split(" ");
        int[] arr = new int[1024];
        boolean gender = true;

        // 判断性别
        if (strArr[0].endsWith("lios") || strArr[0].endsWith("etr")
                || strArr[0].endsWith("initis")) {
            gender = true;
        } else if ((strArr[0].endsWith("liala") || strArr[0].endsWith("etra") || strArr[0]
                .endsWith("inites"))) {
            gender = false;
        } else {
            System.out.println("NO");
            return;
        }

        for (int i = 0; i < strArr.length; i++) {
            if (gender) {
                if (strArr[i].endsWith("lios")) {
                    arr[0] += 1;
                } else if (strArr[i].endsWith("etr")) {
                    arr[1] += 1;
                } else if (strArr[i].endsWith("initis")) {
                    arr[2] += 1;
                } else {
                    System.out.println("NO");
                    return;
                }
            } else {
                if (strArr[i].endsWith("liala")) {
                    arr[0] += 1;
                } else if (strArr[i].endsWith("etra")) {
                    arr[1] += 1;
                } else if (strArr[i].endsWith("inites")) {
                    arr[2] += 1;
                } else {
                    System.out.println("NO");
                    return;
                }
            }
            if ((arr[1] == 0 && arr[2] > 0) || arr[1] > 1) {
                System.out.println("NO");
                return;
            }
        }
        if (arr[1] == 1) {
            System.out.println("YES");
        }
    }
}

 

2792. Grammar Lessons

标签:art   char   inter   spl   over   boolean   rar   目的   support   

原文地址:https://www.cnblogs.com/tangxlblog/p/9977749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!