ACjudge0x1

在打算法题的时候,我通常使用wsl2+vscode的组合(感觉默认cpp提示太烂了,vs过于臃肿),当我们完成一道题目时,通常要使用g++编译,然后用cv大法查看是否ac。可是,在debug时,重复的动作过于痛苦,且难以区分输入输出。因此可以使用linux命令来解决这一问题。

1
g++ {path}  && ./a.out < input.txt > output.txt && diff output.txt answer.txt -y

我们当然可以写入python脚本(其实shell脚本更简洁),并加上初始化功能(- i)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
import os
import sys
import subprocess

files_to_create = ["./input.txt", "./answer.txt"]
def print_red(text):
print(f"\033[31m{text}\033[0m")

def print_green(text):
print(f"\033[32m{text}\033[0m")

def print_yellow_bold(text):
print(f"\033[1;33m{text}\033[0m")
def judge(path):
if not os.path.exists(path):
print("File not found")
return

# Compile the C++ program
command_compile = f"g++ {path} -Wall"
try:
result_compile = subprocess.run(command_compile, shell=True, capture_output=True, text=True, check=True)
print(result_compile.stdout)
print(result_compile.stderr)
except subprocess.CalledProcessError as e:
print(f"Compilation failed with return code {e.returncode}")
print(e.stderr)
return

# Run the compiled program with input.txt and capture the output
command_run = "./a.out < input.txt > output.txt"
try:
subprocess.run(command_run, shell=True, check=True)
except subprocess.CalledProcessError as e:
print(f"Execution failed with return code {e.returncode}")
print(e.stderr)
return

# Compare the output with answer.txt
command_diff = "diff output.txt answer.txt -y "
print_yellow_bold(f"judge file: {path}")
try:
result_diff = subprocess.run(command_diff, shell=True, capture_output=True, text=True)
if result_diff.returncode == 0:
print_green("AC!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print(result_diff.stdout) # This should print all differences
else:
print_red("You should check:")
print(result_diff.stdout) # This should print all differences
except subprocess.CalledProcessError as e:
print(f"Comparison failed with return code {e.returncode}")
print(e.stderr[:500])

def init():
print("Initializing...")
for file in files_to_create:
if not os.path.exists(file):
try:
with open(file, 'w'):
print(f"File {file} created")
except FileExistsError:
print(f"File {file} exists")

if __name__ == "__main__":
current_path = os.getcwd()
os.chdir(current_path)

if len(sys.argv) > 1:
path = sys.argv[1]
else:
print("No file provided")
exit(1)

if path in ("-I", "-i", "-init"):
init()
else:
judge(path)

然后将python文件移动到系统脚本目录

1
2
chmod +x ACjudge                //赋予可执行权限
mv ./ACjudge /usr/local/bin/ACjudge

当然,脚本还有许多可以添加的功能,如创建文件时自动添加模板……如果可以的话,还可以加个python爬虫(针对特定的平台,如洛谷,codeforce),当我们创建对应的文件时,自动爬取响应的输入输出。

vscode已经有许多插件实现这一效果,这里主要针对是平时练习时插件不太好使或者使用小众比赛的情况下。