fc | diff
师承 Gon_Tata。
2025/10/29 16:00: 现在兼容 linux 文件输入输出了!
2025/10/29 17:30: 现在兼容 Windows 文件输入输出了!
2025/10/29 19:00: 现在兼容 linux 标准输入输出了!
2025/10/29 20:30: 现在兼容 Windows 标准输入输出了!
#include<bits/stdc++.h>
using namespace std;
const long long CHECK_TIMES=1e3; // 对拍次数
const long long TIME_LIMIT=1000; // 时限 (ms)
const long long STACK_MEMORY=256; //栈空间 (MB)
string data_outputfile= // 用来存放数据的文件名,对拍结果不同的那组数据
"in";
string yours_code_cpp= // 你的代码的文件名
"a.cpp";
string std_code_cpp= // std 的代码的文件名
"b.cpp";
string data_maker_cpp= // 造数据的代码的文件名
"rand.cpp";
string your_outputfile_out= // 你的代码的输出文件名
"a.out";
string std_outputfile_out= // std 的输出文件名
"b.out";
vector<string> opt={ // 自定义的编译选项
"-std=c++14 ",
// "-Wall", // 显示最多警告信息
"-w", // 不显示任何警告
"-O2", // O2 优化
"-lm"
};
/*
* you should modify this part to adapt your code
* ----------------------------------------
*/
void check(const string &a);
void surprise();
signed main()
{
cout<<"Do you use freopen? If the answer is yes, please input 1 , else input 2: "<<endl;
string a;
for(int i=1;i<=15;i++)
cin>>a,check(a);
surprise();
return 0;
}
/* ---------------------------------------------------------- */
#include<ctime>
#if INT_MAX==RAND_MAX
#include <unistd.h>
#define Sleep(x) usleep(x*1000)
#else
#include <windows.h>
#endif
string data_exe="____DATA.exe";
string your_exe="____YOURS.exe";
string std_exe="____STD.exe";
void diff_with_files_out();
void diff_with_stdout();
void do_diff_with_stdout_linux(bool f);
void do_diff_with_stdout_windows(bool f);
#define del_file(x) " rm "+x+" "
#define make_exe(x,y) " g++ "+x+" "+options+" "+y+" "+Win_sta+" "
#define data_ger(file) (f?"./":"")+file+" > "+data_outputfile+" "
#define code_exe(file,out) (f?"./":"")+file+" < "+data_outputfile+" > "+out
void check(const string &a)
{
cout<<"Checking input... "<<endl;
if(a.size()==1)
{
if(a=="1") diff_with_files_out(),exit(0);
else if(a=="2") diff_with_stdout(),exit(0);
else cout<<"The input is illegal! You have no egg!!!\n";
}
else cout<<"The input is illegal! You have no egg!!!\n";
}
void diff_with_files_out()
{
cout<<"The input is legal!"<<endl;
cout<<"=============================================== "<<endl;
Sleep(300);
cout<<"initializing..."<<endl;
string Win_sta=" ";
int awa=1;
bool f=RAND_MAX==INT_MAX;
if(f) awa=system(("ulimit -s "+to_string(STACK_MEMORY*1024)).c_str());
else Win_sta+="-Wl,--stack="+(to_string(STACK_MEMORY*1024*1024));
assert(awa+1);
string your_code_operation="g++ "+yours_code_cpp;
string std_code_operation="g++ "+std_code_cpp;
string data_maker_operation="g++ "+data_maker_cpp;
for(const string &op:opt)
{
your_code_operation+=" "+op;
std_code_operation+=" "+op;
data_maker_operation+=" "+op;
}
your_code_operation+=" -o "+your_exe+" "+Win_sta;
std_code_operation+=" -o "+std_exe+" "+Win_sta;
data_maker_operation+=" -o "+data_exe+" "+Win_sta;
string comparsion_operation;
if(f) comparsion_operation="diff -Z "+your_outputfile_out+" "+std_outputfile_out+" ";
else comparsion_operation="fc "+your_outputfile_out+" "+std_outputfile_out+" ";
if(f)
{
data_exe="./"+data_exe;
your_exe="./"+your_exe;
std_exe="./"+std_exe;
}
data_exe+=" > "+data_outputfile+" ";
your_exe+=" < "+data_outputfile+" > "+your_outputfile_out+" ";
std_exe+=" < "+data_outputfile+" > "+std_outputfile_out+" ";
awa=system(your_code_operation.c_str());
awa=system(std_code_operation.c_str());
awa=system(data_maker_operation.c_str());
cout<<"initializing Succeeded!!!"<<endl;
cout<<"=============================================== "<<endl;
Sleep(200);
cout<<"Running..."<<endl;
Sleep(200);
for(signed i=1;i<=CHECK_TIMES;i++)
{
printf("Running on test #%d,",i);
if(i<10) printf(" ");
else if(i<100) printf(" ");
else if(i<1000) printf(" ");
else if(i<10000) printf(" ");
// fflush(stdout);
awa=system(data_exe.c_str());
std::chrono::milliseconds mbs = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
long long bgt=mbs.count();
awa=system(your_exe.c_str());
std::chrono::milliseconds mes = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
long long edt=mes.count();
long long dt=edt-bgt;
printf("time=%lldms",dt);
printf(", ");
if(dt<10) printf(" ");
else if(dt<100) printf(" ");
else if(dt<1000) printf(" ");
if(!f) putchar('\n');
// fflush(stdout);
awa=system(std_exe.c_str());
bool flag=0;
if(dt>TIME_LIMIT) assert(!system("echo 'Time Limit Error !!!'")),flag=1;
if(system(comparsion_operation.c_str())) { printf("Wrong Answer! \n"); flag=1; }
else if(f) printf("Accepted");
if(flag) exit(0);
if(f)cout<<'\n';
else puts("Accepted\n-----------------------------------------------\n");
fflush(stdout);
}
}
void surprise()
{
for(int i=1;i<=10000;i++)
cout<<"You have no egg!!!\n",Sleep(50);
}
void diff_with_stdout()
{
cout<<"The input is legal!"<<endl;
cout<<"=============================================== "<<endl;
Sleep(300);
cout<<"initializing..."<<endl;
bool f=RAND_MAX==INT_MAX;
if(f) do_diff_with_stdout_linux(f);
else do_diff_with_stdout_windows(f);
}
void do_diff_with_stdout_linux(bool f)
{
string Win_sta=" ";
int awa=1;
if(f) awa=system(("ulimit -s "+to_string(STACK_MEMORY*1024)).c_str());
else Win_sta+="-Wl,--stack="+(to_string(STACK_MEMORY*1024*1024));
assert(awa+1);
int cnt=0;
string your_code_operation="g++ "+yours_code_cpp;
string std_code_operation="g++ "+std_code_cpp;
string data_maker_operation="g++ "+data_maker_cpp;
for(const string &op:opt)
{
your_code_operation+=" "+op;
std_code_operation+=" "+op;
data_maker_operation+=" "+op;
}
your_code_operation+=" -o "+your_exe+" "+Win_sta;
std_code_operation+=" -o "+std_exe+" "+Win_sta;
data_maker_operation+=" -o "+data_exe+" "+Win_sta;
string e_rand,e_a,e_b;
e_rand=data_ger(data_exe);
e_a=code_exe(your_exe,your_outputfile_out);
e_b=code_exe(std_exe,std_outputfile_out);
string comparsion_operation;
if(f) comparsion_operation="diff -Z "+your_outputfile_out+" "+std_outputfile_out+" ";
else comparsion_operation="fc "+your_outputfile_out+" "+std_outputfile_out+" ";
awa=system(your_code_operation.c_str());
awa=system(std_code_operation.c_str());
awa=system(data_maker_operation.c_str());
cout<<"initializing Succeeded!!!"<<endl;
cout<<"=============================================== "<<endl;
Sleep(200);
cout<<"Running..."<<endl;
Sleep(200);
for(int kkk=1;kkk<=CHECK_TIMES;kkk++)
{
printf("#%d------->", ++cnt);
assert(!system(e_rand.c_str()));
std::chrono::milliseconds mbs = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
long long bgt=mbs.count();
assert(!system(e_a.c_str()));
std::chrono::milliseconds mes = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
long long edt=mes.count();
printf("time: %lld ",edt-bgt);
if(edt-bgt>TIME_LIMIT) assert(!system("echo Time Limit Error")), exit(0);
assert(!system(e_b.c_str()));
if(system(comparsion_operation.c_str())) assert(!system("echo Wrong Answer")), exit(0);
puts("Accepted");
}
}
void do_diff_with_stdout_windows(bool f)
{
string Win_sta=" ";
int awa=1;
if(f) awa=system(("ulimit -s "+to_string(STACK_MEMORY*1024)).c_str());
else Win_sta+="-Wl,--stack="+(to_string(STACK_MEMORY*1024*1024));
assert(awa+1);
int cnt=0;
string your_code_operation="g++ "+yours_code_cpp;
string std_code_operation="g++ "+std_code_cpp;
string data_maker_operation="g++ "+data_maker_cpp;
for(const string &op:opt)
{
your_code_operation+=" "+op;
std_code_operation+=" "+op;
data_maker_operation+=" "+op;
}
your_code_operation+=" -o "+your_exe+" "+Win_sta;
std_code_operation+=" -o "+std_exe+" "+Win_sta;
data_maker_operation+=" -o "+data_exe+" "+Win_sta;
string comparsion_operation;
if(f) comparsion_operation="diff -Z "+your_outputfile_out+" "+std_outputfile_out+" ";
else comparsion_operation="fc "+your_outputfile_out+" "+std_outputfile_out+" ";
awa=system(your_code_operation.c_str());
awa=system(std_code_operation.c_str());
awa=system(data_maker_operation.c_str());
string e_rand,e_a,e_b;
e_rand=data_ger(data_exe);
e_a=code_exe(your_exe,your_outputfile_out);
e_b=code_exe(std_exe,std_outputfile_out);
cout<<"initializing Succeeded!!!"<<endl;
cout<<"=============================================== "<<endl;
Sleep(200);
cout<<"Running..."<<endl;
Sleep(200);
// return;
for(int kkk=1;kkk<=CHECK_TIMES;kkk++)
{
printf("#%d------->", ++cnt);
assert(!system(e_rand.c_str()));
std::chrono::milliseconds mbs = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
long long bgt=mbs.count();
assert(!system(e_a.c_str()));
std::chrono::milliseconds mes = std::chrono::duration_cast< std::chrono::milliseconds >(std::chrono::system_clock::now().time_since_epoch());
long long edt=mes.count();
printf("time: %lld \n",edt-bgt);
if(edt-bgt>TIME_LIMIT) assert(!system("echo Time Limit Error")), exit(0);
assert(!system(e_b.c_str()));
if(system(comparsion_operation.c_str())) assert(!system("echo Wrong Answer")), exit(0);
puts("Accepted\n-----------------------------------------------\n");
}
}
简易版本:
#include<bits/stdc++.h>
using namespace std;
const int N=1e4;
// file name
string yours_code=
"T2.cpp";
string std_code=
"T2baoli.cpp";
string data_maker=
"data.cpp";
string yours_file=
"putin.out";
string std_file=
"baoli.out";
signed main()
{
// #ifndef ONLINE_JUDGE
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
// #endif
cout<<"Initlizing..."<<endl;
int awa=system("ulimit -s 262100");
assert(awa+1);
string s1="g++ "+yours_code+" -std=c++14 -O2 -lm -o YOURS";
string s2="g++ "+std_code+" -std=c++14 -O2 -lm -o STD";
string s3="g++ "+data_maker+" -std=c++14 -O2 -lm -o DATA";
string s4="diff -Z "+yours_file+" "+std_file;
char c[5][256]={};
for(int i=0;i<(int)s1.size();i++) c[1][i]=s1[i];
for(int i=0;i<(int)s2.size();i++) c[2][i]=s2[i];
for(int i=0;i<(int)s3.size();i++) c[3][i]=s3[i];
for(int i=0;i<(int)s4.size();i++) c[4][i]=s4[i];
awa=system(c[1]);
awa=system(c[2]);
awa=system(c[3]);
cout<<"Succeed!"<<endl;
for(int i=1;i<=N;i++)
{
cout<<"Running on test #"<<i<<": ";
fflush(stdout);
awa=system("./DATA");
awa=system("./YOURS");
awa=system("./STD");
if(system(c[4])) {cout<<"Wrong Answer! "<<endl; break;}
else cout<<"Accepted"<<endl;
}
return 0;
}
以下是博客签名,正文无关
本文来自博客园,作者:Wy_x,转载请在文首注明原文链接:https://www.cnblogs.com/Wy-x/p/19174806
版权声明:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议(CC-BY-NC-SA 4.0 协议)进行许可。

浙公网安备 33010602011771号