首页 > 专题系列 > Java解POJ > POJ 3366 Deli Deli [解题报告] Java
2013
11-12

POJ 3366 Deli Deli [解题报告] Java

Deli Deli

问题描述 :

Mrs. Deli is running the delicatessen store “Deli Deli”. Last year Mrs. Deli has decided to expand her business and build up an online store. She has hired a programmer who has implemented the online store.

Recently some of her new online customers complained about the electronic bills. The programmer had forgotten to use the plural form in case that an item is purchased multiple times. Unfortunaly the programmer of Mrs. Deli is on holiday and now it is your task to implement this feature for Mrs. Deli. Here is a description how to make the plural form:

  1. If the word is in the list of irregular words replace it with the given plural.
  2. Else if the word ends in a consonant followed by “y”, replace “y” with “ies”.
  3. Else if the word ends in “o”, “s”, “ch”, “sh” or “x”, append “es” to the word.
  4. Else append “s” to the word.

输入:

The first line of the input file consists of two integers L and N (0 ≤ L ≤ 20, 1 ≤ N ≤ 100). The following L lines contain the description of the irregular words and their plural form. Each line consists of two words separated by a space character, where the first word is the singular, the second word the plural form of some irregular word. After the list of irregular words, the following N lines contain one word each, which you have to make plural. You may assume that each word consists of at most 20 lowercase letters from the English alphabet (‘a’ to ‘z’).

输出:

Print N lines of output, where the ith line is the plural form of the ith input word.

样例输入:

3 7
rice rice
spaghetti spaghetti
octopus octopi
rice
lobster
spaghetti
strawberry
octopus
peach
turkey

样例输出:

rice
lobsters
spaghetti
strawberries
octopi
peaches
turkeys

解题代码:

//* @author: [email protected]
import java.util.Scanner;
public class Main
{
 public static void main(String[] args)
 {
	Scanner in=new Scanner(System.in);
	int a=in.nextInt();
	int b=in.nextInt();
	String[] s1=new String[a];
	String[] s2=new String[a];
	for(int i=0;i< a;i++)
	{
		s1[i]=in.next();
		s2[i]=in.next();
	}
	for(int i=0;i< b;i++)
	{
         String s=in.next();
	  boolean bb=false;
	  for(int j=0;j< a;j++)
	  {
	   if(s.equals(s1[j]))
	   {
		System.out.println(s2[j]);
		bb=true;
		break;
	    }
	   }
	  if(!bb){
		if(s.endsWith("y"))
		{
	        if(s.endsWith("ay")||s.endsWith("ey")||s.endsWith("iy")||s.endsWith("oy")||s.endsWith("uy"))
			System.out.println(s+"s");
		 else
			System.out.println(s.substring(0,s.length()-1)+"ies");
		}
		else if(s.endsWith("o")||s.endsWith("s")||s.endsWith("ch")||s.endsWith("sh")
				||s.endsWith("x")) System.out.println(s+"es");
		else System.out.println(s+"s");
		}
	  }
	}
}

  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。