ZOJ 1050 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.


Input Format:

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 Format:

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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input:

1

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


Sample Output:


4.00

2.41 

代码如下:

#include<iostream>
#include<iomanip>
#include<map>
#include<cmath>
using namespace std;
map<string,int>match;
map<string,int>::iterator m_pos;
map<string,int>doc;
string ten(10, '-');


int main()
{
    int n;
    bool line = false;
    cin >> n;
    while(n--)
    {
        if(line)
            cout << endl;
        line = true;
        match.clear();
        string a,b;
        while(cin >> a)
        {
            if(a == ten)
                break;
            b = "";
            for(int i=0; i<a.size(); i++)
            {
                if(isdigit(a[i])||islower(a[i]))
                    b += a[i];
                else if(isupper(a[i]))
                    b +=(tolower(a[i]));
            }
            if(b!="")
                match[b]++;
        }
        bool doc_end = false;
        while(cin >> a)
        {
            if(doc_end == false && a==ten)
            {
                doc_end = true;
                double num = 0.0;
                for(m_pos=match.begin();m_pos!=match.end(); m_pos++)
                {
                    double m1 = m_pos->second;
                    double m2 = doc[m_pos->first];
                    num += sqrt(m1*m2);
                }
                cout.setf(ios_base::fixed, ios_base::floatfield);
                cout << setprecision(2) << num << endl;


                doc.clear();
            }
            else if(doc_end == true && a == ten)
                break;
            else
            {
                doc_end = false;
                b = "";
                for(int i=0;i< a.size(); i++)
                {
                    if(isdigit(a[i])||islower(a[i]))
                        b += a[i];
                    else if(isupper(a[i]))
                        b += (tolower(a[i]));
                }
                if(match[b]>0)
                    doc[b]++;
            }




        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值