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 ...
随机推荐
- logback个人使用配置
提供一个目前个人供词使用的,无需日志汇总的日志配置文件: <?xml version="1.0" encoding="UTF-8"?> <co ...
- POJ2449:K短路
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 26355 Accepted: 7170 ...
- mina中的发送延时
由于项目需要,用到了 mina 框架进行 tcp 通讯.我是初次接触 mina,于是从 Hello world 开始学习了 mina .期间遇到了一个奇怪的发送数据的延迟问题,解决的过程是曲折的,但找 ...
- 第 六 课 GO语言常量
http://www.runoob.com/go/go-constants.html 一 常量 是一个简单值的标识符,在程序运行时,不会被修改的量. 常量中的数据类型只可以是布尔型.数字型(整数型.浮 ...
- 移植完linux-3.4.2内核,启动系统后使用命令ifconfig -a查看网络配置,没有eth0
问题: / # ifconfig / # ifconfig eth0 ifconfig: eth0: error fetching interface information: Device not ...
- Python-实现与metasploit交互并进行ms17_010攻击
关于ms17_010,可参考http://www.cnblogs.com/sch01ar/p/7672454.html 目标IP:192.168.220.139 本机IP:192.168.220.14 ...
- sql 基础总结
- error C2872: “flann”: 不明确的符号 --- PCL 与OpenCV2 的flann命名空间冲突问题的解决方法
error C2872: "flann": 不明确的符号 - PCL 与OpenCV2命名空间冲突问题的解决方法 error C2872: "flann" 如果 ...
- 杭电acm 1022题
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- HTML基础:
HTML是英文HyperText Markup Language的缩写,即超级文本标记语言,是用于描述网页文档的一种标记语言.HTMl是目前网络上应用最为广泛的语言,也是构成网页文档的主要语言.HTM ...