2013
11-09

Start Up the Startup

Clearly the economy is bound to pick up again soon. As a forward-thinking Internet entrepreneur, you think that the ‘Net will need a new search engine to serve all the people buying new computers. Because you’re frustrated with the poor results most search engines produce, your search engine will be better.

You’ve come up with what you believe is an innovative approach to document matching. By giving weight to the number of times a term appears in both the search string and in the document being checked, you believe you can produce a more accurate search result.

Your program will be given a search string, followed by a set of documents. You will calculate the score for each document and print it to output in the order the document appears in the input. To calculate the score for a document you must first calculate the term score for each term appearing in the search string. A term score is the number of times a term occurs in the search string multiplied by the number of times it occurs in the document. The document score is the sum of the square roots of each term score.

The input consists of a set of documents separated by single lines containing only ten dashes, “———” No line will be longer than 250 characters. No document will be longer than 100 lines. The first document is the search string. The input terminates with two lines of ten dashes in a row.

The input documents will use the full ASCII character set. You must parse each document into a set of terms.

Terms are separated by whitespace in the input document. Comparisons between terms are case-insensitive. Punctuation is removed from terms prior to comparisons, e.g. “don’t” becomes “dont” The resulting terms should contain only the characters {[a-z],[0-9]}. A term in the input consisting only of punctuation should be ignored. You may assume the search string and each document will have at least one valid term.

The output is a series of scores, one per line, printed to two decimal places. The scores are printed in the order the documents occur in the input. No other characters may appear in the output.

fee fi fo fum
----------
fee, fi, fo! fum!!
----------
fee fee fi, me me me
----------
---------- 

4.00
2.41

import java.io.BufferedReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

public class Main{

private static String convertString(String s) {

StringBuilder sb = new StringBuilder();

for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
sb.append(s.charAt(i));
} else if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
sb.append(s.charAt(i));
}
}

return sb.toString();
}

public static void main(String[] args) throws Exception {

String pre = "";

String line = "";

Map< String, Integer> document = new HashMap< String, Integer>();

while (true) {

pre = line;

if ("----------".equals(line)) {
break;
}

String[] s = line.split("[ ]+");

for (int i = 0; i < s.length; i++) {
String str = convertString(s[i]);

if (str.length() > 0) {
Integer number = document.get(str);

if (number == null) {
document.put(str, 1);
} else {
document.put(str, number + 1);
}
}
}

}

// System.out.println(document);

while (true) {

double sum = 0;

Map< String, Integer> search = new HashMap< String, Integer>();

while (true) {

pre = line;

if ("----------".equals(line)) {
break;
}

String[] temp = line.split("[, ]+");

for (int i = 0; i < temp.length; i++) {
String str = convertString(temp[i]);

if (str.length() > 0) {
Integer number = search.get(str);

if (number == null) {
search.put(str, 1);
} else {
search.put(str, number + 1);
}
}
}
}

for (Map.Entry< String, Integer> entry : search.entrySet()) {
if (document.keySet().contains(entry.getKey())) {

sum += Math.sqrt(entry.getValue() * document.get(entry.getKey()));
}
}

if ("----------".equals(line) && "----------".equals(pre)) {
break;
}

System.out.println(new BigDecimal(sum).setScale(2, RoundingMode.HALF_UP));

// double sum =0;

}

}

}

1. 为什么for循环找到的i一定是素数叻，而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak，而你每次取余都用的是原来的m，也就是n