hihoCoder 1385 A Simple Job
Posted 月夜下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hihoCoder 1385 A Simple Job相关的知识,希望对你有一定的参考价值。
#1385 : A Simple Job
描述
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
解析:题意为给定一个文本,问哪个词组(只被1个或多个空格分开的相邻单词成为一个词组)出现的次数最多。如果有不止一个,输出字典序最小的那个。模拟即可。
#include <bits/stdc++.h> using namespace std; map<string, int> m; string w; char s[1000]; vector<string> v[100000]; vector<string> str; int main() { bool ok = false; int cnt = 0; int res = 0; while(gets(s)){ if(s[0] == ‘#‘){ for(int i = 0; i <= cnt; ++i){ int len = (int)v[i].size(); for(int j = 1; j < len; ++j){ string tmp = v[i][j-1]+" "+v[i][j]; ++m[tmp]; int num = m[tmp]; if(num> res){ res = num; str.clear(); str.push_back(tmp); } else if(num == res){ str.push_back(tmp); } } } sort(str.begin(), str.end()); cout<<*str.begin()<<":"<<res<<endl; m.clear(); res = 0; w = ""; str.clear(); for(int i = 0; i < cnt; ++i) v[i].clear(); } else{ for(int i = 0; s[i] != ‘\0‘; ++i){ if(islower(s[i])){ ok = true; w += s[i]; } else{ if(ok){ v[cnt].push_back(w); ok = false; w = ""; } if(s[i] == ‘,‘ || s[i] == ‘.‘){ ++cnt; } } } if(ok){ v[cnt].push_back(w); ok = false; w = ""; } ++cnt; } } return 0; }
以上是关于hihoCoder 1385 A Simple Job的主要内容,如果未能解决你的问题,请参考以下文章