当前位置:编程学习 > C/C++ >>

高级列单词

描述
 
 
  现在给你一篇文章,请统计其中出现次数排名处于前n的单词,这里为了简单起见,所有非字母字符(包括空格、回车、标点、连字符"-"等等)都被认为是分隔单词的标志,换句话说就是在任意两个此类字符之间的连续字母都被认为是一个单词。
 
  与前一题同样的是:所有单词全部转换为小写形式进行统计和输出,每个单词的排名=出现次数更高的单词个数+1,所以出现次数一样的单词排名一样,并且可能输出不止n个单词。排列上排名相同的单词以字典序列出。
输入
 
 
第1行:一个整数n(1<n<=1000),代表输出的最大排名
第2至N行:N-1行文本,每行字符数不限,行内字符均为“可打印”字符,32<=ASCII码<=126),单词最大长度50,N-1行最大不同单词个数3000
本题不显示给出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 程序设计竞赛现场赛
 
在前面(简单列单词 :http://blog.csdn.net/zhangweiacm/article/details/12912029 )中稍微加几个区分单词的判断条件就行了
 
 
 
 
#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(~scanf("%d",&n))  
    {  
        int i=0,k=0,j=0,s=0;  
        getchar();  
        while((c=getchar())!=EOF)  
        {  
            if(c>='A' && c<='Z')  
                c+=32;  
            if(c>='a' && c<='z')  
                {  
                    ch[k][j]=c;  
                    j++;  
                }  
                if(j!=0)  
            if(c==' '||c=='\n'||c==','||c=='"'||c=='.'||c=='\''||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[1].ch,a[1].sum);  
      for(i=0;i<n;i++)  
     {  
        if(a[i].sum!=a[i-1].sum)  
         j=i+1;  
      if(a[i].ch!=NULL)printf("Rank  %d: %s (%d)\n",j,a[i].ch,a[i].sum);  
     }  
    }  
    return 0;  
}  

 

 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,