存在 O(∣Σ∣2log3n)\mathcal{O}(\lvert\Sigma\rvert^2\log^3 n)O(∣Σ∣2log3n) 的做法。轻松拿下最优解。
不过因为偷懒,所以只写了 cpp 的代码。