谍报分析

时间限制: 1 Sec  内存限制: 128 MB

题目描述

“八一三”淞沪抗战爆发后,***几次准备去上海前线视察和指挥作战。但都因为宁沪之间的铁路和公路遭到了敌军的严密封锁,狂轰滥炸,一直未能成行。 
***特科组织,其主要任务是保卫***的安全,了解和掌握敌方的动向。经过一段时间的监听,谍报组获取了敌方若干份密报,经过分析,发现密文中频繁出现一些单词,情报人员试图从单词出现的次数中,推出敌军的行动计划。
请你编程,快速统计出频率高的前十个单词。

输入

密文是由英语单词(小写字母)组成,有若干段。单词之间由一个或多个空格分开,自然段之后可以用一个“,”或“。”表示结束。整个内容的单词数量不超过10000,不同的单词个数不超过500.

输出

输出占10行,每行一个单词及出现的次数,中间一个空格。要求按频率降序输出,出现次数相同的单词,按字典序输出。

样例输入

shooting is at shanghai station. shooting  must  
be carried out. shooting  shooting.
shanghai station must be surrounded,  at least a team of  one hundred  soldiers to fight.  twenty  five soldiers shooting in the north, twenty  five soldiers shooting in the south,  twenty  five soldiers  shooting in  the east, twenty  five soldiers shooting in the west.

样例输出

shooting 8
soldiers 5
five 4
in 4
the 4
twenty 4
at 2
be 2
must 2
shanghai 2
分析
输入有些郁闷,我完全没有看出来是以文本结束,然后就是用string比字符数组要方便很多。
我的字符数组的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct arry{
char words[51];
int times;
}a[501];

bool compare(const arry& x,const arry& y);
int main()
{
    char c;
    int i=0,j=0;
    char t[51];//先把输入的字符存在一个临时字符数组里
    int flag=0;
    while(scanf("%c",&c)!=EOF){
        if(c>='a'&&c<='z'){
            t[i]=c;
            i++;
        }
        else if(c==' '||c==','||c=='.'){//遇到这三个字符说明单词输入完毕,a数组里没有该单词就加入

                t[i]='\0';
               for(int jj=0;jj<j;jj++){
                if((strcmp(t,a[jj].words)==0)){

                    flag=1;
                    a[jj].times++;
                }
               }
               if(flag==0){
                for(int m=0;m<i;m++){
                    a[j].words[m]=t[m];
                }
                a[j].times=1;
                j++;
               }
               flag=0;
               i=0;
        }

    }
    sort(a,a+j,compare);
    for(int k=1;k<=10;k++){
        printf("%s %d\n",a[k].words,a[k].times);
    }
    return 0;
}
bool compare(const arry& x,const arry& y){
if(x.times==y.times){
   int t=strcmp(x.words,y.words);
    if(t>=0)
        return false;
    else
        return true;
}
else{
    return x.times>y.times;
}
}
string写的代码

 

摘自http://blog.csdn.net/xunalove/article/details/72578194
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<memory.h>
    #include<string>
    #include<algorithm>
    using namespace std;
    struct node
    {
        string name;
        int num;
    }a[501];
    bool cmp(node x,node y)
    {
        if(x.num!=y.num)
            return x.num>y.num;
        else
            return x.name<y.name;
    }
    int main()
    {
        char ch;
        int ok=0,t=0;
        string s="";
        while(scanf("%c",&ch)!=EOF)
        {
            if(ch>='a'&&ch<='z')
                s+=ch;
            else
            {
                int i;
                for(i=0;i<t;i++)
                {
                    if(s==a[i].name)
                    {
                        a[i].num++;
                        s="";
                        break;
                    }
                }
                if(i==t&&s!="")
                {
                    a[t].name=s;
                    a[t].num=1;
                    t++;
                    s="";
                }
            }
        }
        sort(a,a+t,cmp);
        for(int i=0;i<10;i++)
           cout<<a[i].name<<" "<<a[i].num<<endl;
        return 0;
    }

 

 
posted @ 2017-08-31 15:11  路人姜。  阅读(216)  评论(0编辑  收藏  举报