首页 > 专题系列 > Java解POJ > POJ 1677 Girls’ Day [解题报告] Java
2013
11-10

POJ 1677 Girls’ Day [解题报告] Java

Girls’ Day

问题描述 :

On Girls’ Day (Haven’t heard about it? Well, you may ask the author for details…), we boys get together with girls in the class. On the occasion, every boy will make a wish for girls. Boys want to know about girls’ responses to their wishes.

If a wish contains one or more girls’ names, it is considered to be talking to them specifically. Otherwise it is talking to all the girls. A wish can simultaneously talk to several girls of course.

A girl would say ‘oh’ if the wish contains at most 9 words.

A girl would say ‘xixi’ if the wish contains at least 10 words and she hears the word ‘beautiful’, ‘pretty’ or ‘lovely’.

A girl would say ‘hehe’ if the wish contains at least 10 words and she doesn’t hear such words mentioned above.

It is confirmed that a wish will not contain all the girls? name.

输入:

The first line of the input contains two integers g and w (1 <= g <= 5, 1 <= w <= 30), the number of girls and the number of wishes respectively. The next g lines each contain a word in lowercase, representing the name of a girl. Then the next w lines each contain a string of letters and punctuations, namely the wish, which will contain at most 200 characters.

Each wish contains one or more sentences, and each sentence is terminated by a ‘!’. The first letter of each sentence is in uppercase, and other letters will be always in lowercase. No other characters apart from ‘!’, spaces and letters will appear in the sentence. You may assume that each wish is correct in grammar.

输出:

You are required to give girls’ response according to the input. For each wish, if it is talking to all the girls, print ‘All’, or otherwise print the list of girls that it is talking to (names are separated by a single space) in the order that they appear in the name list. Then print a semicolon followed by a space, and the response such as ‘hehe’ and ‘xixi’.

样例输入:

5 5
answer
baiqingr
cedar
juleo
seven
Happy girls day to all of you!
Happy girls day to all of you!Wish you happy forever!
Happy girls day for answer mm!
Congratulations for cedar mm and seven mm!Wish you more and more beautiful hehe! 
Hello answer hello baiqingr hello juleo would you mind having dinner together!

样例输出:

All: oh
All: hehe
answer: oh
cedar seven: xixi
answer baiqingr juleo: hehe

解题代码:

//* @author 
/**
 * pku 1677 Girls' Day
 * Memory: 3524K  Time: 172MS 
 * Language: Java  Result: Accepted 
 * @Author	conanhjj
 */
import java.util.*;

public class Main {

 static Scanner in = new Scanner(System.in);

 static int g,w;
 static String[] girl;
 public static void main(String[] args){
	input();
	work();
 }

 static void work() {
  String line;
  for(int i=0;i< w;++i){
	do{
         line = in .nextLine();
       }while(line.length()==0);

	LinkedList words = new LinkedList(Arrays.asList(line.split("[\\s|!]+")));
	if(words.size()>0 && words.peek().equals(""))
		words.removeFirst();
		//	System.out.println(words);

	boolean flag = false;
	boolean xixi = false;
	boolean[] mark = new boolean[g];
	for(int k=0;k< mark.length;++k) mark[k] =false;
	for(String word : words){
		word = word.toLowerCase();
		if(word.equals("beautiful") || word.equals("pretty") ||
                word.equals("lovely")){
			xixi = true;
		  }

		for(int k=0;k< g;++k) {
			if(word.equals(girl[k]) && !mark[k]){
				mark[k] = true;
				flag = true;
			}
		}
	}

	if(!flag){
		System.out.print("All");
	} else {
		boolean first = true;
		for(int k=0;k< g;++k) {
			if(mark[k]){
				if(first){
					System.out.print(girl[k]);
					first = false;
				} else {
					System.out.print(" " + girl[k]);
				}
			}
		}
	}

	System.out.print(": ");
	if(words.size() <= 9 ){
		System.out.println("oh");
	} else {
		if(xixi){
			System.out.println("xixi");
		} else {
			System.out.println("hehe");
		}
	}

  }
 }

 static void input(){
     g = in.nextInt();
	w = in.nextInt();

	girl = new String[g];
	for(int i=0;i< g;++i){
		girl[i] = in.next();
		//System.out.println(girl[i]);
	}
   }
}

  1. 博主您好,这是一个内容十分优秀的博客,而且界面也非常漂亮。但是为什么博客的响应速度这么慢,虽然博客的主机在国外,但是我开启VPN还是经常响应很久,再者打开某些页面经常会出现数据库连接出错的提示