首页 > 专题系列 > Java解POJ > POJ 1566 Haiku Review [解题报告] Java
2013
11-09

POJ 1566 Haiku Review [解题报告] Java

Haiku Review

问题描述 :

Haiku is an ancient form of Japanese poetry. A haiku is a three-line poem with seventeen syllables, where the first line must contain five syllables, the second line must contain seven syllables, and the third line must contain five syllables. The lines do not have to rhyme. Here is an example, where slashes separate the lines:

Computer programs/The bugs try to eat my code/I must not let them.

You must write a program that will review a haiku and check that each line contains the correct number of syllables.

输入:

The input contains one or more lines, each of which contains a single haiku. A haiku will contain at least three words, and words will be separated by either a single space or a slash (`/’). Slashes also separate the three lines of a haiku, so each haiku will contain exactly two slashes. (The three lines of the haiku will be contained within one physical line of the file.) A haiku will contain only lowercase letters (`a’-`z’), forward slashes (`/’), and spaces, and will be no more than 200 characters long (not counting the end-of-line characters).

The haiku `e/o/i’ signals the end of the input.

Each haiku is guaranteed to contain three lines, and each line will contain at least one word. Your job is to determine whether each line has the correct number of syllables (5/7/5). For the purposes of this problem, every contiguous sequence of one or more vowels counts as one syllable, where the vowels are a, e, i, o, u, and y. Every word will contain at least one syllable. (Note that this method of counting syllables does not always agree with English conventions. In the second example below, your program must consider the word `code’ to have two syllables because the `o’ and the `e’ are not consecutive. However, in English the `e’ is silent and so `code’ actually has only one syllable.)

输出:

For each haiku, output a single line that contains `1′ if the first line has the wrong number of syllables, `2′ if the second line has the wrong number of syllables, `3′ if the third line has the wrong number of syllables, or `Y’ if all three lines have the correct number of syllables. If the haiku is not correct, you must output the number of the first line that has the wrong number of syllables.

样例输入:

happy purple frog/eating bugs in the marshes/get indigestion
computer programs/the bugs try to eat my code/i will not let them
a e i o u/this is seven syllables/a e i o u y
e/o/i

样例输出:

Y
2
3

解题代码:

//* @author: Yeming Hu"[email protected]"
import java.util.*;
import java.io.BufferedInputStream;


public class Main
{
    public static int[] numbers = {5,7,5};
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        sc.useDelimiter("\n");
        while(true)
        {
            String line = sc.next();
            line = line.trim();
            if(line.equals("e/o/i"))
            {
                break;
            }
            String[] parts = line.split("/");
            int i;
            for(i = 0; i < parts.length; i++)
            {
                String[] words = parts[i].split("\\s+");
                int numOfSyllables = 0;
                for(String word : words)
                {
                    numOfSyllables += getNumOfSyllables(word);
                }
                if(numOfSyllables != numbers[i])
                {
                    break;
                }
            }
            if( i == parts.length)
            {
                System.out.println("Y");
            }else
            {
                System.out.println(i+1);
            }
        }
    }
    
    public static int getNumOfSyllables(String word)
    {
        boolean pre = false;
        int result = 0;
        for(int i = 0; i < word.length(); i++)
        {
            if(isSyllable(word.charAt(i)))
            {
                if(!pre)
                {
                    result++;
                    pre = true;
                }
            }else
            {
                pre = false;
            }
        }
        return result;
    }
    
    public static boolean isSyllable(char ch)
    {
        return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
                || ch == 'u' || ch == 'y';
    }
}

  1. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。

  2. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。