C A Simple Job
题目3 : 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 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
模拟题,注意到相邻的两个单词算做词语,那么就是ab ab ab这样算两个ab ab了,重叠了一次。
然后用.或者,结束和用空格结束是不同的,因为用.结束不能和后面的组合了,pre要设置为-1
ggg ,,,, ggg ,,, gg , gg aa
gg,.......aa.. ..ggg gg gg.
aa g.g.g.......
####
aa g:1
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = ;
char str[];
char sub[][maxn];
char tosub[];
map<string, int>mp;
void work() {
mp.clear();
int now = ;
int tim = ;
while(strcmp(str + , "####") != ) {
int lenstr = strlen(str + );
int lennow = ;
int pre = -;
++tim;
str[lenstr + ] = ' ';
for (int i = ; i <= lenstr + ; ++i) {
if ((str[i] == ',' || str[i] == '.') && pre != -) {
if (lennow == ) {
pre = -;
continue;
}
sub[now][lennow + ] = '\0';
int lenpre = strlen(sub[pre] + );
strcpy(tosub + , sub[pre] + );
tosub[lenpre + ] = ' ';
strcpy(tosub + lenpre + , sub[now] + );
// cout << tosub + 1 << "++++" << tim << endl;
lennow = ;
pre = -;
now = !now;
mp[tosub + ]++;
} else if ((str[i] == ',' || str[i] == '.') && pre == -) {
lennow = ;
continue;
} else if (str[i] == ' ') {
if (lennow == ) continue;
sub[now][lennow + ] = '\0';
if (pre != -) {
int lenpre = strlen(sub[pre] + );
strcpy(tosub + , sub[pre] + );
tosub[lenpre + ] = ' ';
strcpy(tosub + lenpre + , sub[now] + );
// cout << tosub + 1 << " " << "****" << tim <<endl;
mp[tosub + ]++;
}
lennow = ;
pre = now;
now = !now;
} else {
sub[now][++lennow] = str[i];
}
}
gets(str + );
}
int ans = -inf;
string toans;
for (map<string, int> :: iterator it = mp.begin(); it != mp.end(); ++it) {
if (ans < it->second || ans == it->second && it->first < toans) {
ans = it->second;
toans = it->first;
}
}
cout << toans << ":" << ans << endl;
return;
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
while(gets(str + ) != NULL) {
work();
}
return ;
}
C A Simple Job的更多相关文章
- PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)
最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...
- Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】
原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...
- WATERHAMMER: A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION
开启阅读模式 WATERHAMMER A COMPLEX PHENOMENON WITH A SIMPLE SOLUTION Waterhammer is an impact load that is ...
- BZOJ 3489: A simple rmq problem
3489: A simple rmq problem Time Limit: 40 Sec Memory Limit: 600 MBSubmit: 1594 Solved: 520[Submit] ...
- Le lié à la légèreté semblait être et donc plus simple
Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- 设计模式之简单工厂模式Simple Factory(四创建型)
工厂模式简介. 工厂模式专门负责将大量有共同接口的类实例化 工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类. 工厂模式有三种形态: 1.简单工厂模式Simple Factory ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- 关于The C compiler "arm-none-eabi-gcc" is not able to compile a simple test program. 的错误自省...
在 GCC ARM Embedded https://launchpad.net/gcc-arm-embedded/ 上面下载了个arm-none-eabi-gcc 用cmake 编译时 #指定C交叉 ...
- A Simple OpenGL Shader Example II
A Simple OpenGL Shader Example II eryar@163.com Abstract. The OpenGL Shading Language syntax comes f ...
随机推荐
- MySQL数据库服务器参数优化mycnf,16G内存8核CPU,
业务场景: 后台支持手机在线更新系统,db服务器内存16G,8核,dell的pc服务器. qps: 200个左右 tps: 1个左右 一分钟50几个 sort_buffer_size = 32M 大了 ...
- JVM插庄之一:JVM字节码增强技术介绍及入门示例
字节码增强技术:AOP技术其实就是字节码增强技术,JVM提供的动态代理追根究底也是字节码增强技术. 目的:在Java字节码生成之后,对其进行修改,增强其功能,这种方式相当于对应用程序的二进制文件进行修 ...
- JVM插码之三:javaagent介绍及javassist介绍
本文介绍一下,当下比较基础但是使用场景却很多的一种技术,稍微偏底层点,就是字节码插庄技术了...,如果之前大家熟悉了asm,cglib以及javassit等技术,那么下面说的就很简单了...,因为下面 ...
- 输出缓存与CachePanel
缓存的级别 缓存的作用自不必说,提高系统性能最重要的手段之一.上至应用框架,下至文件系统乃至CPU,计算机中各部分设计都能见到缓存的身影.许多朋友一直在追求如何提高Web应用程序的性能,其实最容易被理 ...
- kvm ip查看
1.virsh --list(查看有哪些服务器) 2.virsh dumpxml 虚拟机名称 查看服务器对应的mac地址 3.然后再宿主机上arp -a 查看对应的mac地址对应的ip
- 报错apachectl restart
httpd not running, trying to start(98)Address already in use: make_sock: could not bind to address [ ...
- Angular11 模板表单、响应式表单(自定义验证器)、HTTP、表单元素双向绑定
1 模板表单 模型通过指令隐式创建 技巧01:需要在模块级别引入 FormsModule ,通常在共享模块中引入再导出,然后在需要用到 FormsModule 的模块中导入共享模块就可以啦 impor ...
- c# DateTime.ToString()转换为统一的格式
DateTime.ToString()的转换结果时根据当前电脑的显示格式来转换的,不能同意,而且有些格式我们想将他们重新转换为时间时,会报错,如: 2017/11/21/周二 10:23:57,如果转 ...
- 1. xss原理分析与剖析(1)
0×01 前言: <xss攻击手法>一开始在互联网上资料并不多(都是现成的代码,没有从基础的开始),直到刺的<白帽子讲WEB安全>和cn4rry的<XSS跨站脚本攻击剖析 ...
- UITableViewCell 的复用机制
cell重用机制 http://blog.cnrainbird.com/index.php/2012/03/20/guan_yu_uitableview_de_cell_fu_yong_tan_tan ...