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 ...
随机推荐
- python setuptools安装
执行# python setup.py install 时发生如下错误 Traceback (most recent call last): File "setup.py", li ...
- POJ3126Prime Path(BFS)
#include"cstdio" #include"queue" #include"cstring" using namespace std ...
- flume 配置与使用
1.下载flume,解压到自建文件夹 2.修改flume-env.sh文件 在文件中添加JAVA_HOME 3.修改flume.conf 文件(原名好像不叫这个,我自己把模板名改了) 里面我自己配的( ...
- nginx中给目录增加密码保护实现程序
一款nginx中给目录增加密码保护实现程序,可以有效的保护一些目录不被访问,有需要的朋友可参考一下. 了防止一些可能出现存在漏洞的后台脚本暴露,使用验证的方式保护这些文件所在的目录 使用apache的 ...
- openstack常见问题汇总
汇总下常见的问题以及解释下一些比较容易让人萌的参数配置等等 问题汇总1.使用纯文本模式进行复制粘贴,打死不要用word!!!可以解决绝大多数问题,如果你依然执迷不悟,那么就好自为之吧 2.创建路由器时 ...
- VisualGDB系列7:使用VS创建Linux静态库和动态库
根据VisualGDB官网(https://visualgdb.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指正. 本文介绍如何在VS中创建静态库和动态库, ...
- net start sql server (instance)
如何启动 SQL Server 实例(net 命令) 其他版本 可以使用 Microsoft Windows net 命令启动 Microsoft SQL Server 服务. 启动 SQL Se ...
- $.parseJSON失效的问题
郁闷死,之前都是用parseJSON来解析json的,这次竟然不行了,提示parseJSON is not a function,jquery是1.72的,经过前端的帮忙,var objs = JSO ...
- SVN使用技巧和参考文档总结
以下文章为网上收集: myEclipse 8.5下SVN环境的搭建(重点推荐) SVN建立版本库,配置用户和权限 Tortoise SVN使用方法,简易图解 版本控制软件SVN使用方法详解 学习笔记 ...
- p2071 座位安排
传送门 题目 已知车上有N排座位,有N*2个人参加省赛,每排座位只能坐两人,且每个人都有自己想坐的排数,问最多使多少人坐到自己想坐的位置. 输入格式: 第一行,一个正整数N. 第二行至第N*2+1行, ...