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 ...
随机推荐
- 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge
目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...
- C# 播放音乐
用 .NET 自带的类库 System.Media 下面的 SoundPlayer 来播放音乐的方式,此种方式使用托管代码,应该是更为可取的方式吧 使用起来非常简单,下面稍作说明: . 支持同步.异步 ...
- [bzoj3670] [NOI2014] [lg2375] 动物园
nxt数组为KMP的next数组num[i]储存了i前面可以匹配的串的个数.先在KMP求nxt中顺便求出num最后再找到对于i的最大的前后缀不重叠的可匹配的j,ans*=(num[j]+1)%1000 ...
- Wannafly 锁
题意: 现在有 $n$ 个人,每个人有一个已然给定的重要度 $a_i$,现有 K 个锁,每个锁有若干钥匙,分配给一些人,要求一群人能够打开全部 $K$ 把锁, 当且仅当他们重要度的和大于等于 $m$, ...
- 第五课5、ROS客户端2
1.简单的主题(topic)发布者和主题订阅者: 编写主题发布者节点需要: a.初始化ROS系统: b.广播消息:在foo主题上发布Foo_type_msg类型的消息 c.指定频率发布消息到foo主题 ...
- 在Ionic Framework 退出APP
使用以下代码: ionic.Platform.exitApp(); 在控制器中增加: $scope.Quit = function(){ ionic.Platform.exitApp(); }
- spring framework 源码
spring framework 各版本源码下载地址 现在spring的源码下载地址真是不好找,这次终于找到了.记录一下,以帮助需要的朋友. https://github.com/spring-pro ...
- Makefiles
A tutorial by example Compiling your source code files can be tedious, specially when you want to in ...
- js或jQuery中 邮箱跳转的问题,跳转到指定邮箱(通过layui的ifram实现)
对刚做的东西记个笔记 如果遇到同样问题解决起来又问题的欢迎留言 var emailtext = $("#TextBoxEmail").val();//获得要截取的值 var arr ...
- 【mybatis批量插入】
mybatis批量插入操作: MySQL:1.INSERT INTO TABLE_NAME(ID,NAME)VALUES(1,'张三'),(2,'李四') 2.INS ...