2016 Multi-University Training Contest 5 1011 Two DP
http://acm.hdu.edu.cn/showproblem.php?pid=5791
HDU5791 Two
题意 :两个数组,多少个不连续子串相等
思路:
dp[i][j] :a串i结尾,b串j结尾的不连续子串数目个数
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 typedef long long ll; 7 const int maxn=1e3+7; 8 const int mod=1e9+7; 9 const int inf=0x3f3f3f3f; 10 ll dp[maxn][maxn],a[maxn],b[maxn]; 11 int n,m; 12 int main(){ 13 while(~scanf("%d%d",&n,&m)){ 14 for(int i=1;i<=n;i++)scanf("%lld",&a[i]); 15 for(int i=1;i<=m;i++)scanf("%lld",&b[i]); 16 memset(dp,0,sizeof(dp)); 17 for(int i=1;i<=n;i++){ 18 for(int j=1;j<=m;j++){ 19 dp[i][j]=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+(a[i]==b[j]?dp[i-1][j-1]+1:0)+mod)%mod; 20 } 21 } 22 printf("%lld\n",dp[n][m]); 23 } 24 return 0; 25 }