C A Simple Job

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

 

模拟题,注意到相邻的两个单词算做词语,那么就是ab ab ab这样算两个ab ab了,重叠了一次。

然后用.或者,结束和用空格结束是不同的,因为用.结束不能和后面的组合了,pre要设置为-1

ggg ,,,, ggg ,,, gg , gg aa
gg,.......aa.. ..ggg gg gg.
aa g.g.g.......
####

aa g:1

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 600;
char str[1200];
char sub[2][maxn];
char tosub[1200];
map<string, int>mp;
void work() {
    mp.clear();
    int now = 0;
    int tim = 0;
    while(strcmp(str + 1, "####") != 0) {
        int lenstr = strlen(str + 1);
        int lennow = 0;
        int pre = -1;
        ++tim;
        str[lenstr + 1] = ' ';
        for (int i = 1; i <= lenstr + 1; ++i) {
            if ((str[i] == ',' || str[i] == '.') && pre != -1) {
                if (lennow == 0) {
                    pre = -1;
                    continue;
                }
                sub[now][lennow + 1] = '\0';
                int lenpre = strlen(sub[pre] + 1);
                strcpy(tosub + 1, sub[pre] + 1);
                tosub[lenpre + 1] = ' ';
                strcpy(tosub + lenpre + 2, sub[now] + 1);
//                cout << tosub + 1 << "++++" << tim << endl;
                lennow = 0;
                pre = -1;
                now = !now;
                mp[tosub + 1]++;
            } else if ((str[i] == ',' || str[i] == '.') && pre == -1) {
                lennow = 0;
                continue;
            } else if (str[i] == ' ') {
                if (lennow == 0) continue;
                sub[now][lennow + 1] = '\0';
                if (pre != -1) {
                    int lenpre = strlen(sub[pre] + 1);
                    strcpy(tosub + 1, sub[pre] + 1);
                    tosub[lenpre + 1] = ' ';
                    strcpy(tosub + lenpre + 2, sub[now] + 1);
//                    cout << tosub + 1 << " " << "****" << tim <<endl;
                    mp[tosub + 1]++;
                }
                lennow = 0;
                pre = now;
                now = !now;
            } else {
                sub[now][++lennow] = str[i];
            }
        }
        gets(str + 1);
    }
    int ans = -inf;
    string toans;
    for (map<string, int> :: iterator it = mp.begin(); it != mp.end(); ++it) {
        if (ans < it->second || ans == it->second && it->first < toans) {
            ans = it->second;
            toans = it->first;
        }
    }
    cout << toans << ":" << ans << endl;
    return;
}

int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    while(gets(str + 1) != NULL) {
        work();
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/liuweimingcprogram/p/5903211.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值