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 ;
}

最新文章

  1. Visual Studio 2015的坑:中文字符串编译后成乱码
  2. cnodejs社区论坛4--话题列表
  3. GJM :Unity 使用SqlServer数据库 [原创]
  4. Mysql学习笔记(三)对表数据的增删改查。
  5. Zyxel Switch-How to block a fake DHCP server without enabling DHCP snooping?
  6. 前端js的书写规范和高效维护的方案_自我总结使用的方案
  7. 5.9-4用字符串生成器给字符串str追加1~10这10个数字
  8. discuz X2.0教程]教你快速了解Discuz!程序文件功能,修改文件从此不用再求人
  9. 用于MySql的SqlHelper
  10. Spring之Spring MVC
  11. 第三代搜索推出网民评价系统,seo末日还会远吗?
  12. Android中的跨进程通信方法实例及特点分析(二):ContentProvider
  13. Cannot complete the install because one or more required items could not be found
  14. 小P的秘籍
  15. ceph S3客户端操作--s3cmd
  16. Linux内核分析第二周总结
  17. [HDU4123]Bob’s Race
  18. L222 词汇题
  19. Java多线程——死锁
  20. web页面超时自动退出方法

热门文章

  1. SICP 1.6-1.8
  2. Android Camera2 拍照(四)——对焦模式
  3. c# SQLHelper总汇
  4. 枚举与字符串转及RecordSet转XML,JSON
  5. 伪类&amp;伪元素
  6. HtmlAgilityPack开发
  7. BAT-把当前用户以管理员权限运行(用户帐户控制:用于内置管理员帐户的管理员批准模式)
  8. Android零基础入门第66节:RecyclerView点击事件处理
  9. PRML Chapter3
  10. 解决xp越来越慢的办法(其中有些自动备份的功能)