I (embarrassingly) misunderstood this question when I first posted an answer. I post this with the following caveats about 'longest sequence':
- In the following overlapping sequences will also be identified (this may not be the desired functionality), e.g. {1,2,3,1,2,3,1,2,3}->{1,2,3,1,2,3}. This could be accounted for.
- More than one sequence could be the same longest length. Methods such as
Fold[Replace, #, {{___, Longest[x__], ___,
x__, ___} :> {x}, {_} | # :> {}}] &
will detect first identified. e.g
longSS[{a, b, f, c, d, a, b, g, c, d}]
yields {a,b} but {c,d} is also an acceptable answer.
Note also extra curly brackets in output relate to the issue of possible multiple maximal subsequences.
func[u_] :=
Module[{v, tb, mx, mxm, ps, sb, pk, pks},
v = Append[u, ToString[u[[-2]] u[[-1]]]];
tb = Table[
MapThread[Boole@SameQ[#1, #2] &, {v, RotateLeft[v, j]}], {j,
Length[v] - 1}];
mx = (Max /@ Map[Total, Split /@ tb, {2}]);
mxm = Max@mx;
If[mxm <= 1, Return[{}]];
ps = Position[mx, mxm];
sb = Split /@ Extract[tb, ps];
pk = Map[
Function[x, Flatten[If[Total@# == Max@mx, #, 0. #] & /@ x]], sb];
pks = Union@Map[Union@Partition[#, mxm] &, Pick[v, #, 1] & /@ pk];
Flatten[pks, 1]
]
Testing cases:
test = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {2, 0}, {3, 0}, {3, 1}, {2,
1}, {1, 0}, {1, 1}, {4, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}, {2,
0}, {3, 0}, {3, 1}, {2, 1}, {1, 0}, {2, 2}, {4, 0}};
func[test]
yields:
{{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {2, 0}, {3, 0}, {3, 1}, {2, 1}, {1,
0}}}
func /@ {{1, 2, 3, 4}, {1, 2, 3, 1, 2}, {1, 2, 3, 4, 1, 5}, {1, 2, 3,
1, 2, 3}}
yields:
{{}, {{1, 2}}, {}, {{1, 2, 3}}}
More than one maximal repeated subsequence:
func[{a, b, c, d, g, a, b, f, c, d}]
yields:
{{a, b}, {c, d}}
or
func[{a, b, c, f, g, h, a, b, c, x, f, g, h, y, f, g, h}]
yields:
{{a, b, c}, {f, g, h}}
The original test case:
func[{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {2, 0}, {3, 0}, {3, 1}, {2,
1}, {1, 0}, {1, 1}, {4, 0}}]
gives:
{{{1, 0}, {1, 1}}}
Excepting extra parentheses, test cases are handled. Overlap detection may not be a desired feature.
I post this, not as an ideal answer, but probably as a penance for misunderstanding question in first place. I am learning a lot looking at the other answers.
{{3,0},{3,1}}
? $\endgroup${{1,0},{1,1}}
... $\endgroup$