A Simple Job 模拟+multiset

来源:互联网 发布:网络电视盒 编辑:程序博客网 时间:2024/05/01 16:21

题目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 goodat good at above all me this is####world hello ok####样例输出at good:3hello ok:1

题意:
给出一个文本, 问那个词组出现次数最多. 被1个或多个空格分开的相邻单词背称为词组.

分析:
直接模拟,找出所有合法的词组。

AC代码:

#include <set>#include <string>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxLen = 1050;char word1[maxLen],word2[maxLen],str[maxLen];multiset<string> myset;int main(){    while(gets(str) != NULL)    {        memset(word1,0,sizeof(word1));        memset(word2,0,sizeof(word2));        if(strcmp(str,"####")==0)        {            int maxx = -1;            for(multiset<string>::iterator itr = myset.begin();itr!=myset.end();++itr)            {                int x = myset.count(*itr);                maxx = max(maxx,x);            }            //cout << maxx << endl;            for(multiset<string>::iterator itr = myset.begin();itr!=myset.end();++itr)            {                if(myset.count(*itr)==maxx)                {                    cout << *itr << ':' << maxx << '\n';                    break;                }            }            myset.clear();        }        else        {            int len = strlen(str);            int first = 0;            while(first < len)            {                //到一个合法单词                if(word1[0]=='\0')                {                    while(first < len && (str[first]<'a' || str[first]>'z'))                        first++;                    int last = first + 1;                    while(last < len && str[last]>= 'a' && str[last]<='z')                        last++;                    memset(word1,0,sizeof(for(map<string,int>::iterator it=mmap.begin();it!=mmap.end();it++)            {                int x = mmap.count(*it);                maxx = max(maxx,x);            }word1));                    strncpy(word1,str+first,last-first);                    first = last;                }                //printf("%s",word1);                //寻找第二个合法单词                while(first < len && str[first]==' ') //寻找第二个单词的首字母                    first++;                if(str[first]<'a' || str[first]>'z') //非字母                {                    memset(word1,0,sizeof(word1));                    continue;                }                else                {                    int last = first + 1;                    while(last < len && str[last]>= 'a' && str[last]<='z')                        last++;                    memset(word2,0,sizeof(word2));                    strncpy(word2,str+first,last-first);                    //printf("%s %s %d\n",word1,word2,last);                    string t1(word1),t2(word2);                    myset.insert(t1+" "+t2);                    memset(word1,0,sizeof(word1));                    strcpy(word1,word2);                    memset(word2,0,sizeof(word2));                    first = last;                }            }        }    }    return 0;}
1 0