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. swagger ui 值类型形参加文字注释

    例: @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", name = "indexCondition ...

  2. Android手势识别的发展

    在播放器.与手势识别.所以,看看今天的我们Android手势识别. 首先,我们需要站在巨人的肩膀上.有些人举了个例子和说明. 第一章: http://www.2cto.com/kf/201110/10 ...

  3. Android 5.0(L) ToolBar(替代ActionBar) 现实(四)

    经过三天休息,我回来了,我们继续讨论Toolbar. 在此之前假设您正在步步紧跟下来的序列,然后,你应该注意到MainActivity据说他已被警告.因为他们,我们声明toolbar对象.但一直没有用 ...

  4. Visifire charts ToolBar

    <charts:Chart x:Name="ChartPat" Theme="Theme2" BorderBrush="Gray" P ...

  5. MVC 用基架创建Controller,通过数据库初始化器生成并播种数据库

    1 创建MVC应用程序 2 在Model里面创建实体类 using System; using System.Collections.Generic; using System.Linq; using ...

  6. github中README.md文件写法解析,git指令速查表

    http://blog.csdn.net/u012234115/article/details/41778701 http://blog.csdn.net/u012234115/article/det ...

  7. WPF应用程序的启动画面[Splash Screen本质分析]

    原文:WPF应用程序的启动画面[Splash Screen本质分析] 不经意间发现了wpf的这个小玩意,感觉蛮有意思的.我在项目中添加了一张图片 如图: wpf-1.JPG(10.73 K) 2010 ...

  8. 浅谈.NET(C#)与Windows用户账户信息的获取

    原文:浅谈.NET(C#)与Windows用户账户信息的获取 目录 1. 用户账户名称 - 使用Environment类 2. 用户账户信息 - 使用WindowsIdentity和IdentityR ...

  9. 十个 Web 开发者熟悉的经典开源项目和工具

    摘要: 一个都不知道的算我输! 这篇文章主要列出了曾经乃至现在都十分受 Web 开发者欢迎的开源工具,相信使用开源工具的 Web 开发者会对它们感兴趣的,它们中有的甚至诞生十多年了,但仍然在发光发热. ...

  10. 通过Graphics对象获取它所属的Control

    using System.Runtime.InteropServices;   [DllImport("user32.dll")] public static extern Int ...