【动态规划】HDU 5791 Two

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5791

题目大意:

  A,B两个数列,问A的子集和B的子集相等的子集对数。子集内顺序按照数列顺序,相同的数字视为不同。

题目思路:

  【动态规划】

  f[i][j]表示A前i个数,B前j个数且第j个数必取的值。g[i][j]表示j不一定必取得值。

  ans=∑f[n][j]。

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define mod 1000000007
26 #define MAX 0x7f7f7f7f
27 #define PI 3.14159265358979323
28 #define N 1004
29 using namespace std;
30 typedef long long LL;
31 int cas,cass;
32 int n,m,lll,ans;
33 int a[N],b[N];
34 int f[N][N],g[N][N];
35 int main()
36 {
37     #ifndef ONLINE_JUDGE
38 //    freopen("1.txt","r",stdin);
39 //    freopen("2.txt","w",stdout);
40     #endif
41     int i,j,k,l;
42 //    for(scanf("%d",&cas);cas;cas--)
43 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
44 //    while(~scanf("%s",s+1))
45     while(~scanf("%d",&n))
46     {
47         scanf("%d",&m);
48         ans=0;
49         for(i=1;i<=n;i++)scanf("%d",a+i);
50         for(j=1;j<=m;j++)scanf("%d",b+j);
51         for(i=1;i<=n;i++)
52         {
53             for(j=1;j<=m;j++)
54             {
55                 f[i][j]=f[i-1][j];
56                 if(a[i]==b[j])
57                     f[i][j]=(f[i][j]+g[i-1][j-1]+1)%mod;
58                 g[i][j]=(g[i][j-1]+f[i][j])%mod;
59             }
60         }
61         for(i=1;i<=m;i++)ans=(ans+f[n][i])%mod;
62         printf("%d
",ans);
63     }
64     return 0;
65 }
66 /*
67 //
68 
69 //
70 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/5813527.html