倒排文档



所述输出字的第一行,其中的行数(多个单词,根据该输出从小到大的排序,中间空格相距,编号从一开始就注意事项)。假设有,出口 -1
输出频率排名的第二行R出现的次数的字。


测试数据的频度分布,例如,下面的。看得见,级别3这个单词,数为2
I,4
Beijing,2
in,2
love,2
.,1
Bejing,1
a,1
also,1
am,1
and,1
beautiful,1
is,1
life,1
live,1
student,1
there,1

travelling,1


这题非常easy,參考答案是这种:

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;

const int NN=100; //单词总数
const int MM=100000; //文本单行最大字符数

/* 文本单行 */
char ss[MM];
/* 统计频次 */
map<string,int> Mmap;
/* 用于最后的频次排序 */
struct node
{
    int x;
    string s;
}a[NN];

/* 用于最后的频次排序的排序规则 */
bool cmp(node xx,node yy)
{
    if(xx.x==yy.x)
        return xx.s<yy.s;
    return xx.x>yy.x;
}

bool fun(char *p,string temp)
{
    bool fg=false;  //此行中。是否有temp
    string t="";
    for(int i=0;*(p+i);i++)
    {
        if(*(p+i)==' ')
        {
            if(t.size()>0)
            {
                if(Mmap.count(t)==0)
                {
                    Mmap[t]=1;
                }
                else
                    Mmap[t]++;
                if(t==temp)
                    fg=true;
            }
            t="";
        }
        else
            t+=*(p+i);
    }

    if(t.size()>0)
    {
        if(Mmap.count(t)==0)
        {
            Mmap[t]=1;
        }
        else
            Mmap[t]++;
        if(t==temp)
            fg=true;
    }

    return fg;
}

int main()
{
    string temp;
    int n,R,tol=0;

    queue<int>ans; //存放查询单词出现的行数

    cin>>temp>>n>>R;
    getchar();

    for(int i=0;i<n;i++)
    {
        gets(ss);

       // cout<<"ss=="<<ss<<endl;

        if( fun(ss,temp) )
            ans.push(i+1);
    }

    map<string,int>::iterator itt = Mmap.begin();
    while(itt!=Mmap.end())
    {
        a[tol].s=(*itt).first;
        a[tol].x=(*itt).second;

      //  cout<<(*itt).first<<" "<<(*itt).second<<endl;

        tol++;
        itt++;
    }

    sort(a,a+tol,cmp);

    if(ans.size()>0)
    {
        int x=ans.front();
        ans.pop();
        printf("%d",x);
        while(!ans.empty())
        {
            x=ans.front();
            ans.pop();
            printf(" %d",x);
        }
        puts("");
    }
    else
        puts("-1");

    cout<<a[R-1].x<<endl;

    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-10-16 20:25  phlsheji  阅读(813)  评论(0编辑  收藏  举报