【动态规划】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\n",ans);
63     }
64     return 0;
65 }
66 /*
67 //
68
69 //
70 */

时间: 2024-10-15 00:59:24

【动态规划】HDU 5791 Two的相关文章

hdu 5791 (DP) Two

hdu 5791 Two Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1421    Accepted Submission(s): 630 Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequ

dp 动态规划 hdu 1003 1087

动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和,其实就是分成 以0结尾的序列 以1结尾的序列 以2结尾的序列 ... 以n结尾的序列 所以以n结尾的序列的最大值就是以n-1结尾的序列的最大值+n的值 或最大值是n的值 关系式: d[i]=max(d[i-1]+a[i],a[i]) d[i]为以i结尾的序列和的最大值,a[i]为第i个数的值 #in

HDU 5791:Two(DP)

http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' and sequence B' are same. For example, {1,2} and {1,2} are same. {1,2,4} and {1,4,2} are not s

hdu 5791 思维dp

题目描述: 求序列A,B的公共子序列个数: 基本思路: 想到了dp,选的状态也对,但是就是就是写不出状态转移方程,然后他们都出了,到最后我还是没出,很难受,然后主要是没有仔细考虑dp[i][j],dp[i][j-1],dp[i-1][j],dp[i-1][j-1]在A[ i]和B[i]在相同和不相同是的数量关系,我为啥就没想到要减呢,只想着怎么把他们加起来,着实智障: 定义状态dp[i][j]为序列A扫到i,序列B扫到B时候的公共子序列个数,状态转移方程如下: 其实这个状态转移方程也没那么好证明

HDU 5791 Two

Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' and sequence B' are same. For example, {1,2} and {1,2} are same. {1,2,4} and {1,4,2} are not same. A' is a subsequence of A. B' is a subsequence of B. Th

hdu 5791 Two dp

Two Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' and sequence B' are same. For example, {1,2} and {1,2} ar

HDU 5791

题意: 给出两个串和它们的长度,求有多少对相同子序列.例如, {1,1,2} 有 7 个子序列 {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. 最后结果对 1e9 + 7 取余. 解题 := = DP 如果 a[i] == b[j] , d[i][j] = d[i-1][j] + d[i][j-1] + 1;否则 d[i][j] = d[i-1][j] + d[i][j-1] - d[i-1][j-1]; 取余的时候小心负数的情况 = = #include <cst

HDU 5791 Two ——(LCS变形)

感觉就是最长公共子序列的一个变形(虽然我也没做过LCS啦= =). 转移方程见代码吧.这里有一个要说的地方,如果a[i] == a[j]的时候,为什么不需要像不等于的时候那样减去一个dp[i-1][j-1]呢?其实是要减去的,然后我们注意+1是什么呢?这两个位置是相同的,那么这一对组合是1,然后包含这一个,在dp[i-1][j-1]中相同的又可以拿出来加一遍了,因此就抵消了~ 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #

hdu 5791 Two 二维dp

Two Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 889    Accepted Submission(s): 405 Problem Description Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' an