算法题解统一使用现代 C++ / Python 3 作为默认使用的编程语言,基本原则如下:
- 语法风格:Google C++ Style Guide / Google Python Style Guide
- 相关工具:cpplint / pylint / clang format / autopep8 (部分规则有修改)
- 在复杂度不存在巨大差异的情况下,算法实现的可读性(思路)优于极致的效率追求;
- 不使用竞赛中常见的宏定义和变量命名风格(手速狗慎用此原则);
- 不排斥使用 bits/stdc++.h 这样的头文件,不排斥使用 using namespace std;
- 不排斥使用标准库(STL)中的常见接口,不认识的请查文档如 cppreference, 当作学习语法;
- 默认一开始不做卡常数级别优化,一些题会给出无法 AC 的情况并解释,过早优化是万恶之源;
- 默认不禁用 C++ 标准流同步,不做 I/O 层面的优化(除非数据量巨大)。
想要快速跳转 OJ 题解,可在地址栏按此模式输入 URL(将 xxx 替换成题目编号):
Online Judge 模版与命令行
推荐使用 Visual Studio Code 并配置好对应的 c_cpp_properties.json 文件。
1 2 3 4 5 |
. ├── data.in ├── data.out ├── main.cpp └── main.py |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <bits/stdc++.h> using namespace std; int main () { freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); // Start Code Here fclose(stdin); fclose(stdout); return 0; } |
1 2 3 4 5 6 7 8 9 |
# C++ clang-format -style=google -i main.cpp cpplint --filter=-legal/copyright,-build/namespaces main.cpp g++ -g -O0 main.cpp && ./a.out && cat data.out # Python autopep8 --in-place --aggressive --aggressive main.py pylint --disable=C0103,C0114,C0116 main.py python3 main.py < data.in |
相关书籍中的题目整理
- 算法竞赛入门经典(第二版)& 例题习题解答 :两本书中从第 3 章开始出现的习题和例题解答。