题目3 : 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define INF 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
using namespace std;
typedef long long ll;
int main()
{
int len;
int wordlen,wordnum;
char str[600];
char word[600];
char wordstr[600][600];
char phrase[600];
map<string,int> p;
map<string,int>::iterator it;
map<string,int>::iterator temp;
while(gets(str)!=NULL)
{
if(strcmp(str,"####")==0)
{
int maxx=-100;
for(it =p.begin();it!=p.end();it++){
if(maxx < it->second)
{
maxx = it->second;
temp = it;
}
}
cout << temp->first<<":"<<temp->second<<endl;
p.erase(p.begin(),p.end());
continue;
}
wordlen = 0;
wordnum = 0;
len = strlen(str);
for(int i=0;i<=len;i++)
{
if(str[i]>='a'&&str[i]<='z')
{
word[wordlen++] = str[i];
}
else if(str[i] == ' ')
{
while(str[i+1] == ' ') i++;
if(strlen(word)!=0)
strcpy(wordstr[wordnum++],word);
wordlen = 0;
memset(word,0,sizeof(word));
}
else if(str[i] == ','||str[i] == '.'||str[i] == '\0')
{
if(strlen(word)!=0)
strcpy(wordstr[wordnum++],word);
wordlen = 0;
memset(word,0,sizeof(word));
for(int i=0;i<wordnum-1;i++)
{
memset(phrase,0,sizeof(phrase));
strcpy(phrase,wordstr[i]);
strcat(phrase," ");
strcat(phrase,wordstr[i+1]);
//printf("phrase=%s\n",phrase);
if((it=p.find(phrase)) == p.end())
p.insert(pair<string,int>(phrase,1));
else
it->second++;
}
wordnum = 0;
memset(wordstr,0,sizeof(wordstr));
}
}
}
}