| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 674 人关注过本帖
标题:蒟蒻想制作一个AC自动机
只看楼主 加入收藏
pandaoxi
Rank: 1
来 自:中国河北省石家庄市
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-10-13
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
蒟蒻想制作一个AC自动机
写着玩的程序,突然就想开发一个骗过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;
}
搜索更多相关主题的帖子: string int return 输出 title 
2023-07-31 21:39
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1489
专家分:9082
注 册:2010-3-16
收藏
得分:10 
g++指定编译版本加参数-std=c++17
代码中只见new没有delete,程序运行阶段new的内存一直保留着。
2023-08-01 08:50
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:10 
而且用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;
}
2023-08-01 11:02
pandaoxi
Rank: 1
来 自:中国河北省石家庄市
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-10-13
收藏
得分:0 
谢谢各位大佬!我回去再试试
(很多OIer的码风都很奇怪,都是简洁优先

温和中坚持,宁静中创新。
2023-08-01 11:37
快速回复:蒟蒻想制作一个AC自动机
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.101761 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved