简单列单词

来源:互联网 发布:光驱推荐知乎 编辑:程序博客网 时间:2024/06/14 07:09

简单列单词

时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte
总提交:146            测试通过:85

描述

  如果你计划读一本英语小说,可能你需要做一些提前准备,例如列出书中“常见的生词”。也许在以前,这是一件不可能的事。因为你既然没读过这本书,又怎么知道哪些是常见的词呢。但是随着计算机和电子书的普及,这边为了可能,在用计算机统计完一本书后,你可能发现这样的结果:排在前几位的都是一些非常简单的词语——the(3626次)、and(1922次)、to(1860次)、he(1748次)、a(1691次)。而在你翻过一两百个单词后就会开始出现一些你想要的东西。

  现在从文章中提取单词的工作已经做好了,你需要做的就是统计一下排名。每个单词的排名=出现次数更高的单词个数+1,所有单词全部转换为小写形式进行统计和输出,所有出现次数一样的单词排名一样,并且可能输出不止n个单词。排列上排名相同的单词以字典序列出。

输入

第1行:一个整数n(1<n<=10000),代表输出的最大排名
第2行:N个单词,每个单词后跟一个空格,单词仅由字母组成,最大长度50,最大不同单词个数10000
本题不显示给出N,以EOF判断输入结束

输出

第M行:Rank r: word (count),r为单词排名,如果为个位数,左边多空一格,排名后接一个冒号一个空格,word为单词,单词后再加一个空格,count为出现次数,用小括号括起来。

样例输入

15
Albus Rose Hugo and Lily laughed The train began to move and Harry walked alongside it watching his son s thin face already ablaze with excitement Harry kept smiling and waving even though it was like a little bereavement watching his son glide away from him The last trace of steam evaporated in the autumn air The train rounded a corner Harry s hand was still raised in farewell He ll be alright murmured Ginny As Harry looked at her he lowered his hand absentmindedly and touched the lightning scar on his forehead I know he will The scar had not pained Harry for nineteen years All was well

样例输出

Rank  1: the (6)
Rank  2: harry (5)
Rank  3: and (4)
Rank  3: his (4)
Rank  5: he (3)
Rank  5: was (3)
Rank  7: a (2)
Rank  7: hand (2)
Rank  7: in (2)
Rank  7: it (2)
Rank  7: s (2)
Rank  7: scar (2)
Rank  7: son (2)
Rank  7: train (2)
Rank  7: watching (2)

题目来源

安徽大学第三届ACM/ICPC 程序设计竞赛现场赛



我是用map加结构体做的。蛮简单的。


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>using namespace std;struct node{    string s1;    int num;};bool cmp(node n,node m){    if(n.num!=m.num)        return n.num>m.num;    else        return n.s1<m.s1;}int main(){    map<string,int>mapp;    int n,m,i,j;    int k,t;    struct node std[100000];    scanf("%d",&n);    string s2;    while(cin>>s2)    {        for(j=0; s2[j]; j++)        {            if(s2[j]>='A'&&s2[j]<='Z')            {                s2[j]+=32;            }        }        mapp[s2]++;    }    map<string,int>::iterator ii;    for(ii=mapp.begin(),i=0; ii!=mapp.end(); ii++,i++)    {        std[i].s1=ii->first;        std[i].num=ii->second;    }    sort(std,std+i,cmp);    t=0;    int p=0;    for(i=0,k=0; i<n; i++)    {        if(t!=std[i].num)        {            k++;            printf("Rank  %d: ",k+p);            k+=p;            p=0;        }        else        {            printf("Rank  %d: ",k);            p++;        }        cout<<std[i].s1<<" ";        printf("(%d)\n",std[i].num);        t=std[i].num;    }}


原创粉丝点击