from typing import Sequence


def lcsubstring(A, B, traceback=True):
    """
    Computes the longest common, contiguous substring in both
    sequences `A`, `B`. The elements in `A` and `B` must be comparable
    with these comparison functions: 
    https://docs.python.org/3/reference/datamodel.html#object.__lt__

    :param A
        The first sequence of elements.
    
    :param B
        The second sequence of elements.

    :param traceback = True
        Whether to gather a list of all longest substrings in the
        resulting computation.
    
    :return
        if traceback:
            (<list:substrings>, <int:longest_size>)
        else:
            <int:longest_size>
    """
    # verification
    if not isinstance(A, Sequence) or not isinstance(B, Sequence):
        raise ValueError("Sequences A, B must be of Sequence type")

    La = len(A)
    Lb = len(B)

    # maxlen, table & stack
    maxlen = 0
    W = [ [ 0 for _ in range(Lb + 1) ] for _ in range(La + 1) ]
    S = []

    for i, a in enumerate(A):
        Ti = i + 1
        for j, b in enumerate(B):
            Tj = j + 1
            if a == b:
                k = W[Ti-1][Tj-1]
                W[Ti][Tj] = k + 1
                if k + 1 >= maxlen:
                    S.append((Ti, Tj))
                    maxlen = k + 1

    def _traceback(Ti, Tj):
        nonlocal W, S, A
        result = []
        while W[Ti][Tj] > 0:
            result.insert(0, A[Ti-1])
            Ti -= 1
            Tj -= 1
        return result
    
    if traceback:
        traceback = []
        for pos in reversed(S):
            Ti, Tj = pos
            if W[Ti][Tj] < maxlen:
                break
            traceback.append(_traceback(Ti, Tj))
        return traceback, maxlen
    return maxlen