zoukankan      html  css  js  c++  java
  • 【河南省第十届ACM 省赛 A-谍报分析】

    题目描述

    “八一三”淞沪抗战爆发后,*几次准备去上海前线视察和指挥作战。但都因为宁沪之间的铁路和公路遭到了敌军的严密封锁,狂轰滥炸,一直未能成行。

    特科组织,其主要任务是保卫的安全,了解和掌握敌方的动向。经过一段时间的监听,谍报组获取了敌方若干份密报,经过分析,发现密文中频繁出现一些单词,情报人员试图从单词出现的次数中,推出敌军的行动计划。

    请你编程,快速统计出频率高的前十个单词。

    输入

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

    我的做法是先用map记录每个单词出现的次数,因为map不是顺序结构,没办法sort,所以存到vector<pair<string, int> >中。

    每次读一个字符,直到EOF

    #include <bits/stdc++.h>
    using namespace std;
    char s[50];
    map<string, int>M;
    vector<pair<string, int> >V;
    bool cmp(pair<string, int>x ,pair<string, int>y)
    {
        if(x.second == y.second)
            return x.first < y.first;
        return x.second > y.second;
    }
    int main()
    {
        char c;
        int n = 0, f = 0;
        while(~scanf("%c", &c))
        {
            if(c>='a'&&c<='z'||c>='A'&&c<='Z')
            {
                if(f)
                    s[n++] = c;
                else
                {
                    s[n++] = c;
                    f = 1;
                }
            }
            else
            {
                if(f)
                {
                    s[n] = '';
                    M[s]++;
                    n = 0;
                    f = 0;
                }
            }
        }
        for(map<string, int>::iterator it = M.begin(); it != M.end(); it++)
            V.push_back(make_pair(it->first, it->second));
        sort(V.begin(), V.end(), cmp);
        int num = 1;
        for(vector<pair<string, int> >::iterator it = V.begin(); it != V.end() && num <= 10; it++, num++)
            cout<<it->first<<" "<<it->second<<endl;
        return 0;
    }
  • 相关阅读:
    view 与layer
    xcode中create groups 和 create folder reference 的区别
    iOS 9 学习系列:UIStack View (转载)
    使用JQuery插件,排序Gridview的某个字段
    Fixed GridView Header
    在TextBox里面仅仅允许数字,按Enter键进入下一个TextBox
    实现AJAX局部刷新以及PageMethod方法的使用
    用户控件
    JSON的使用
    ASP.NET页面生命周期
  • 原文地址:https://www.cnblogs.com/lesroad/p/8811198.html
Copyright © 2011-2022 走看看