Longest common sub-sequences with a condition
A two-dimensional $dp[i][j]$ is not fine enough to enable a recurrence relation. We need to track more information to be able to find the solutions to larger problems from the solutions of smaller problems.
Instead of $dp[i][j]$, try filling the following two tables simultaneously

$dp\_good[i][j]$, which means the length of longest common good subsequence of $S[1:i]$ and $T[1:j]$ that ends at $S[i]$ and $T[j]$. This number is $0$ is $S[i]\not=T[j]$.
$dp\_bad[i][j]$, which means the length of longest common bad subsequence of $S[1:i]$ and $T[1:j]$ that ends at $S[i]$ and $T[j]$. Here a bad subsequence just means it is not a good subsequence. Again, this number is $0$ is $S[i]\not=T[j]$.

The answer will be the maximum of all $dp\_good[i][j]$.
The above approach should work when you are looking for a sequence that &quot;contains at least one pair of numbers which are adjacent and equal.&quot;
For the original problem, Task: ZAJ
Stutter, we should define a good string as a stutter, i.e., a string that consists of successive pairs of two equal elements. A bad string will be defined as &quot;almost a stutter&quot;, that is, a strings which becomes a stutter if we delete its last element.