// Return the number of ways that all n2 elements of a2 appear // in the n1 element array a1 in the same order (though not // necessarily consecutively). The empty sequence appears in a // sequence of length n1 in 1 way, even if n1 is 0. // For example, if a1 is the 7 element array // 10 50 40 20 50 40 30 // then for this value of a2 the function must return // 10 20 40 1 // 10 40 30 2 // 20 10 40 0 // 50 40 30 3 int countIncludes(const double a1[], int n1, const double a2[], int n2) { if(n2 == 0) return 1; if(n2 > n1) return 0; if(a1[0] != a2[0]) return countIncludes(a1+1, n1-1, a2, n2); else return countIncludes(a1+1, n1-1, a2+1, n2-1) + countIncludes(a1+1, n1-1, a2, n2); }
// Return the number of ways that all n2 elements of a2 appear // in the n1 element array a1 in the same order (though not // necessarily consecutively). The empty sequence appears in a // sequence of length n1 in 1 way, even if n1 is 0. // For example, if a1 is the 7 element array // 10 50 40 20 50 40 30 // then for this value of a2 the function must return // 10 20 40 1 // 10 40 30 2 // 20 10 40 0 // 50 40 30 3 int countIncludes(const double a1[], int n1, const double a2[], int n2) { int count = 0; if (n2 <=0) return 1; if (n1 <= 0) return 0; if (n1 < n2) return 0; if (a2[0] != a1[0]) return countIncludes(a1+1, n1-1, a2, n2); else { //if a match is found, move on to next element of a2 int next = countIncludes(a1+1, n1-1, a2+1, n2-1); //but also continue checking through a1 for same match int same = countIncludes(a1+1, n1-1, a2, n2); return same+next; } }
int countIncludes(const char a1[], int n1, const char a2[], int n2) { if (n2<1) { return 1; } if (n1 < 1) { return 0; } if (a1[0] == a2[0]) { return countIncludes(a1 + 1, n1 - 1, a2 + 1, n2 - 1)+countIncludes(a1 + 1, n1 - 1, a2, n2); } else{ return countIncludes(a1 + 1, n1-1, a2, n2); } }