Substring
MediumRating 1700CodeforcesShortest Paths & Topological Sort
Approach hintTry it first, then revealHide
Any cycle makes the value unbounded, so detect one with a topological sort first. Otherwise let dp[v][c] be the largest count of letter c on a path ending at v, propagated in topological order.