hdu 5791 Two(LCS)

dp[i][j]表示A序列前i个数和B序列前j个数的相同子序列对有多少个。复杂度O(n^2)O(n?2??)

/*by*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL N=1010;
const LL mod=1000000007;
LL a[N],b[N];
LL dp[N][N];
int main()
{
    LL n,m,i,j;
    while(~scanf("%lld%lld",&n,&m)) {
        memset(dp,0,sizeof(dp));
        for(i=1; i<=n; i++)
            scanf("%lld",&a[i]);
        for(i=1; i<=m; i++)
            scanf("%lld",&b[i]);
        for(i=1; i<=n; i++) {
            for(j=1; j<=m; j++) {
                if(a[i]==b[j])
                    dp[i][j]=(dp[i-1][j]+dp[i][j-1]+1)%mod;
                else
                    dp[i][j]=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+mod)%mod;/*去重,相减可能是负数,先加个mod*/
            }
        }
        printf("%lld\n",dp[n][m]);
    }
    return 0;
}
时间: 2024-10-12 23:11:14

hdu 5791 Two(LCS)的相关文章

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

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)

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 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<ios

HDU 1503【LCS】(字符串合并输出)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题目大意: 给两个字符串,组成一个长度尽可能小的字符串,它包含上述两个字符串,且原字符串中的字符在该串中的相对位置不变. Sample Input apple peach ananas banana pear peach Sample Output appleach bananas pearch 解题思路:要结合样例来理解题意,本题主要难在如何输出题目要求字符串,这就需要我们仔细研究样例,去发

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 1159 dp LCS

这题的话,边界处理起来比较复杂,我归纳了讨论,最后还是wa了,哎,看了dicuss的代码,处理还是巧妙 #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <string> #include <cstring> using namespace std; const int MAXN = 1E3+10; int dp[M

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