题目链接:http://codeforces.com/problemset/problem/163/A

题意:

  给你两个字符串a,b,问你有多少对"(a的子串,b的子序列)"可以匹配。

题解:

  表示状态:

    dp[i][j] = pairs

    a的子串以a[i]结尾,b的子序列以b[1 to j]结尾的方案数。

  找出答案:

    ans = ∑ dp[i][lb]

    (la,lb代表a和b的长度)

  如何转移:

    dp[i][j] = dp[i][j-1]

    if(a[i] == b[j]) dp[i][j] += dp[i-1][j-1]+1

    a[i]和b[j]相同时,可以将dp[i][j]中的匹配都向后延伸一位。

    同时(a[i],b[j])也是一种匹配,所以还要+1。

  边界条件:

    set dp = 0

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 5005
#define MOD 1000000007 using namespace std; int ans=;
int dp[MAX_N][MAX_N];
char a[MAX_N];
char b[MAX_N]; int main()
{
scanf("%s%s",a+,b+);
int la=strlen(a+);
int lb=strlen(b+);
memset(dp,,sizeof(dp));
for(int i=;i<=la;i++)
{
for(int j=;j<=lb;j++)
{
dp[i][j]=(dp[i][j-]+(a[i]==b[j])*(dp[i-][j-]+))%MOD;
}
ans=(ans+dp[i][lb])%MOD;
}
printf("%d\n",ans);
}

Codeforces 163A Substring and Subsequence:dp【子串与子序列匹配】的更多相关文章

  1. CodeForces 163A Substring and Subsequence dp

    A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...

  2. Codeforces 163A Substring and Subsequence

    http://codeforces.com/problemset/problem/163/A?mobile=true 题目大意:给出两个串,问a的连续子串和b的子串(可以不连续)相同的个数. 思路:在 ...

  3. Codeforce 163 A. Substring and Subsequence DP

    A. Substring and Subsequence   One day Polycarpus got hold of two non-empty strings s and t, consist ...

  4. CodeForces - 919D Substring (拓扑排序+dp)

    题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...

  5. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  6. CF-163A Substring and Subsequence 字符串DP

    Substring and Subsequence 题意 给出两个字符串s,t,求出有多少对s的子串和t的子序列相等. 思路 类似于最长公共子序列的dp数组. dp[i][j]表示s中以i为结尾的子串 ...

  7. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  8. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  9. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

随机推荐

  1. sparkstreaming+socket workCount 小案例

    Consumer代码 import org.apache.spark.SparkConf import org.apache.spark.streaming.StreamingContext impo ...

  2. eclipse +cygwin+C++

    用Android eclipse做C++开发,一开始提示no binary的错误,貌似是因为没有编译二进制出来,我本机装了cygwin, 在命令台输入gcc,无显示,说明我没有把cygwin/bin的 ...

  3. Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree

    Problem A tree is a connected graph with no cycles. A rooted tree is a tree in which one special ver ...

  4. MIC中的数据传输

    先看一段代码,如下 #include<stdlib.h> #include<stdio.h> #define LEN 5 int main(int argc,char** ar ...

  5. 06 Memcached中的一些参数限制

    一: Memcached中的一些参数限制 Key的长度:250字节(二进制协议支持65536个字节) value的限制:1M ,一般都是存储一些文本,如新闻标题等等这个值足够了, 内存的限制:32位下 ...

  6. hdu 5881 Tea (2016 acm 青岛网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5881 Tea Time Limit: 3000/1000 MS (Java/Others)    Me ...

  7. IOS程序国际化

    1.1 新建一个Single View app模版项目,命名为Localization. 1.2 新建后,可以看到工作目录结构文件如下,单击InfoPlist.strings,查看右边的属性,在Loc ...

  8. 安装Hadoop 1.1.2 (一 安装JDK)

    1 下载jdk1.7 xxx .rpm 2 以Root权限登陆 3 修改文件权限  chmod +x jdk-7u25-linux-x64.rpm 4 安装 JDK  rpm -ivh jdk-7u2 ...

  9. 【BZOJ2401】陶陶的难题I 欧拉函数+线性筛

    [BZOJ2401]陶陶的难题I 题意:求,n<=1000000,T<=100000 题解:直接做是n*sqrt(n)的,显然会TLE,不过这题a和b都是循环到n,那么就可以进行如下的神奇 ...

  10. React-Native在gitHub下载的Demo不能运行问题!!!

    1.目前找到的最可行的运行React-Native Demo的解决方案 请参考:http://blog.csdn.net/shubinniu/article/details/52873250 2.检查 ...