码迷,mamicode.com
首页 > Web开发 > 详细

Web Data Management(一)

时间:2015-05-12 20:49:58      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

  今天上了web数据管理,老师布置了一个简单的exercise,题目如下:

  Question : Consider the following documents:
    d1 = I like to watch the sun set with my friend.
    d2 = The Best Places To Watch The Sunset.
    d3 = My friend watches the sun come up.

  Task : write a program which can output the document ID given by an input word.

  解题思路(1):

    1. 使用Java的String类的constains()方法,这里不贴实现代码.

  解题思路(2):

    1. 将字符串分割为字符串数组

    2.判断字符串数组里是否包含给定key word.

public class Exercise1 {

	public static void main(String[] args) {

		// 1. 转换为数组
		StringBuilder d1 = new StringBuilder("I like to watch the sun set with my friend.");
		StringBuilder d2 = new StringBuilder("The Best Places To Watch The Sunset.");
		StringBuilder d3 = new StringBuilder("My friend watches the sun come up.");

		// 删除最后面的点,分割字符串
		String[] sd1 = d1.deleteCharAt(d1.length() - 1).toString().split(" ");
		String[] sd2 = d2.deleteCharAt(d2.length() - 1).toString().split(" ");
		String[] sd3 = d3.deleteCharAt(d3.length() - 1).toString().split(" ");

		// 2. 输入关键字
		Scanner scanner = new Scanner(System.in);
		System.out.print("Input key word :");
		String word = scanner.next();

		boolean isSd1 = search(sd1, word);
		boolean isSd2 = search(sd2, word);
		boolean isSd3 = search(sd3, word);
		// 3. 遍历寻找
		if (isSd1 || isSd2 || isSd3) {
			System.out.print("The key word " + word + " constains in ");
			if (isSd1) {
				System.out.print("d1 ");
			}
			if (isSd2) {
				System.out.print("d2 ");
			}
			if (isSd3) {
				System.out.print("d3 ");
			}
		} else {
			System.out.println("not found");
		}

	}

	private static boolean search(String[] str, String word) {
		boolean flag = false;
		for (String string : str) {
			// 不考虑大小写的相等
			if (string.equalsIgnoreCase(word)) {
				return true;
			}
		}
		return flag;
	}

}

   解题思路(3):

    1. 使用Java的正则表达式(这里是作者自己从网上找的reg代码)

import java.util.Scanner;
import java.util.regex.Pattern;

public class Exercise1 {

	public static void main(String[] args) {
		
		String d1 = "I like to watch the sun set with my friend.";
		String d2 = "The Best Places To Watch The Sunset.";
		String d3 = "My friend watches the sun come up.";
		
		// 1. 获取输入,可以是多个单词,空格分割
		Scanner scanner = new Scanner(System.in);
		System.out.print("Input key word :");
		StringBuffer word = new StringBuffer(scanner.nextLine());
		scanner.close();
		
		/**
		 * 构建正则表达式, 忽略大小写
		 * .*(keyword1|keyword2|...).*
		 */
		String words = word.toString().replace(‘ ‘, ‘|‘);
		StringBuilder reg = new StringBuilder(".*(").append(words).append(").*");
        Pattern pattern = Pattern.compile(reg.toString(), Pattern.CASE_INSENSITIVE);  
        
        boolean isSd1 = pattern.matcher(d1).matches();
		boolean isSd2 = pattern.matcher(d2).matches();
		boolean isSd3 = pattern.matcher(d3).matches();

		if (isSd1 || isSd2 || isSd3) {
			System.out.print("The key word " + word + " constains in ");
			if (isSd1) {
				System.out.print("d1 ");
			}
			if (isSd2) {
				System.out.print("d2 ");
			}
			if (isSd3) {
				System.out.print("d3 ");
			}
		} else {
			System.out.println("not found");
		}
	}
}

 

    

Web Data Management(一)

标签:

原文地址:http://www.cnblogs.com/LeeAnkang/p/4498299.html

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