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 ...
随机推荐
- B+树索引和哈希索引的明显区别是:
如果是等值查询,那么哈希索引明显有绝对优势,因为只需要经过一次算法即可找到相应的键值:当然了,这个前提是,键值都是唯一的.如果键值不是唯一的,就需要先找到该键所在位置,然后再根据链表往后扫描,直到找到 ...
- varnish安装和配置
实验环境:CentOS7 Varnish是高性能开源的反向代理服务器和HTTP缓存服务器. #varnish服务器:172.16.252.142 [root@varnish localhost]#yu ...
- sell02 展现层编写
# API ###商品列表 ``` GET /sell/buyer/product/list ``` 参数 ``` 无 ``` 返回 ``` { "code": 0, " ...
- [原创]Devexpress XtraReports 系列索引
该系列已经完结...以后如果有高级功能,会再开一个新的系列,该系列是比较基础的报表应用..谢谢大家一直的支持. [原创]Devexpress XtraReports 系列 1 创建静态报表 Demo地 ...
- Centos下添加/删除用户
useradd具体参数 [root@yhwang ~] useradd -h Usage: useradd [options] LOGIN useradd -D useradd -D [options ...
- Spring入门第一课
今天先不讲Spring是什么. Spring网址:http://projects.spring.io/spring-framework/ Eclipse 安装开发IDE 在Eclipse Market ...
- 18.phpmyadmin 4.8.1 远程文件包含漏洞(CVE-2018-12613)
phpmyadmin 4.8.1 远程文件包含漏洞(CVE-2018-12613) phpMyAdmin是一套开源的.基于Web的MySQL数据库管理工具.其index.php中存在一处文件包含逻辑, ...
- jquery提供的数据提交方式1
1,ajax提交,部分代码为easyui function editNode(id){ $.ajax({ dataType:'json', async:true, //默认的就是异步提交 type:' ...
- grep的常用命令语法
grep的常用命令语法 1. 双引号引用和单引号引用在g r e p命令中输入字符串参数时,最好将其用双引号括起来.例如:"m y s t r i n g".这样做有两个原因,一是 ...
- 【mybatis 如何写union和union查询】
select d.* from (select a.CheckType,b.UserName,a.CheckNumber, a.PayName ,a.PayBank,a.PayBankNumber,a ...