ZOJ1050

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1050

自己敲代码,怎么改都不对,弄了两天,就是弄不出来……

题解的思路和我的大致相同,只不过用了容器map而已,我不服奋斗

map<Key, value>可以自动建立Key - value的对应。key 和 value可以是任意你需要的类型。 根据key值快速查找记录,查找的复杂度基本是Log(N)。

比如题解上的map<string, int>就是建立起了string与int之间的对应,可以理解为以string为下标的int型数组(并不是数组)。遍历要用到迭代器。

#include<iostream>
#include<cstdio>
#include<map>
#include<cmath>

using namespace std;

map<string,int> match;
map<string,int>::iterator mPos;//遍历map的迭代器
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 docEnd = false;
        while (cin>>a)
        {
            if (!docEnd && a == ten)
            {
                docEnd = true;
                double num = 0;
                for (mPos=match.begin(); mPos!=match.end(); mPos++)
                {
                    double m1 = mPos->second;
                    double m2 = doc[mPos->first];
                    num += sqrt(m1 * m2);
                }
                printf("%.2lf\n",num);
                doc.clear();
            }
            else if (docEnd && a == ten)
                break;
            else
            {
                docEnd = 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;
}

自己的代码(没有过):

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>

using namespace std;

string searchString[25000];
string document[25000];
int number[25000];

int main()
{
    int n,num = 0;

    cin>>n;
    //getchar();
    while (n--)
    {
        num++;
        getchar();
        if (num != 1)
            cout<<endl;
        int searchNum = 0;
        string buf;
        while (cin>>buf)
        {
            if (buf == "----------")
                break;
            string str = "";
            for (int i=0; i<buf.size(); i++)
                if (isdigit(buf[i]) || islower(buf[i]))
                    str += buf[i];
                else if (isupper(buf[i]))
                    str += tolower(buf[i]);
            bool f = true;
            for (int i=0; i<searchNum; i++)
                if (searchString[i] == str)
            {
                f = false;
                break;
            }
            if (!f)
                continue;
            searchString[searchNum++] = str;
        }
        memset(number,0,sizeof(number));
        for (int i=0; i<searchNum; i++)
        {
            for (int j=0; j<searchNum; j++)
                if ( searchString[i] == searchString[j] )
                    number[i]++;
        }

        bool docEnd = false;
        int documentNum = 0;
        while (cin>>buf)
        {
            if (!docEnd && buf == "----------")
            {
                docEnd = true;
                double ans = 0;
                for (int i=0; i<searchNum; i++)
                {
                    int inDocument = 0;
                    for (int  j=0; j<documentNum; j++)
                        if ( searchString[i] == document[j] )
                            inDocument++;
                    ans += sqrt(number[i] * inDocument);
                }
                printf("%.2lf\n",ans);
                documentNum = 0;
            }
            else if (docEnd && buf == "----------")
                break;
            else
            {
                docEnd = false;
                string str = "";
                for (int i=0; i<buf.size(); i++)
                if (isdigit(buf[i]) || islower(buf[i]))
                    str += buf[i];
                else if (isupper(buf[i]))
                    str += tolower(buf[i]);
                document[documentNum++] = str;
            }
        }
    }

    return 0;
}


你可能感兴趣的:(ZOJ1050)