一个数组a1 = 10 50 40 20 50 40 30
统计另一个数组a2是否包含在a1中,a2在a1中可以不连续
比如
另一个数组a2 =10 20 40 则只有一种包含方式, 返回1
a2=50 40 30 则有3种包含方式 返回3
header是
统计另一个数组a2是否包含在a1中,a2在a1中可以不连续
比如
另一个数组a2 =10 20 40 则只有一种包含方式, 返回1
a2=50 40 30 则有3种包含方式 返回3
header是
int countIncludes(const double a1[], int n1, const double a2[], int n2)
solution:
int countIncludes( int a[], int n, int b[], int m)
{
if(m==0) return 1; // 或者 if(m<=0),防止非法長度
if(n==0) return 0; // 或者 if(n<=0)
//也可以加上 if(m>n) return 0; 不加的話用下面的recursion也能得到正確答案
if(a[0]==b[0] )
return countIncludes(a+1,n-1,b+1,m-1)+countIncludes(a+1,n-1,b,m);
else
return countIncludes(a+1,n-1,b,m);
}
Time: O(nm)