SYZOJ 题目 Subtask 配置工具

在程序运行后,需要输入测试数据文件所在的绝对目录,要求测试数据文件名称的格式形如 "1-xxxxxxx",即存在至少一个分隔符 -,且在分隔符前是区分 Subtask 的字符串,相同的内容会按相同 Subtask 对待,分隔符后是任意字符串,可以包含其他分隔符。

Code
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <dirent.h>

// Function to get all file names in a directory
std::vector<std::string> getFilesInDirectory(const std::string &dirPath) {
    std::vector<std::string> files;
    DIR *directory = opendir(dirPath.c_str());
    if (directory) {
        struct dirent *entry;
        while ((entry = readdir(directory))) {
            if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
                files.emplace_back(entry->d_name);
            }
        }
        closedir(directory);
    }
    return files;
}

int main() {
    // Prompt the user to enter the absolute path of the working directory
    std::cout << "Please enter the absolute path of the working directory: ";
    std::string work_dir;
    std::cin >> work_dir;

    // Get all files in the working directory
    std::vector<std::string> files = getFilesInDirectory(work_dir);

    // Create data structure to store subtasks using std::map
    std::map<std::string, std::set<std::string>> subtask_cases;

    // Traverse files and classify them by subtask number
    for (const std::string &filename: files) {
        if (filename.find('-') != std::string::npos) {
            std::istringstream iss(filename);
            std::string subtask_number;
            std::getline(iss, subtask_number, '-');

            std::string subtask_name = filename.substr(0, filename.find('.'));

            // Add the filename to the list of test cases for the corresponding subtask
            subtask_cases[subtask_number].insert(subtask_name);
        }
    }

    // Create the final YAML configuration file
    std::ofstream yaml_file("output.yaml");
    yaml_file << "subtasks:\n";
    for (const auto &entry: subtask_cases) {
        const std::string &subtask_number = entry.first;
        const std::vector<std::string> test_cases(entry.second.begin(), entry.second.end());

        yaml_file << "  - score: 20\n";
        yaml_file << "    type: min\n";
        yaml_file << "    cases: [";

        for (size_t i = 0; i < test_cases.size(); ++i) {
            yaml_file << "'" << test_cases[i] << "'";
            if (i < test_cases.size() - 1) {
                yaml_file << ", ";
            }
        }

        yaml_file << "]\n";
    }

    yaml_file.close();

    std::cout << "\nYAML configuration file has been generated as output.yaml" << std::endl;

    // Wait for user to press Enter (Return) key before exiting
    std::cin.ignore();
    std::cout << "\nPress Enter to exit...";
    std::cin.get(); // Wait for Enter key

    return 0;
}
posted @ 2023-09-20 10:29  User-Unauthorized  阅读(88)  评论(3编辑  收藏  举报