POJ 1119 Start Up the Startup

 

Start Up the Startup
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 1366   Accepted: 346

Description

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

4.00
2.41

Source

Mid-Atlantic 2001

 

/* 用STL的MAP来做哈希还是挺不错的,不过不是所有时候都适用的 */ #include <iostream> #include <map> #include <string> #include <algorithm> #include <cmath> using namespace std; map<string, int> text; map<string, int> text1; string temp; bool allPunc(string &str) { char c; string temp = ""; bool allP = true; for(int i = 0; i < str.length(); i++) { c = str[i]; if(c >= 'A' && c <= 'Z') { allP = false; temp = temp + char(c - 'A' + 'a'); } else if(c >= 'a' && c <= 'z') { allP = false; temp = temp + c; } else if(c >= '0' && c <= '9') { allP = false; temp = temp + c; } } str = temp; return allP; } void checkInsert(const string & str) { map<string, int>::iterator it; it = text1.find(str); if(it != text1.end()) { it->second++; } } void init() { map<string, int>::iterator it; for(it = text1.begin(); it != text1.end(); it++) it->second = 0; } double getRes() { double total = 0; map<string, int>::iterator it; map<string, int>::iterator it1; for(it = text.begin(); it != text.end(); it++) { it1 = text1.find(it->first); total += sqrt(double(it->second) * double(it1->second)); } return total; } int main() { text.clear(); while(cin>>temp && temp != "----------") { if(!allPunc(temp)) { map<string, int>::iterator it; it = text.find(temp); if(it == text.end()) text[temp] = 1; else text[temp]++; } } while(cin>>temp && temp != "----------") { text1 = text; init(); if(!allPunc(temp)) checkInsert(temp); while(cin>>temp && temp != "----------") { if(!allPunc(temp)) checkInsert(temp); } printf("%.2f/n", getRes()); } return 0; } 

你可能感兴趣的:(POJ 1119 Start Up the Startup)