I believe that the basic idea would be to use dynamic programming. something like that:
for i in 1:length(a) {
for j in 1:length(b) {
if (a[i]==b[j]) then {
result[i,j] = result[i-1,j-1]+1 #remember to initialize the borders with zeros
# track the maximum of the matrix
} else {
result[i,j]=0
}
}
}
this question is basically similra to the context of sequence alignment, common in bioinformatics. in fact, you should be able to use existing sequence alignment algorithms for your purpose (such as blast, etc.) by setting the "gap" penalties to very high values, practically disallowing gaps in the alignment