hdu_5791_Two(DP)
题目链接:hdu_5791_Two
题意:
给你两串数列,问你相同的子序列有多少个,要注意,可以重复,比如1 和1 1 1 ,相同的子序列为3个
题解:
就和求最长公共子序列差不多,只不过要全部加起来
下面是官方题解:
Two:
水题。dp[i][j]表示A序列前i个数和B序列前j个数的相同子序列对有多少个。复杂度O(n^2)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #define F(i,a,b) for(int i=a;i<=b;i++) 4 5 6 const int N=1e3+7,mod=1e9+7; 7 int dp[N][N],m,n,a[N],b[N]; 8 9 int main() 10 { 11 while(~scanf("%d%d",&n,&m)) 12 { 13 F(i,1,n)scanf("%d",a+i); 14 F(i,1,m)scanf("%d",b+i); 15 F(i,0,1000)dp[i][0]=dp[0][i]=0; 16 F(i,1,n)F(j,1,m) 17 { 18 int *p=&dp[i][j]; 19 *p=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1])%mod; 20 if(*p<0)*p+=mod; 21 if(a[i]==b[j])*p=(*p+dp[i-1][j-1]+1)%mod; 22 } 23 printf("%d\n",dp[n][m]); 24 } 25 return 0; 26 }