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

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++】

 1 #include <cstdio>
 2 #include <string>
 3 #include <map>
 4 std::map<std::string, int> data;
 5 std::string opt;
 6 int maxn;
 7 bool slove(){
 8     opt.clear(); data.clear(); maxn = 0;
 9     char rd[505], *i, *j;
10     bool isRD = 0, lst = 0;
11     int n;
12     std::string temp;
13     while (gets(rd)){
14         isRD = 1; temp.clear();
15         if (rd[0] == ‘#‘) break;
16         for (i = rd; *i == ‘ ‘; ++i);
17         for (; *i; ++i){
18             if (*i == ‘,‘ || *i == ‘.‘){
19                 for (++i; *i == ‘ ‘; ++i);
20                 temp.clear(); --i;
21             }
22             else{
23                 temp += *i;
24                 if (*i != ‘ ‘) continue;
25                 for (++i; *i == ‘ ‘; ++i);
26                 for (j = i; ‘a‘ <= *j && *j <= ‘z‘; ++j) temp += *j;
27                 if (i == j){ temp.clear(); --i; continue; }
28                 data[temp] = (n = data[temp] + 1);
29                 if (n > maxn || (n == maxn && temp < opt)) opt = temp, maxn = n;
30                 temp.clear();
31                 while (i < j) temp += *i++;
32                 --i;
33             }
34         }
35     }
36     return isRD;
37 }
38 int main(){
39     while (slove()){
40         printf("%s:%d\n", opt.c_str(), maxn);
41     }
42     return 0;
43 }
时间: 2024-10-29 20:41:58

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

hihoCoder 1385 A Simple Job

#1385 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (ICL), Peking University is an interdisciplinary institute of science and liberal arts, it focuses primarily on the fundamental researches and applicati

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

1.定义 简单工厂模式又称静态工厂方法模式.重命名上就可以看出这个模式一定很简单.它存在的目的很简单:定义一个用于创建对象的接口. 2.适用场景 如果一个客户要一款宝马车,一般的做法是客户去创建一款宝马车,然后拿来用.后来出现工业革命.用户不用去创建宝马车.因为客户有一个工厂来帮他创建宝马.想要什么车,这个工厂就可以建.比如想要320i系列车.工厂就创建这个系列的车.即工厂可以创建产品. 3.评价 优点: 工厂类是整个模式的关键.包含了必要的逻辑判断,根据外界给定的信息,决定究竟应该创建哪个具体

Simple Rules/简单的规律

<编程导论(Java)·3.1.1 三种结构.Java语句> [p94]计算机科学家Corrado Bohm和Giuseppe Jacopini证明,使用顺序(sequencing),选择(alternation)和循环(iteration)这三种结构就足以表达所有程序的本质.世事如棋局局新,每一局棋都是新局,世界上的程序也一样,无穷无尽极富变化,然而它受十分简单的规则(三种基本结构)所支配.--早期的编程书籍中,会有顺序程序设计,选择结构程序设计和循环结构程序设计等说法.有时候,人们将它们称

Servlet的生命周期以及简单工作原理

Servlet生命周期分为三个阶段: 1,初始化阶段  调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段 调用destroy()方法 Servlet初始化阶段: 在下列时刻Servlet容器装载Servlet: 1,Servlet容器启动时自动装载某些Servlet,实现它只需要在web.XML文件中的<Servlet></Servlet>之间添加如下代码: <loadon-startup>1</loadon-startup&g

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

>_<" 这是一个非常简单的利用C#的窗口工程创立的程序,用来做一个简单的动画,涉及Timer和Graphics,适合初学者,高手略过~ PS:请忽略菜单栏的东西~背景改成了白色! 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 us

simple queue(简单队列)

1.simple queue模型 2.创建一个连接工具类 package com.dwz.rabbitmq.util; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class ConnectionUtils {

Linux内核BPF的简单工作原理

BPF用于很多的抓包程序,在linux中,一般内核自动编译进了af_packet这个驱动,因此只需要在用户态配置一个PACKET的socket,然后将filter配置进内核即可,使用setsockopt的SO_ATTACH_FILTER 命令,这个filter是在用户空间配制的,比如tcpdump应用程序,tcpdump和内核BPF过滤器的关系类似iptables与netfilter的关系,只是Netfilter实现了match/target的复合配合,而BPF的target则只是选择是否要与不

hihoCoder #1234 : Fractal(数学简单题)

时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 This is the logo of PKUACM 2016. More specifically, the logo is generated as follows: 1. Put four points A0(0,0), B0(0,1), C0(1,1), D0(1,0) on a cartesian coordinate system. 2. Link A0B0, B0C0, C0D0, D0A0 separat

Flex开发简单工作流程设计工具

在线预览地址 http://rj.8634.com/xiaoshandong/workflowdesigner/workflowdesigner.html 源代码下载   http://files.cnblogs.com/files/ffmpeg/WorkFlowDesigner.zip 截图   源代码 WorkFlowDesigner.mxml <?xml version="1.0" encoding="utf-8"?> <mx:Applica