简单列单词

描述

  如果你计划读一本英语小说,可能你需要做一些提前准备,例如列出书中“常见的生词”。也许在以前,这是一件不可能的事。因为你既然没读过这本书,又怎么知道哪些是常见的词呢。但是随着计算机和电子书的普及,这边为了可能,在用计算机统计完一本书后,你可能发现这样的结果:排在前几位的都是一些非常简单的词语——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 程序设计竞赛现场赛

 

这题输入和输出有点困惑,之后看了学长的代码才懂得,花了我一个半小时才AC掉,值得我收获的是对排序函数(sort)又有了新的认识及用法

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct stu
{
   char ch[50];
   int sum;
}a[10000];
int n;
int cmp(stu a,stu b)
{
    if(a.sum>b.sum)
    return 1;
    else if(a.sum==b.sum)
        {
        if(strcmp(a.ch,b.ch)<0)
        return 1;
        else
        return 0;
        }
     else
     return 0;
}
int main()
{
    char c,ch[10000][50],b[50]="++";
    while(cin>>n)
    {
        int i=0,k=0,j=0,s=0;
        getchar();
        while((c=getchar())!='\n')
        {
            if(c>='A' && c<='Z')
                c+=32;
            if(c>='a' && c<='z')
                {
                    ch[k][j]=c;
                    j++;
                }
            if(c==' ')
                {
                    k++;
                    j=0;
                }

        }
        while(i<=k)
        {
            if(strcmp(ch[s],b)!=0)
            {
                a[i].sum=1;
                strcpy(a[i].ch,ch[s]);
                for(j=s+1;j<=k;j++)
                {
                    if(strcmp(ch[j],a[i].ch)==0)
                    {
                        strcpy(ch[j],b);
                        a[i].sum++;
                    }
                }
                i++;
            }
            s++;
        }
       sort(a+0,a+k+1,cmp);j=1;
       printf("Rank  %d: %s (%d)\n",j,a[0].ch,a[0].sum);
      for(i=1;i<n;i++)
     {
        if(a[i].sum!=a[i-1].sum)
         j=i+1;
      printf("Rank  %d: %s (%d)\n",j,a[i].ch,a[i].sum);
     }

    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值