一道典型的DP题

一个数组a1 = 10 50 40 20 50 40 30
统计另一个数组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)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值