HihoCoder - 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.

Input

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.

Output

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.

Sample Input

above,all ,above all good at good at good
at good at above all me this is
####
world hello ok
####

Sample Output

at good:3
hello ok:1

题解:

纯模拟,应该是2016年北京网络赛最简单的一道题了(全场只会这一道题 ,~5555绝望啊o(╥﹏╥)o)

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <map>

using namespace std;

map<pair<string,string>,int> MMP;

string s;

int main(){
	
	while(getline(cin,s)){
		if(s == "####")continue;
		MMP.clear();
		int ma = 0;
		pair<string,string> PP;
		while(s != "####"){
			string t1,t2;
			t1 = t2 = "";
			bool flag = false;
			pair<string,string> P;
			for(int i=0 ; i<s.length() ; ++i){
				if(s[i] == '.' || s[i] == ','){
					if(flag && t2!=""){
						P.first = t1;
						P.second = t2;
						MMP[P]++;
						if(MMP[P] > ma){
							ma = MMP[P];
							PP = P;
						}
					}
					t1 = t2 = "";
					flag = false;
					continue;
				}
				if(flag){
					if(s[i] == ' '){
						if(t2 == "");
						else {
							P.first = t1;
							P.second = t2;
							MMP[P]++;
							if(MMP[P] > ma){
								ma = MMP[P];
								PP = P;
							}
							else if(MMP[P] == ma){
								if(P.first < PP.first || (P.first==PP.first && P.second<PP.second))PP = P;
							}
							t1 = t2;
							t2 = "";
						}
					}
					else t2 += s[i];
				}
				else if(!flag){
					if(s[i] == ' '){
						if(t1 == "");
						else flag = true;
					}
					else t1 += s[i];
				}
			}
			if(flag && t2!=""){
				P.first = t1;
				P.second = t2;
				MMP[P]++;
				if(MMP[P] > ma){
					ma = MMP[P];
					PP = P;
				}
				else if(MMP[P] == ma){
					if(P.first < PP.first || (P.first==PP.first && P.second<PP.second))PP = P;
				}
			}
			getline(cin,s);
		}
		cout<<PP.first<<' '<<PP.second<<':'<<ma<<endl;
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值