| name | cp-style |
| description | 這個 repo 寫競程解題碼時的命名、骨架與驗證約定。要新增或修改 contest/、topic/、template/、practice/ 底下的 .cpp,或是要開新比賽資料夾時使用。 |
競程解題碼風格
適用範圍:這個 repo 的 .cpp 解題碼。其他語言(.rs / .kt 練手場次)不適用。
骨架
解題檔一律長這樣,直接照抄 template/default.cpp:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int maxn = 200005;
const int mod = 1e9 + 7;
void solve() {
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
while (t--)
solve();
}
- 單筆測資(AtCoder 幾乎都是):
int t = 1; 配註解掉的 // cin >> t;
- 多筆測資(Codeforces 幾乎都是):改成
int t; cin >> t;
- 型別別名只寫用得到的那幾行,沒用到
pll 就不要留
命名
| 項目 | 寫法 | 不要寫 |
|---|
| 陣列上界 | const int maxn | MAXN、N、SZ |
| 模數 | const int mod | MOD、const ll mod |
| 64 位元 | ll | long long、LL、#define ll long long |
| pair | pii / pll | typedef pair<int,int> pii |
| 換行 | cout << x << '\n'; | endl(會 flush)、"\n" |
| fast io | ios_base::sync_with_stdio(0); cin.tie(0); | (false) / nullptr |
變數名用短小寫縮寫,不要 camelCase。 常用的一組:pre / suf / nxt / last / now / cnt /
tot / ans / ret / mx / idx / cur / to。成對的概念取對稱的名字(fwd 配 bwd,
不要 fwd 配 back)。全名只留給真的講不清楚的東西。
ll pen = penalty[i][j];
int nxt = 1 - p;
ll fwd, bwd;
資料結構的函式沿用長名字:build / update / query / pushup / pushdown,不要縮成 upd / qry。
不要做的事
- 不用巨集。沒有
rep、all(x)、pb、fi/se。#define 一律不出現。
- 不要把輸出攢進
string out 再一次印。sync_with_stdio(0) + '\n' 已經夠快,攢字串只是讓 code 難讀。
- 不要為了少寫幾個字而讓邏輯變隱晦。例如斜率只有 ±1 時就寫
if (i % 2 == 0),不要寫 x += s 再偷偷翻轉 s。
- 不要動
template/ 的骨架。那裡是拿來複製貼上的片段,很多檔案根本沒有 main,不要硬套 solve()。
排版
由 .clang-format 決定(Google base、4 空格縮排、行寬 100、SortIncludes: false)。
改完檔案跑:
clang-format -i --style=file <file>.cpp
資料夾與檔名
| 平台 | 格式 | 範例 |
|---|
| Codeforces | {number}-{type}(數字在前) | 1098-div2、1088-div12、187-edu |
| AtCoder | {prefix}-{number}(前綴在前) | abc-468、arc-180 |
| LeetCode | {number}-{type} | 400-weekly |
比賽檔名就是題號:A.cpp … G.cpp(LeetCode 用 Q1.cpp … Q4.cpp)。
template/ 的模板檔:PascalCase、一檔一演算法、縮寫一律展開,依主題放進
graph/ tree/ ds/ string/ math/ misc/ 六個子資料夾(default.cpp 留在根目錄)。
AC → AhoCorasick、SA → SuffixArray、BIT → Fenwick、cdt → ParallelBinarySearch;
只有 KMP、CDQ 這種本身沒有全名的保留原樣。線段樹家族用共同前綴
(SegmentTree / SegmentTreeLazy / SegmentTreeBeats / SegmentTreePersistent)讓它們排在一起。
topic/ 的解題檔:{來源大寫}{題號}.cpp — POJ3020、HDU3018、CF723E、UVA11402、GYM100112K。
同一題的另一種解法加後綴 -變體(HDU1255-v2、CF536B-z)。不是特定題目的練習檔用
PascalCase 描述名(RectangleAreaIntersection)。
開新比賽資料夾用腳本,會自動放好 7 份 template 和 Makefile:
./scripts/new-contest.sh at abc-469
.\scripts\new-contest.ps1 cf 1099-div2
實驗檔、對拍用的 brute、效能測試檔不留在 repo 裡,驗證完就刪。
驗證習慣
寫完一題要證明它對,順序是:
- 編譯:
g++ -std=c++17 -O2 -Wall -Wextra -o A A.cpp
- 跑過全部官方 sample,逐筆對答案
- 有隨機性或推導不直觀的題目,寫 brute force 對拍(小 n,數百到數千筆)
- 大測資量一次執行時間,確認沒有超時風險
- 驗證完把執行檔和 brute 檔刪掉
批次改動多個檔案時(例如全 repo 重排版),先跑一次 g++ -fsyntax-only 建立基準線,
改完再跑一次比對,確保沒有新增的編譯錯誤。
比賽進行中
AtCoder 禁止在進行中的比賽使用生成式 AI。看到題目頁上「Until ... posting is prohibited」的橫幅時,
先確認那場是否已結束;還在進行中就只翻譯題意,不給做法或程式碼。