hihocoder 1385.A Simple Job(ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

443

题目


描述  


Institute of Computational Linguistics (ICL), Peking University is an interdisciplinary institute of science and liberal arts, it focuses primarily on the fundamental researches and applications of language information processing. The research of ICL covers a wide range of areas, including Chinese syntax, language parsing, computational lexicography, semantic dictionaries, computational semantics and application systems.

Professor X is working for ICL. His little daughter Jane is 9 years old and has learned something about programming. She is always very interested in her daddy's research. During this summer vacation, she took a free programming and algorithm course for kids provided by the School of EECS, Peking University. When the course was finished, she said to Professor X: "Daddy, I just learned a lot of fancy algorithms. Now I can help you! Please give me something to research on!" Professor X laughed and said:"Ok, let's start from a simple job. I will give you a lot of text, you should tell me which phrase is most frequently used in the text."

Please help Jane to write a program to do the job.


输入  


There are no more than 20 test cases.

In each case, there are one or more lines of text ended by a line of "####". The text includes words, spaces, ','s and '.'s. A word consists of only lowercase letters. Two adjacent words make a "phrase". Two words which there are just one or more spaces between them are considered adjacent. No word is split across two lines and two words which belong to different lines can't form a phrase. Two phrases which the only difference between them is the number of spaces, are considered the same.

Please note that the maximum length of a line is 500 characters, and there are at most 50 lines in a test case. It's guaranteed that there are at least 1 phrase in each test case.


输出  


For each test case, print the most frequently used phrase and the number of times it appears, separated by a ':' . If there are more than one choice, print the one which has the smallest dictionary order. Please note that if there are more than one spaces between the two words of a phrase, just keep one space.


样例输入  


above,all ,above all good at good at good
at good at above all me this is
####
world hello ok
####
样例输出
at good:3
hello ok:1



题解


统计词组的频率,输出频率最高的词组中字典序最小的一个

首先要进行的是处理字符串,换行、 ,. 都会截断词组
而空格不会对词组造成影响,为了方便使用自己写的读入函数

读入后要做的就是根据不同的读入情况,组合前后两个词(如果可以称为词组)
然后计数,直接使用 map 即可

由于 map 本身的存储形式是二叉树存储(采用二分查找,因此速度很快)
因此 map 本身就是按照字典序存储的
所以直接用 迭代器(iterator) 从前遍历
第一个计数是最大值的就是要输出的

代码


/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

typedef long long LL;

const int INF = 0x7FFFFFFF;
const double eps = 1e-10;

int kase = 1;

map<string,int> mp;

int read_string(string &s) {
    char c;
    s = "";
    while(c=getchar(),!((c >= 'a'&&c <= 'z')||c=='#')) {
        if(c == '\n' || c == ',' || c == '.')
            return 0;
        if(c == EOF)
            return -1;
    }
    while(((c >= 'a'&&c <= 'z')||c=='#')) {
        s += c;
        c = getchar();
    }
    if(c == '\n' || c == ',' || c == '.')
        return 2;
    return 1;
}

int MapPush(string s) {
    if(mp.count(s) == 0)
        mp.insert(pair<string,int>(s,0));
    mp[s]++;
    return mp[s];
}

bool Do() {
    int Max = 0;
    mp.clear();

    string a="",b="";
    while(1) {
        int res = read_string(b);
        if(b == "####")
            break;

        if(res == -1)
            return false;
        if(res == 0) {
            a = b;
            b = "";
        } 
        if(res == 2) {
            if(a != "") {
                string Add = a + " " + b;
                Max = max(Max,MapPush(Add));
            }
            a = "";
            b = "";
        }
        if(res==1){
            if(a != "") {
                string Add = a + " " + b;
                Max = max(Max,MapPush(Add));
            }
            a = b;
            b = "";
        }
    }

    map<string,int>::iterator it = mp.begin();
    while(it != mp.end()) {
        if(it->second == Max) {
            cout << it->first << ":" << Max << endl;
            break;
        }
        ++it;
    }

    return true;
}

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    while(Do());

    return 0;
}
发布评论
  • 点击查看/关闭被识别为广告的评论