【教程】C++ 造数据一条龙教程Ⅱ

· · 科技·工程

【教程】C++ 造数据一条龙教程Ⅰ

6 造数据的应用

6.1 对拍

对拍是造数据最重要的应用场景。一个对拍系统包含四个部分:

  1. 数据生成器 gen.cpp:接受命令行^{[2]}参数(种子、数据规模等),输出测试数据。
  2. 标准程序 std.cpp:已知正确的解法(通常是暴力或已验证的算法)。
  3. 测试程序 test.cpp:待验证的解法。
  4. 对拍脚本 tester.bat^{[3]} / tester.cpp:循环执行生成→运行→比对的流程。

以 P1001 A+B Problem 为例,给出完整代码:

:::success[gen.cpp]

#include <bits/stdc++.h>
using namespace std;
mt19937 gen;
long long rand_ll(long long l, long long r) {
    return uniform_int_distribution<long long>(l, r)(gen);
}

// 编译后命令行输入 gen.exe <种子> <a,b 下界> <a,b 上界> 以运行
int main(int argc, char* argv[]) {
    unsigned t = atoi(argv[1]);
    gen.seed(t * 114514 + 7891); // 根据第一个参数设定生成器种子

    int l = atoi(argv[2]), r = atoi(argv[3]);
    cout << rand_ll(l, r) << " " << rand_ll(l, r) << "\n";
    return 0;
}

:::

:::success[std.cpp]

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b;
    return 0;
}

:::

:::success[test.cpp]

#include <bits/stdc++.h>
using namespace std;

int main() {
    unsigned a, b; // 故意写成 unsigned 让对拍时出错
    cin >> a >> b;
    cout << a + b;
    return 0;
}

:::

:::success[tester.bat(务必以 ANSI 格式保存,否则中文乱码)]

@echo off
:: 关闭命令回显
chcp 936 >nul
:: 设置活动代码页以防乱码(>nul 隐藏命令输出)

set cnt=0
set max_cnt=20
:: max_cnt:对拍次数

:loop
if %cnt% equ %max_cnt% (
    pause
    goto :eof
)

set /a cnt+=1
gen.exe %cnt% -1000000000 1000000000 >data.in
:: 运行数据生成器,输出数据到 data.in
std.exe <data.in >std.out
:: 运行标准程序,从 data.in 读入,输出到 std.out
test.exe <data.in >test.out
:: 运行测试程序,从 test.in 读入,输出到 test.out
:: 若路径有特殊符号(空格等),必须用 "" 包裹

fc /w std.out test.out >nul 2>&1
:: 比较两个输出(2>&1 隐藏命令错误信息)
:: /w 过滤多余空白字符进行比较(但是这不完全等同于 OJ 的判题规则,有需要可自写比较逻辑)
if %errorlevel% equ 1 (
    echo 第 %cnt% 组测试 WA!
    pause
    goto :eof
)
:: 两个输出不一致,中止对拍(出错的数据可在文件中查看)

echo 第 %cnt% 组测试 AC!
goto loop

:::

:::success[tester.cpp]

#include <bits/stdc++.h>
using namespace std;

int main() {
    int max_cnt = 20;
    for(int i = 1; i <= max_cnt; i++) {
        system(("gen.exe " + to_string(i) + " -1000000000 1000000000 >data.in").c_str()); // 注意,system 传入的是 C 风格字符串,string 需转化
        system("std.exe <data.in >std.out");
        system("test.exe <data.in >test.out");

        int code = system("fc /w std.out test.out >nul 2>&1");
        if(code == 1) {
            cout << "第 " << i << " 组测试 WA!" << "\n";
            return 0;
        }
        cout << "第" << i << " 组测试 AC!" << "\n";
    }
    return 0;
}

:::

对拍流程:

  1. 编译 gen.cpp、std.cpp、test.cpp。
  2. 在 tester 中设置好传入 gen.exe 的参数。
  3. 运行 tester。

:::info[关于 Special Judge 与 checker]{open}

如果一道题有 Special Judge,那么将 tester 执行文件比较的部分换为执行 checker 即可。\ 注意传入 checker 的参数要适配(通常是 checker.exe <输入文件> <输出文件> <答案文件>)。

:::

6.2 出题造数据

一道题目的测试数据应当是具有区分度的,能够卡掉不正确的解法。一个出题造数据系统包含三个部分:

  1. 数据生成器 gen.cpp:接受命令行参数(测试点编号等),输出测试数据。
  2. 标准程序 std.cpp:题目的整解。
  3. 启动器 run.bat / run.cpp:循环执行生成数据。

以 P1001 A+B Problem 为例,给出完整代码:

:::success[gen.cpp]

#include <bits/stdc++.h>
using namespace std;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
long long rand_ll(long long l, long long r) {
    return uniform_int_distribution<long long>(l, r)(gen);
}

// 编译后命令行输入 gen.exe <测试点编号> 以运行
int main(int argc, char* argv[]) {
    int id = atoi(argv[1]);

    if(id >= 1 && id <= 2) // 较小的正数据
        cout << rand_ll(1, 1e4) << " " << rand_ll(1, 1e4) << "\n";
    else if(id >= 3 && id <= 4) // 较小的数据
        cout << rand_ll(-1e4, 1e4) << " " << rand_ll(-1e4, 1e4) << "\n";
    else if(id >= 5 && id <= 6) // 较大的正数据
        cout << rand_ll(1e6, 1e9) << " " << rand_ll(1e6, 1e9) << "\n";
    else if(id >= 7 && id <= 9) // 随机数据
        cout << rand_ll(-1e9, 1e9) << " " << rand_ll(-1e9, 1e9) << "\n";
    else if(id == 10) // 极大边界数据
        cout << "1000000000 1000000000" << "\n";
    return 0;
}

:::

std.cpp 与对拍中的相同,这里不重复放了。

:::success[run.bat(务必以 ANSI 格式保存,否则中文乱码)]

@echo off
chcp 936 >nul

set cnt=0
set max_cnt=10

:loop
if %cnt% equ %max_cnt% (
    pause
    goto :eof
)

set /a cnt+=1
gen.exe %cnt% >sum%cnt%.in
std.exe <sum%cnt%.in >sum%cnt%.out

echo 测试点 #%cnt% 已生成!
goto loop

:::

:::success[run.cpp]

#include <bits/stdc++.h>
using namespace std;

int main() {
    const int max_cnt = 10;
    for (int i = 1; i <= max_cnt; i++) {
        string in = "sum" + to_string(i) + ".in";
        string out = "sum" + to_string(i) + ".out";

        system(("gen.exe " + to_string(i) + " >" + in).c_str());
        system(("std.exe <" + in + " >" + out).c_str());

        cout << "测试点 #" << i << " 已生成!" << "\n";
    }
    return 0;
}

:::

执行流程:

  1. 编译 gen.cpp、std.cpp。
  2. 在 run 中设置好传入 gen.exe 的参数。
  3. 运行 run。

6.3 (拓展)测试大样例

其实讲这个有点偏题了,但既然对拍和出题都讲了,那就顺便讲了。

假设你的文件结构为:

sum/
├── tester.bat 或 tester.cpp
├── sum.cpp
└── data/
    ├── sum1.in
    ├── sum1.ans
    ├── sum2.in
    ├── sum2.ans
    ├── sum3.in
    └── sum3.ans

以 P1001 A+B Problem 为例,类似于对拍,tester 的完整代码如下:

:::success[tester.bat(务必以 ANSI 格式保存,否则中文乱码)]

@echo off
chcp 936 >nul

set cnt=0
:loop
set /a cnt+=1
if not exist data\sum%cnt%.in (
    pause
    goto :eof
)
::自动检查文件存在性

sum.exe <data\sum%cnt%.in >data\sum%cnt%.out
fc /w data\sum%cnt%.out data\sum%cnt%.ans >nul 2>&1

if %errorlevel% equ 0 (
    echo 第 %cnt% 组数据 AC!
) else (
    echo 第 %cnt% 组数据 WA!
)
goto loop

:::

:::success[tester.cpp]

#include <bits/stdc++.h>
using namespace std;

int main() {
    for (int i = 1; ; i++) {
        string in = "data/sum" + to_string(i) + ".in";
        string out = "data/sum" + to_string(i) + ".out";
        string ans = "data/sum" + to_string(i) + ".ans";

        ifstream test(in);
        if (!test) break;
        test.close();

        system(("sum.exe <" + in + " >" + out).c_str());
        int code = system(("fc /w " + out + " " + ans).c_str());
        if (code == 1)
            cout << "第 " << i << "组数据 AC!" << "\n";
        else
            cout << "第 " << i << "组数据 WA!" << "\n";
    }
    return 0;
}

:::

测试流程:

  1. 编译 sum.cpp(不要文件读写)。
  2. 运行 tester。

关于 Special Judge 和 checker 的问题同 6.1。

7 Special Judge 与 checker

8 交互题与交互库

9 (拓展)题目测试点配置文件和自定义计分脚本

Ex 注释