A Simple Job 2016北京网赛

本文介绍了一个简单的编程任务:从给定的文本中找出最常出现的词组,并提供了一个实现该功能的C++代码示例。
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

依然是模拟 vector< vector< string > >vec
vec[i]保存第i段所有单词
从字母开始 遇到非字母 断一个单词出来 存在push_back到vec.back()中
遇到非字母非空白或者换行 push_back(vector< string >())进vec中
然后穷举 map记录短语出现次数

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<bitset>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007

vector<vector<string> >vec;
map<string,int>mp;
vector<string>tmpvecstr;//空的vec 换行 遇到逗号等 插入的时候用

void f(string&str){
    string tmp;
    vec.push_back(tmpvecstr);//另起一段
    for(string::iterator it=str.begin();it<str.cend();)
        if(isalpha(*it)){//获取单词
            string::iterator it2=it;
            for(;isalpha(*it2);++it2);
            tmp=str;
            vec.back().push_back(tmp.assign(it,it2));
            it=it2;
        }
        else//逗号 句点等 另起一段
            if(!isspace(*it)){
                vec.push_back(tmpvecstr);
                ++it;
            }
            else//空白字符
                ++it;
}

void cau(){
//词组存mp中
    for(int i=0;i<vec.size();++i){
        vector<string>&tvec=vec[i];
        for(int j=0;j<(int)tvec.size()-1;++j)
            ++mp[tvec[j]+' '+tvec[j+1]];
    }
    string ansstr=mp.begin()->first;//记录答案
    int ansnum=mp.begin()->second;//答案出现次数
    for(map<string,int>::iterator it=mp.begin();it!=mp.end();++it)
        if(it->second>ansnum){
            ansstr=it->first;
            ansnum=it->second;
        }
        else
            if((it->second==ansnum)&&(it->first<ansstr)){
                ansstr=it->first;
            }
    cout<<ansstr<<":"<<ansnum<<endl;
}

int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    string str;
    while(getline(cin,str)){
        if(str=="####"){
            cau();
            vec.clear();
            mp.clear();
        }
        else
            f(str);
    }
    return 0;
}
### SimpleJob 接口详解 #### 实现自定义作业逻辑 `SimpleJob` 是 Quartz 中的一个接口,用于简化作业的开发过程。任何实现了 `SimpleJob` 的类都必须重写 `execute` 方法来定义具体的业务逻辑[^1]。 ```java public class MyCustomJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 获取传递给任务的数据映射 JobDetail jobDetail = context.getJobDetail(); JobDataMap dataMap = jobDetail.getJobDataMap(); String message = dataMap.getString("message"); System.out.println("Executing custom job with message: " + message); } } ``` #### 构建并配置 JobDetail 对象 为了使调度器能够识别和运行上述自定义的任务,需要通过 `JobBuilder` 来构建相应的 `JobDetail` 实例,并设置必要的属性: ```java // 创建job详情对象, 并指定对应的job实现类以及一些静态数据 JobDetail job = newJob(MyCustomJob.class) .withIdentity("myJob", "group1") // 设置job名称和组名 .usingJobData("message", "Hello from my simple job!") // 添加到JobDataMap中的键值对 .build(); ``` #### 调度执行计划 最后一步是安排何时何地触发此任务。这可以通过创建一个合适的 Trigger 完成,比如下面的例子展示了如何设定每分钟触发一次的时间表: ```java Trigger trigger = newTrigger() .withIdentity("trigger1", "group1") .startNow() .withSchedule(simpleSchedule().withIntervalInMinutes(1).repeatForever()) .build(); ``` 一旦有了 `JobDetail` 和 `Trigger` ,就可以将其提交给 Scheduler 进行管理了: ```java Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); ``` 以上就是关于 `SimpleJob` 接口的基本介绍及其使用方式。需要注意的是,由于每次执行都会新建实例,因此不建议在任务内部保存状态信息;如果确实有跨次调用的需求,则应借助外部机制如数据库或缓存服务来保持一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值