浅谈online judge平台 spj [special judge] 使用 | 修改问题(下)

简介: 第六种:交互题的spj第七种[带有testlib.h]的另一种解决方式第八种 使用validation.h的BAPC2018(较难)

第六种:交互题的spj


本平台暂不支持交互题,所以题库里的交互题目前没有进行处理通过

可以参考 洛谷的交互题spj 对应写法


第七种[带有testlib.h]的另一种解决方式


…遇见后后续更新

2021-10-21更新

将以下文件入口对应

inf->标准输入 argv[1]

ouf->用户输出 argv[3]

ans->答案结果 argv[2]


void registerTestlibCmd(int argc, char* argv[]) {
  __testlib_ensuresPreconditions();
  testlibMode = _checker;
  __testlib_set_binary(stdin);
  if (argc > 1 && !strcmp("--help", argv[1]))
    __testlib_help();
  // if (argc < 4 || argc > 6)
  // {
  //     quit(_fail, std::string("Program must be run with the following arguments: ") +
  //         std::string("<input-file> <output-file> <answer-file> [<report-file> [<-appes>]]") +
  //         "\nUse \"--help\" to get help information");
  // }
  appesMode = false;
//  if (argc == 3) {///改 
//    resultName = "";
//    appesMode = false;
//  }
//
//  if (argc == 4) {
//    resultName = make_new_file_in_a_dir(argv[3]);
//    appesMode = false;
//  }///改 
  // if (argc == 6)
  // {
  //     if (strcmp("-APPES", argv[5]) && strcmp("-appes", argv[5]))
  //     {
  //         quit(_fail, std::string("Program must be run with the following arguments: ") +
  //                     "<input-file> <output-file> <answer-file> [<report-file> [<-appes>]]");
  //     }
  //     else
  //     {
  //         resultName = argv[4];
  //         appesMode = true;
  //     }
  // }
  inf.init(argv[1], _input);
  ouf.init(argv[3], _output);
  ans.init(argv[2], _answer);/// 改 
}
void registerTestlib(int argc, ...) {
  if (argc  < 3 || argc > 5)
    quit(_fail, std::string("Program must be run with the following arguments: ") +
         "<input-file> <output-file> <answer-file> [<report-file> [<-appes>]]");
  char** argv = new char*[argc + 1];
  va_list ap;
  va_start(ap, argc);
  argv[0] = NULL;
  for (int i = 0; i < argc; i++) {
    argv[i + 1] = va_arg(ap, char*);
  }
  va_end(ap);
  registerTestlibCmd(argc + 1, argv);
  delete[] argv;
}


第八种 使用validation.h的BAPC2018(较难)


首先站是原始的BAPC后台validation.h文件:

// A header library to safely parse team input.
// It does not support floating points or big integers.
// The easiest way to use this is to symlink it from a validator directory,
// so that it will be picked up when creating a contest zip.
// The default checking behaviour is lenient for both white space and case.
// When validating .in and .ans files, the case_sensitve and space_change_sensitive flags should be
// passed. When validating team output, the flags in problem.yaml should be used.
#include <algorithm>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <limits>
using namespace std;
const string case_sensitive_flag         = "case_sensitive";
const string space_change_sensitive_flag = "space_change_sensitive";
class Validator {
  const int ret_AC = 42, ret_WA = 43;
  bool case_sensitive;
  bool ws;
  public:
  Validator(int argc, char **argv, istream &in = std::cin) : in(in) {
    for(int i = 0; i < argc; ++i) {
      if(argv[i] == case_sensitive_flag) case_sensitive = true;
      if(argv[i] == space_change_sensitive_flag) ws = true;
    }
    if(ws) in >> noskipws;
  }
  // No copying, no moving.
  Validator(const Validator &) = delete;
  Validator(Validator &&)      = delete;
  // At the end of the scope, check whether the EOF has been reached.
  // If so, return AC. Otherwise, return WA.
  ~Validator() {
    eof();
    AC();
  }
  void space() {
    if(ws) {
      char c;
      in >> c;
      if(c != ' ') expected("space", string("\"") + c + "\"");
    }
    // cerr << "read space!\n";
  }
  void newline() {
    if(ws) {
      char c;
      in >> c;
      if(c != '\n') expected("newline", string("\"") + c + "\"");
    }
    // cerr << "read newline!\n";
  }
  // Just read a string.
  string read_string() { return read_string_impl(); }
  // Read a string and make sure it equals `expected`.
  string read_string(string expected) { return read_string_impl(expected); }
  // Read an arbitrary string of a given length.
  string read_string(size_t min, size_t max) {
    string s = read_string();
    if(s.size() < min || s.size() > max)
      expected("String of length between " + to_string(min) + " and " + to_string(max), s);
    return s;
  }
  // Read the string t.
  void test_string(string t) {
    string s = read_string();
    if(case_sensitive) {
      if(s != t) expected(t, s);
    } else {
      if(lowercase(s) != lowercase(t)) expected(t, s);
    }
  }
  // Read a long long.
  long long read_long_long() {
    string s = read_string_impl("", "integer");
    long long v;
    try {
      size_t chars_processed = 0;
      v                      = stoll(s, &chars_processed);
      if(chars_processed != s.size())
        WA("Parsing " + s + " as long long failed! Did not process all characters");
    } catch(const out_of_range &e) {
      WA("Number " + s + " does not fit in a long long!");
    } catch(const invalid_argument &e) { WA("Parsing " + s + " as long long failed!"); }
    return v;
  }
  // Read a long long within a given range.
  long long read_long_long(long long low, long long high) {
    auto v = read_long_long();
    if(low <= v && v <= high) return v;
    expected("integer between " + to_string(low) + " and " + to_string(high), to_string(v));
  }
  int read_int() {
    return read_long_long(std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
  }
  int read_int(int low, int high) {
    int v = read_long_long(std::numeric_limits<int>::min(), std::numeric_limits<int>::max());
    if(low <= v && v <= high) return v;
    expected("integer between " + to_string(low) + " and " + to_string(high), to_string(v));
  }
  // Read a long double.
  long double read_long_double() {
    string s = read_string_impl("", "integer");
    long double v;
    try {
      size_t chars_processed;
      v = stold(s, &chars_processed);
      if(chars_processed != s.size())
        WA("Parsing ", s, " as long double failed! Did not process all characters.");
    } catch(const out_of_range &e) {
      WA("Number " + s + " does not fit in a long double!");
    } catch(const invalid_argument &e) { WA("Parsing " + s + " as long double failed!"); }
    return v;
  }
  // Check the next character.
  bool peek(char c) {
    if(!ws) in >> ::ws;
    return in.peek() == char_traits<char>::to_int_type(c);
  }
  // Return WRONG ANSWER verdict.
  [[noreturn]] void expected(string exp = "", string s = "") {
    if(s.size())
      cout << "Expected " << exp << ", found " << s << endl;
    else if(exp.size())
      cout << exp << endl;
    exit(ret_WA);
  }
  template <typename T>
  [[noreturn]] void WA(T t) {
    cout << t << endl;
    exit(ret_WA);
  }
  template <typename T, typename... Ts>
  [[noreturn]] void WA(T t, Ts... ts) {
    cout << t;
    WA(ts...);
  }
  template <typename... Ts>
  void assert(bool b, Ts... ts) {
    if(!b) WA(ts...);
  }
  private:
  // Read an arbitrary string.
  // expected: if not "", string must equal this.
  // wanted: on failure, print "expected <wanted>, got ..."
  string read_string_impl(string expected_string = "", string wanted = "string") {
    if(ws) {
      char next = in.peek();
      if(isspace(next)) expected(wanted, "whitespace");
    }
    string s;
    if(in >> s) {
      if(!case_sensitive) {
        s               = lowercase(s);
        expected_string = lowercase(expected_string);
      }
      if(!expected_string.empty() && s != expected_string)
        WA("Expected string \"expected\", but found ", s);
      return s;
    }
    expected(wanted, "nothing");
  }
  // Return ACCEPTED verdict.
  [[noreturn]] void AC() { exit(ret_AC); }
  void eof() {
    if(in.eof()) return;
    // Sometimes EOF hasn't been triggered yet.
    if(!ws) in >> ::ws;
    char c = in.get();
    if(c == char_traits<char>::eof()) return;
    expected("EOF", string("\"") + char(c) + "\"");
  }
  // Convert a string to lowercase is matching is not case sensitive.
  string &lowercase(string &s) {
    if(!case_sensitive) return s;
    transform(s.begin(), s.end(), s.begin(), ::tolower);
    return s;
  }
  istream &in;
};


我们尤其需要注意公有成员方法:

Validator(int argc, char **argv, istream &in = std::cin) : in(in) {
    for(int i = 0; i < argc; ++i) {
      if(argv[i] == case_sensitive_flag) case_sensitive = true;
      if(argv[i] == space_change_sensitive_flag) ws = true;
    }
    if(ws) in >> noskipws;
  }


main中的前几行代码:

// Set up the input and answer streams.
  std::ifstream in(argv[1]);
  std::ifstream ans(argv[2]);


因为本平台都是使用文件的形式进行判断所提交的代码,这里可以用流的形式进行操作,将42、43改成对应的0、1然后将main中添加如下代码:

// Set up the input and answer streams.
  std::ifstream in(argv[1]);
  std::ifstream ans(argv[2]);
  std::ifstream user(argv[3]);
  Validator out(argc, argv, user);
  // 以下代码为输入


然后将公有成员构造方法改成:

Validator(int argc, char **argv, std::ifstream &in) : in(in) {
  for(int i = 0; i < argc; ++i) {
    if(argv[i] == case_sensitive_flag) case_sensitive = true;
    if(argv[i] == space_change_sensitive_flag) ws = true;
  }
  if(ws) in >> noskipws;
}


即可完美解决

目录
相关文章
|
JSON Shell API
ThinkPHP5使用Swagger-php接口文档
ThinkPHP5使用Swagger-php接口文档
430 0
|
6月前
|
虚拟化 iOS开发 MacOS
VMware ESXi 8.0U3e macOS Unlocker & OEM BIOS 集成驱动版,新增 12 款 I219 网卡驱动
VMware ESXi 8.0U3e macOS Unlocker & OEM BIOS 集成驱动版,新增 12 款 I219 网卡驱动
309 15
|
存储 弹性计算 安全
阿里云第七代云服务器ECS性能、适用场景与价格参考
阿里云第七代云服务器ECS(Elastic Compute Service)作为阿里云最新一代的高性能计算产品,凭借其基于最新硬件架构和虚拟化技术的全面升级,在计算能力、存储性能、网络传输速度以及灵活性等多个方面实现了显著提升。这一代云服务器旨在为用户提供更为强大、稳定且可定制的云端基础设施服务,广泛适用于从基础的Web托管到复杂的高性能计算等多种应用场景。
|
JavaScript 前端开发
BootStrap在Vue中的安装使用详细教程
这篇文章提供了在Vue项目中安装和使用Bootstrap的详细教程,包括安装jQuery、引入Bootstrap、配置Webpack以及在项目中进行测试和查看效果的步骤。
BootStrap在Vue中的安装使用详细教程
|
XML 搜索推荐 API
通义千问API:让大模型使用各种工具
本章我们将通过一个简单的例子,揭示基于LangChain的Agent开发的秘密,从而了解如何扩展大模型的能力。
通义千问API:让大模型使用各种工具
|
弹性计算 JavaScript Ubuntu
ECS 挂载 OSS 多Bucket
ECS 挂载 OSS 多Bucket
221 0
|
存储 安全 编译器
【C++ 17 泛型容器对比】C++ 深度解析:std::any 与 std::variant 的细微差别
【C++ 17 泛型容器对比】C++ 深度解析:std::any 与 std::variant 的细微差别
880 1
|
存储 对象存储
【阿里云OSS】You have no right to access this object because of bucket acl.
【阿里云OSS】You have no right to access this object because of bucket acl.
18258 1
【阿里云OSS】You have no right to access this object because of bucket acl.
|
数据采集 弹性计算 供应链
阿里云ECS付费类型:包年包月、按量付费和抢占式实例区别详解
阿里云服务器付费模式:包年包月适合长期稳定服务,价格优惠;按量付费适合短期或波动需求,按小时计费;抢占式实例价格低但可能被系统释放,适合无状态应用。选择取决于业务场景和资源稳定性需求。
381 0
|
数据挖掘 数据处理 索引
如何使用Python的Pandas库进行数据筛选和过滤?
Pandas是Python数据分析的核心库,提供DataFrame数据结构。基本步骤包括导入库、创建DataFrame及进行数据筛选。示例代码展示了如何通过布尔索引、`query()`和`loc[]`方法筛选`Age`大于19的记录。
305 0