题目链接:http://hihocoder.com/problemset/problem/1385
#1385 : A Simple Job
-
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
描述
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.
题目大意:求最多词组的个数,如果相同,输出字典序小的那个
解析:把 ‘ , ’ 和 ‘ . ’变成空格,他们之间的字符串作为单独的一行,用map记录不同字符串的个数
代码如下:
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 1e9;
const int mod = 1<<30;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
char s[N], e[5] = "####";
map<string, int> mp;
int num = 0;
char ans[N];
void inint(int l, int r)
{
int f = 0, m = 0;
char a[N], c[N];
for(int i = l; i <= r; i++)
{
if(s[i] == ' ')
{
f++;
while(s[i] == ' ') i++;
i--;
}
else a[m++] = s[i];
if(s[i] == ' ' && f == 1)
{
a[m] = ' '; a[m + 1] = 0;
strcpy(c, a);
m = 0;
}
if(s[i] == ' ' && f == 2)
{
a[m] = 0;
strcat(c, a);
if(!mp.count(c)) mp[c] = 0;
mp[c]++;
if(mp[c] > num) { num = mp[c]; strcpy(ans, c); }
else if(strcmp(ans, c) > 0 && mp[c] == num) { strcpy(ans, c);}
a[m] = ' '; a[m + 1] = 0;
strcpy(c, a);
f = 1; m = 0;
}
}
if(m > 0 && f)
{
a[m] = 0; strcat(c, a);
if(!mp.count(c)) mp[c] = 0;
mp[c]++;
if(mp[c] > num){ num = mp[c]; strcpy(ans, c); }
else if(strcmp(ans, c) > 0 && mp[c] == num) { strcpy(ans, c);}
}
}
int main()
{
num = ans[0] = 0;
while(gets(s))
{
if(strcmp(s, e) == 0)
{
printf("%s:%d\n", ans, num);
mp.clear(); num = ans[0] = 0;
}
int j = 0, i;
for(i = 0; s[i]; i++)
{
if(s[i] == ',' || s[i] == '.')
{
s[i] = ' ';
inint(j, i - 1);
while(s[i] == ' '|| s[i] == ',' || s[i] == '.') i++;
j = i;
i--;
}
}
inint(j, i - 1);
}
return 0;
}