引言
有時我們需要使用CCR測評器(CCR-Plus是一個開源的信息學競賽測評軟件,Github鏈接https://github.com/sxyzccr/CCR-Plus)進行SpecialJudge(以下簡稱SPJ)。例如判斷選手輸出與標准輸出的差距,大於一定的值就算錯,這時就需要用SpecialJudge了。
在CCR測評器中,SPJ是用一項叫做自定義校驗器的功能實現的。CCR的文檔沒有寫明校驗器的語法,網上也沒有這一類的信息。於是,我在CCR的源代碼中找到了CCR的默認校驗器(全文比較),並將校驗器的寫法寫成此篇博客。
正文
SPJ程序的位置
編譯好的SPJ程序放在\data\prob\目錄下(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\n%s\n", 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)) != '\n';) if (c1 != -1) s1.push_back(c1); for (; !feof(fout) && (c2 = fgetc(fout)) != '\n';) 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\n%s\n", 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\n%s\n", 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題了。