zoukankan      html  css  js  c++  java
  • 关于CCR测评器的自定义校验器(Special Judge)

    引言

    有时我们需要使用CCR测评器(CCR-Plus是一个开源的信息学竞赛测评软件,Github链接https://github.com/sxyzccr/CCR-Plus)进行SpecialJudge(以下简称SPJ)。例如判断选手输出与标准输出的差距,大于一定的值就算错,这时就需要用SpecialJudge了。

    在CCR测评器中,SPJ是用一项叫做自定义校验器的功能实现的。CCR的文档没有写明校验器的语法,网上也没有这一类的信息。于是,我在CCR的源代码中找到了CCR的默认校验器(全文比较),并将校验器的写法写成此篇博客。

    正文

    SPJ程序的位置

    编译好的SPJ程序放在dataprob目录下(prob是题目名)。

    如何写SPJ校验器

    查看CCR默认全文比较校验器源代码:

    // https://github.com/sxyzccr/CCR-Plus/blob/master/src/tools/checker/fulltext_utf8.cpp
    #include <string>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    string In, Out, Ans, Log;
    FILE* fout, *fans, *flog;
    
    void End(const string& info, double x, int state = 0)
    {
        fprintf(flog, "%.3lf
    %s
    ", x, info.c_str());
        exit(state);
    }
    
    inline void filter(string& s)
    {
        for (; s.size() && isspace(s[s.size() - 1]); s.erase(s.size() - 1));
    }
    
    string elided(const string& s, int p)
    {
        string pre = "", suf = "";
        for (int i = 0; i < p; i++) pre.push_back(s[i]);
        if (pre.size() > 3) pre = string("…") + pre.substr(pre.size() - 3, 3);
        int l = s.size() - p;
        if (pre.size() + l >= 13) l = 11 - pre.size(), suf = "…";
        for (int i = 0; i < l; i++) pre.push_back(s[p + i]);
        return pre + suf;
    }
    
    int compare(const string& a, const string& b)
    {
        int la = a.length(), lb = b.length();
        for (int i = 0; i < la && i < lb; i++) if (a[i] != b[i]) return i;
        return la != lb ? min(la, lb) : -1;
    }
    
    void Open()
    {
        if (Log.size()) flog = fopen(Log.c_str(), "w"); else flog = stdout;
        if (flog == NULL) exit(1);
        if ((fans = fopen(Ans.c_str(), "r")) == NULL) exit(1);
        if ((fout = fopen(Out.c_str(), "r")) == NULL) exit(1);
    }
    
    void Check()
    {
        char s[256];
        for (int i = 1; !feof(fout) || !feof(fans); i++)
        {
            string s1 = "", s2 = "";
            char c1 = -1, c2 = -1;
            for (; !feof(fans) && (c1 = fgetc(fans)) != '
    ';) if (c1 != -1) s1.push_back(c1);
            for (; !feof(fout) && (c2 = fgetc(fout)) != '
    ';) if (c2 != -1) s2.push_back(c2);
            if (feof(fout) && s1 != "" && s2 == "")
            {
                if (i == 1) End("选手输出为空", 0);
                sprintf(s, "第%d行 标准输出:"%s" 选手输出已结尾", i, elided(s1, 0).c_str());
                End(s, 0);
            }
            if (feof(fans) && s1 == "" && s2 != "")
            {
                sprintf(s, "第%d行 标准输出已结尾 选手输出:"%s"", i, elided(s2, 0).c_str());
                End(s, 0);
            }
            filter(s1), filter(s2);
            int p = compare(s1, s2);
            if (p >= 0)
            {
                sprintf(s, "第%d行 标准输出:"%s" 选手输出:"%s"", i, elided(s1, p).c_str(), elided(s2, p).c_str());
                End(s, 0);
            }
        }
    }
    
    int main(int argc, char* argv[])
    {
        In = "";
        Ans = argc < 3 ? "" : argv[2];
        Out = argc < 4 ? "" : argv[3];
        Log = argc < 5 ? "" : argv[4];
        Open();
        Check();
        End("", 1);
    }
    

    SPJ程序需要两个必要的函数:

    FILE* fout, *fans, *flog;
    void End(const string& info, double x, int state = 0){
        fprintf(flog, "%.3lf
    %s
    ", x, info.c_str());
        exit(state);
    }
    void Open(){
        if (Log.size()) flog = fopen(Log.c_str(), "w"); else flog = stdout;
        if (flog == NULL) exit(1);
        if ((fans = fopen(Ans.c_str(), "r")) == NULL) exit(1);
        if ((fout = fopen(Out.c_str(), "r")) == NULL) exit(1);
    }
    

      

    Open()是进行程序的初始化,End则是返回分数和备注。

    当CCR需要调用校验器时,它会向SPJ程序传递一个参数数组argv[]。数组的第二项为标准答案,第三项为选手答案,第四项为一些日志。

    当程序开始时,我们先获取这几个参数。

    int main(int argc, char* argv[])
    {
        string In, Out, Ans, Log;
        In = "";
        Ans = argc < 3 ? "" : argv[2];
        Out = argc < 4 ? "" : argv[3];
        Log = argc < 5 ? "" : argv[4];
        //Ans和Out变量存储的是标准答案和选手答案的路径
    }
    

      

    接下来我们需要读入标准答案和选手答案。我们使用freopen重定向到答案文件。

    Open();//初始化,这句很重要。
    double answer,output;//标准输出和选手输出
    freopen(Ans.c_str(),"r",stdin);//重定向到标准输出
    cin>>answer;//读入
    freopen(Out.c_str(),"r",stdin);//重定向到选手输出
    cin>>output;//读入
    

      

    接下来是判断,具体的读入和判断过程因题目而异。

    判断完成后需要使用End函数返回结果。End函数的使用很简单,第一个参数是要显示在测评记录上的字符串,第二个参数是double类型的,表示分数百分比,是一个0-1的值(例如这个测试点为10分,这个参数为0.6,那么这个测试点最终得分就是6分)。

    if (abs(ans-output)>0.02){
        End("与标准答案相差过大",0);    
    }else{
        End("",1);    
    }
    

      

    最终的SPJ代码就是这样:

    #include <string>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    FILE* fout, *fans, *flog;
    void End(const string& info, double x, int state = 0){
        fprintf(flog, "%.3lf
    %s
    ", x, info.c_str());
        exit(state);
    }
    void Open(){
        if (Log.size()) flog = fopen(Log.c_str(), "w"); else flog = stdout;
        if (flog == NULL) exit(1);
        if ((fans = fopen(Ans.c_str(), "r")) == NULL) exit(1);
        if ((fout = fopen(Out.c_str(), "r")) == NULL) exit(1);
    }
    int main(int argc, char* argv[]){
        string In, Out, Ans, Log;
        In = "";
        Ans = argc < 3 ? "" : argv[2];
        Out = argc < 4 ? "" : argv[3];
        Log = argc < 5 ? "" : argv[4];
        //Ans和Out变量存储的是标准答案和选手答案的路径
        Open();//初始化,这句很重要。
        double answer,output;//标准输出和选手输出
        freopen(Ans.c_str(),"r",stdin);//重定向到标准输出
        cin>>answer;//读入
        freopen(Out.c_str(),"r",stdin);//重定向到选手输出
        cin>>output;//读入
        if (abs(ans-output)>0.02){
            End("与标准答案相差过大",0);    
        }else{
            End("",1);    
        }
        return 0;
    }
    

      

    在CCR的高级配置中从下拉菜单选择这个校验器,就可以使用这个校验器测评这道SPJ题了。

  • 相关阅读:
    线性回归(Linear Regression)的理解及原理
    3个模型搞清楚用户留存分析
    机器学习简单介绍
    数据分析经典方法:5W2H分析法
    使用guava RateLimiter限流
    Maven之assembly自定义打包
    IDE自动编译
    神奇的$scope
    二分法查找
    深入理解CSS选择器优先级
  • 原文地址:https://www.cnblogs.com/abc2237512422/p/9063591.html
Copyright © 2011-2022 走看看