Codeforces 163A Substring and Subsequence:dp【子串与子序列匹配】
题目链接: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【子串与子序列匹配】的更多相关文章
- CodeForces 163A Substring and Subsequence dp
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...
- Codeforces 163A Substring and Subsequence
http://codeforces.com/problemset/problem/163/A?mobile=true 题目大意:给出两个串,问a的连续子串和b的子串(可以不连续)相同的个数. 思路:在 ...
- 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 ...
- CodeForces - 919D Substring (拓扑排序+dp)
题意:将一个字符串上的n个字符视作点,给出m条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- CF-163A Substring and Subsequence 字符串DP
Substring and Subsequence 题意 给出两个字符串s,t,求出有多少对s的子串和t的子序列相等. 思路 类似于最长公共子序列的dp数组. dp[i][j]表示s中以i为结尾的子串 ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
随机推荐
- HTML5 2D平台游戏开发#10Wall Jump
这个术语不知道怎么翻译比较贴切,但并不妨碍对字面意思的理解,大概就是飞檐走壁.比如: 这是游戏<忍者龙剑传>中的场景,玩家可以通过操纵角色在墙面上移动并跳跃. 首先需要实现角色抓墙这一动作 ...
- dedecms增加自定义表单管理员
打开\dede\inc\grouplist.txt 添加 >>自定义表单 >f_List>列出表单 >f_New>新建表单 >f_Edit>编辑表单 & ...
- iphone开发技术要学习的内容
一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...
- angular选择器功能
1.$event对象 $event对象其实就是潜在的jQuery事件对象,通过$event.currentTarget获取当前元素,通过$event.target获取当前元素的子元素. 例如: ...
- android 自定义 listView
目录: 1.主布局 ListView <?xml version="1.0" encoding="utf-8"?><RelativeLayou ...
- gulp安装教程
1.安装nodejs并选装cnpm: npm install cnpm -g --registry=https://registry.npm.taobao.org 2.全局安装gulp: cnpm i ...
- Cadence 15.7 win7无法启动解决方法
原帖地址:http://blog.sina.com.cn/s/blog_69a5dce90100kscf.html 按照XP下的破解方法安装Cadence15.7后, 如果不能正常启动Cadence ...
- UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)
今天使用MySQLdb往MySQL插入中文数据遇到一个异常: UnicodeEncodeError: 'latin-1' codec can't encode characters in positi ...
- python 捕获异常详细信息
import os import sys import traceback BasePath = os.path.dirname(os.getcwd()) sys.path.append(BasePa ...
- Power Designer体验之旅
版权声明:本文为博主原创文章.未经博主允许不得转载. https://blog.csdn.net/wang13667539325/article/details/36025245 从某种程度上说.不论 ...