hihoCoder #1385 : A Simple Job(简单工作)

时间限制:1000ms
单点时限:1000ms
内存限制:256MB

Description - 题目描述

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.

计算机语言学研究所(ICL),是北大文理结合、多学科交叉,且致力于语言信息处理基础研究与应用的研究所。ICL的研究领域广泛,包括汉语语法、语义分析、计算词典学、语义词典、计算机语义学还有应用系统。
X教授在ICL工作。他有个9岁小的女儿略懂编程。女儿对他爸爸的研究很感兴趣。此次暑假,她参加了有北大信息学院带给孩子们的免费算法编程课。学毕,她对X教授说:“老爸,我已经学了很多很神奇的算法。现在我能帮你忙了!分给我一点你的研究吧!”X教授高兴地回答:“好,那我们就先从简单的开始。我会给你很多的句子,你要告诉我那个词组最常被使用。”
帮Jane敲个程序搞定这件事吧。

CN

Input - 输入

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.

测试用例不超过20组。
其中若干行文本均以"####"结束输入。每个文本包括若干单词,空格,','s 与 '.'。单词只由小写字母组成。两个相邻的单词即为一个“词组”。被一个或多个空格分隔的单词也是相邻的。没有任何单词被分割为两行,并且不同行的单词不能组成词组。两个词组间只有空格数量不同,则这两个词组是相同的。
注意,每行的最大长度为500个字符,每组测试用例至少有50行。每组测试用例至少有1个词组。

CN

Output - 输出

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.

对于每个测试用例,输出最常使用的词组与其出现次数,用一个':'隔开。如果存在多解,输出字典序最小的答案。注意,若单词间存在多个空格,只保留一个空格。

CN

Sample Input - 样例输入

above,all ,above all good at good at good
at good at above all me this is
####
world hello ok
####

Sample Output - 样例输出

at good:3
hello ok:1

【题解】

  刚刚看完题目的时候脑子有点激动,第一反应想来一发AC自动机。然而看了看提交数量有点……不科学。

  然后……额,好像直接用map来储存和维护就行了。

  WA点,在非严格初始化下,一定要处理好每个读取的字符串的结束标志,不然可能被之前的数据影响,比如下面这组数据。

b                   b
c c b
####

【代码 C++】

 #include <cstdio>
#include <string>
#include <map>
std::map<std::string, int> data;
std::string opt;
int maxn;
bool slove(){
opt.clear(); data.clear(); maxn = ;
char rd[], *i, *j;
bool isRD = , lst = ;
int n;
std::string temp;
while (gets(rd)){
isRD = ; temp.clear();
if (rd[] == '#') break;
for (i = rd; *i == ' '; ++i);
for (; *i; ++i){
if (*i == ',' || *i == '.'){
for (++i; *i == ' '; ++i);
temp.clear(); --i;
}
else{
temp += *i;
if (*i != ' ') continue;
for (++i; *i == ' '; ++i);
for (j = i; 'a' <= *j && *j <= 'z'; ++j) temp += *j;
if (i == j){ temp.clear(); --i; continue; }
data[temp] = (n = data[temp] + );
if (n > maxn || (n == maxn && temp < opt)) opt = temp, maxn = n;
temp.clear();
while (i < j) temp += *i++;
--i;
}
}
}
return isRD;
}
int main(){
while (slove()){
printf("%s:%d\n", opt.c_str(), maxn);
}
return ;
}

hihoCoder 1385 : A Simple Job(简单工作)的更多相关文章

  1. hihoCoder 1385 A Simple Job

    #1385 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (I ...

  2. Java设计模式:Simple Factory(简单工厂)模式

    概念定义 简单工厂(Simple Factory)模式,又称静态工厂方法(Static Factory Method)模式,即定义一个工厂类,根据传入的不同参数创建不同的产品实例,这些实例对象具有共同 ...

  3. React virtual DOM explained in simple English/简单语言解释React的虚拟DOM

    初学React,其中一个很重要的概念是虚拟DOM,看了一篇文章,顺带翻译一下. If you are using React or learning React, you must have hear ...

  4. Simple Factory 简单工厂模式(静态工厂)

    基本概念: 1) Simple Factory模式属于创建型模式, 2) 简单工厂模式是由一个工厂(注意是一个!)对象决定创建出哪一种产品类的实例(例如你到肯德基说你要鸡腿,要薯条,要饮料还是,,,这 ...

  5. Servlet的生命周期以及简单工作原理的讲解

    Servlet生命周期分为三个阶段: 1,初始化阶段              调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段           调用destr ...

  6. 林大妈的JavaScript基础知识(二):编写JavaScript代码前的一些简单工作

    在介绍JavaScript语法前,我们需要知道,学习语法必须要多利用手敲代码来巩固记忆.因此,由于JavaScript的特性,它不能像C++和Java一样独立地编译及运行,我们需要在调试运行JavaS ...

  7. [C#] Timer + Graphics To Get Simple Animation (简单的源码例子,适合初学者)

    >_<" 这是一个非常简单的利用C#的窗口工程创立的程序,用来做一个简单的动画,涉及Timer和Graphics,适合初学者,高手略过~

  8. Codeforces 665D Simple Subset [简单数学]

    题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除 ...

  9. 设计模式(四):SIMPLE FACTORY简单工厂模式 -- 创建型模式

    1.定义 简单工厂模式又称静态工厂方法模式.重命名上就可以看出这个模式一定很简单.它存在的目的很简单:定义一个用于创建对象的接口. 2.适用场景 如果一个客户要一款宝马车,一般的做法是客户去创建一款宝 ...

随机推荐

  1. 故障处理-ORA-00376/ORA-01110

    数据库实例启动之后发现,9号数据文件发生故障,file 9 cannot be read at this time, ORACLE Instance ilndb2 (pid = 16) - Error ...

  2. nginx反向代理(proxy_pass)tomcat的过程中,session失效的问题解决

    Nginx反向代理tomcat,很是方便,但是也有些细节的问题需要注意:今天遇到了这样一个问题,tomcat中路径“host/web1”,nginx中直接“host/”代理,这时候session就无法 ...

  3. 浅谈Entity Framework 增删改查和事务操作

    1.增加对象 DbEntity db = new DbEntity(); //创建对象实体,注意,这里需要对所有属性进行赋值(除了自动增长主键外),如果不赋值,则会数据库中会被设置为NULL(注意是否 ...

  4. SynchronousQueue 的简单应用

    SynchronousQueue是这样一种阻塞队列,其中每个 put 必须等待一个 take,反之亦然.同步队列没有任何内部容量,甚至连一个队列的容量都没有.      不能在同步队列上进行 peek ...

  5. 如何用ajax提交多组同样的数据(数组)到后台?

    我在AJAX中这样写 $("#subbutton").click(function(){          var machineCode_1=$("#machineCo ...

  6. ubifs物理存储

    Ubifs通过ubi管理MTD设备,ubi的LEB随机映射PEB,其本身占用一部分PEB,具体文件存储情况分析如下. 1. Ubi中不管是是逻辑块号还是物理块号都是从0开始的.一般情况下,Nandfl ...

  7. Nginx+Lua(OpenResty)开发高性能Web应用

    使用Nginx+Lua(OpenResty)开发高性能Web应用 博客分类: 跟我学Nginx+Lua开发 架构 ngx_luaopenresty 在互联网公司,Nginx可以说是标配组件,但是主要场 ...

  8. 时光煮雨 Unity3D实现2D人物动画② Unity2D 动画系统&资源效率

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoT ...

  9. string.Format出现异常"输入的字符串格式有误"的解决方法

    string.Format出现异常"输入的字符串格式有误"的解决方法 今天在做项目时,碰到一个很奇怪的问题,我使用string.Format居然报“输入的字符串格式有误”的错误,我 ...

  10. 【01:转自知乎:关于 openSUSE 】

    我是 openSUSE 中文维基唯一的非官方维护者,openSUSE 简体中文翻译团队召集人,linuxsir SuSE 版块的版主,openSUSE 官方论坛 http://forums.opens ...