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 same. A' is a subsequence of A. B' is a subsequence of B. The subsequnce can be not continuous. For example, {1,1,2} has 7 subsequences {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. The answer can be very large. Output the answer mod 1000000007.
 
Input
 
The input contains multiple test cases.

For each test case, the first line cantains two integers N,M(1≤N,M≤1000). The next line contains N integers. The next line followed M integers. All integers are between 1 and 1000.

 
Output
 
For each test case, output the answer mod 1000000007.
 
Sample Input
 
3 2
1 2 3
2 1
3 2
1 2 3
1 2
 
Sample Output
 
2
3

题意:有两个串,求两两子串相同的个数有多少(可以不连续)。

思路:有点类似于LCS的DP,(+MOD)%MOD是因为有可能减出负数,因为取MOD一开始可能很大,后面变得很小。

 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 1005
#define MOD 1000000007
typedef long long LL; LL dp[N][N];
int a[N], b[N];
/*
1 2 3
2 1
*/
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m)) {
dp[][] = ;
for(int i = ; i <= n; i++) {
scanf("%d", a+i);
dp[i][] = ;
}
for(int i = ; i <= m; i++) {
scanf("%d", b+i);
dp[][i] = ;
}
/*
有这三部分
dp[i-1][j-1]
dp[i-1][j] - dp[i-1][j-1]
dp[i][j-1] - dp[i-1][j-1]
如果不匹配的话 dp[i][j] = dp[i-1][j] - dp[i-1][j-1] + dp[i][j-1] - dp[i-1][j-1] + dp[i-1][j-1]
匹配的话 dp[i][j] = 不匹配的状态 + dp[i-1][j-1] + 1
dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] 表示当前不匹配的状态有多少种,因为dp[i-1][j]和dp[i][j]中有dp[i-1][j-1]重复,所以要减去一个
如果当前匹配的话,就不用减去,因为要留一个来和当前的a[i]和b[j]匹配。
*/
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
if(a[i] == b[j]) {
dp[i][j] = (dp[i-][j] + dp[i][j-] + + MOD) % MOD;
} else {
dp[i][j] = (dp[i-][j] + dp[i][j-] - dp[i-][j-] + MOD) % MOD;
}
}
} printf("%I64d\n", dp[n][m] % MOD);
}
return ;
}

HDU 5791:Two(DP)的更多相关文章

  1. HDU 1260:Tickets(DP)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  2. HDU 5965:扫雷(DP,递推)

    扫雷 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...

  3. HDU 4833 Best Financing(DP)(2014年百度之星程序设计大赛 - 初赛(第二轮))

    Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在第dates[ ...

  4. POJ 2192 :Zipper(DP)

    http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  5. Codeforces Gym101341K:Competitions(DP)

    http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...

  6. HDU 4301 Divide Chocolate(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4301 题意: 有一块n*2大小的巧克力,现在某人要将这巧克力分成k个部分,每个部分大小随意,问有多少种分法. 思 ...

  7. HDU 4833 Best Financing (DP)

    Best Financing Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. POJ 1260:Pearls(DP)

    http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8 ...

  9. HDU 1422 重温世界杯(DP)

    点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...

随机推荐

  1. Something write in FSE 2014

    Now, I find a problem, I have become my personal CSDN into a personal electronic diary. Actually, th ...

  2. wpf采用Xps实现文档显示、套打功能

    原文:wpf采用Xps实现文档显示.套打功能 近期的一个项目需对数据进行套打,用户要求现场不允许安装office.页面预览显示必须要与文档完全一致,xps文档来对数据进行处理.Wpf的Document ...

  3. Visifire charts AxisLabels FontSize

    <charts:Chart.AxesX> <charts:Axis LineThickness="0.25" > <charts:Axis.AxisL ...

  4. XF 列表视图分组列表填充

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  5. 第0001题 : 产生随机数(顺便读random模块官方文档)

    看这个之前我准备先看一下random模块的官方文档... 在整个随机模块中,  最基础的就是random, 它产生一个 [0.0, 1.0)的浮点数. 这个模块下所有的函数实际上是绑定在一个叫做ran ...

  6. .NET与 java通用的3DES加密解密方法

    C#代码 private void button1_Click(object sender, EventArgs e) { string jiami = textBox1.Text; textBox2 ...

  7. Qt中使用Boost

    编译BOOST库 bjam stage --toolset=qcc --without-graph --without-graph_parallel --without-math --without- ...

  8. WPF——TargetNullValue(如何在绑定空值显示默认字符)

    原文:WPF--TargetNullValue(如何在绑定空值显示默认字符) 说明:在数据绑定时,如果有些字段为空值,那么在数据绑定时可以用默认值来显示为空的字段. </Grid> { L ...

  9. vs2017 cordova调试ios app

    https://docs.microsoft.com/en-us/visualstudio/cross-platform/tools-for-cordova/first-steps/ios-guide ...

  10. DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。

    其实之前我自己是没测出这个问题的,但是一放到测试的手上就出来了,原因我知道在哪里改输什么东西,但是人家不知道啊.报错如下: --------------------------- “DataGridV ...