注册 登录
编程论坛 C++教室

蒟蒻想制作一个AC自动机

pandaoxi 发布于 2023-07-31 21:39, 961 次点击
写着玩的程序,突然就想开发一个骗过Lemon评测平台的程序。
目前,爆栈还是内存炸了不知道(大哭),而且用Dev C++写的,不会开C++11,具体也不会调了
希望各位大佬看看我的程序,提出一点点改进建议

蒟蒻的码风很难看,请见谅

程序代码:

// Author:PanDaoxi
#include <bits/stdc++.h>
#include <dirent.h>
using namespace std;

#define endl "\n"
#define ll long long

const int INF = 101;
int k;
bool useStdIO, errors;
string iFile, oFile, problemPath, errType, title, conStr, inp, dataFile[INF];

void getData(string path){
    DIR* d = opendir(path.c_str());
    if(!d){
        errors = true;
        errType = "Could not reach the folder";
        return;
    }
    dirent* e;
    while((e = readdir(d))){
        dataFile[++k] = e -> d_name;
        if(dataFile[k] == "." || dataFile[k] == "..") k--;
    }
    closedir(d);
    return;
}

string getInput(string path){
    FILE* cur = fopen(path.c_str(), "r");
    if(!cur){
        errors = true;
        return errType = "Could not read from the INPUT file";
    }
    fseek(cur, 0, SEEK_END);
    size_t sz = ftell(cur);
    char* tmp = new char[sz];
    rewind(cur);
    fread(tmp, sizeof(char), sz, cur);
    fclose(cur);
    return string(tmp);
}

void checkErr(){
    if(errors){
        cerr << errType;
        exit(0);
    }
    return;
}

int main(){
    // 请用户填写如下题目信息
    useStdIO = true;                // 是否使用标准输入输出(true=使用标准输出,false=使用文件输出)
    title = "T1";                   // 当前题目名称
    iFile = title + ".in";            // 题目要求的输入数据文件(仅当 useStdIO 为真时有效)
    oFile = title + ".out";            // 题目要求的输出数据文件(仅当 useStdIO 为真时有效)
   
   
// 请用户不要操作以下内容
    problemPath = "../../data/" + title + "/";
    if(useStdIO){
        oFile = "con";
        while(getline(cin, conStr)) inp += conStr + "\n";
    }
    else{
        freopen(iFile.c_str(), "r", stdin);
        inp = getInput(iFile);
    }
    freopen(oFile.c_str(), "w", stdout);
    checkErr();
    getData(problemPath);
    checkErr();
    for(int i=1; i<=k; i++){
        if(dataFile[i].find("in") != dataFile[i].npos) continue;
        else if(getInput(problemPath + dataFile[i]) == inp){
            string ans = getInput(problemPath + dataFile[i].substr(0, dataFile[i].find(".")) + oFile.substr(oFile.find(".")));
            checkErr();
            cout << ans;
            fclose(stdout);
            return 0;
        }
    }
    errors = true;
    errType = "Could not find the answer";
    checkErr();
    fclose(stdout);
   
    return 0;
}
3 回复
#2
apull2023-08-01 08:50
g++指定编译版本加参数-std=c++17
代码中只见new没有delete,程序运行阶段new的内存一直保留着。
#3
rjsp2023-08-01 11:02
而且用Dev C++写的,不会开C++11
那就别用 DevCpp 这种过时老古董呗


另外,你的代码像天书,各种局部变量都混杂成全局函数。怎样写代码,我给你做个示范(仅仅只是示范,我不知道正确的流程)
程序代码:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <filesystem>
using namespace std;

int main( void )
{
    const bool use_stdio = true;
    const std::string title = "T1";

    auto get_istream_data = []( std::istream& is ) {
        return std::string( std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>() );
    };

    std::ifstream in_( title+".in" );
    std::ofstream out_( title+".out" );
    std::istream& fin = use_stdio ? std::cin : in_;
    std::ostream& fout = use_stdio ? std::cout : out_;
    std::string inp = get_istream_data( fin );

    for( auto& fe : std::filesystem::directory_iterator("../../data/"+title+"/") )
    {
        if( fe.is_regular_file() && fe.path().extension()=="in" ) // 我怀疑你代码写错了
        {
            if( std::ifstream f(fe.path()); get_istream_data(f)==inp )
            {
                std::ofstream ans( std::filesystem::path(fe.path()).replace_extension("out") );
                fout << ans.rdbuf();
                break;
            }
        }
    }

    return 0;
}
#4
pandaoxi2023-08-01 11:37
谢谢各位大佬!我回去再试试
(很多OIer的码风都很奇怪,都是简洁优先
1