Substring and Subsequence

题意

给出两个字符串s,t,求出有多少对s的子串和t的子序列相等。

思路

类似于最长公共子序列的dp数组。

dp[i][j]表示s中以i为结尾的子串和t中前j个的子序列相等的个数。

转移的时候dp[i][j]=dp[i][j-1];

如果s[i]==t[j]那么dp[i][j]+=dp[i-1][j-1]+1;,1是s[i]自身作为子串的情况.

最后统计dp[i][lent]的和

代码

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f; char s[N],t[N];
int dp[5050][5050];
int main()
{
scanf("%s%s",s+1,t+1);
int lens=strlen(s+1);
int lent=strlen(t+1);
int ans=0;
for(int i=1; i<=lens; i++)
{
for(int j=1; j<=lent; j++)
{
dp[i][j]=dp[i][j-1];
if(s[i]==t[j])
{
dp[i][j]+=dp[i-1][j-1]+1;
dp[i][j]%=mod;
}
}
}
for(int i=1; i<=lens; i++)
{
ans+=dp[i][lent];
ans%=mod;
}
printf("%d",ans);
return 0;
}

CF-163A Substring and Subsequence 字符串DP的更多相关文章

  1. Codeforces 163A Substring and Subsequence:dp【子串与子序列匹配】

    题目链接:http://codeforces.com/problemset/problem/163/A 题意: 给你两个字符串a,b,问你有多少对"(a的子串,b的子序列)"可以匹 ...

  2. CodeForces 163A Substring and Subsequence dp

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

  3. AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)

    引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度  pos[i][j]表示从i开始的j字符最早出现位 ...

  4. Codeforces 163A Substring and Subsequence

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

  5. 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 ...

  6. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  7. 【BZOJ 2121】 (字符串DP,区间DP)

    2121: 字符串游戏 Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,B ...

  8. NOIP2015Day2T2子串(字符串dp)

    又被“if(a=b)”坑了QAQ...写C++还是得开Warning,这么久了pascal还没改过来咋回事啊QWQ 题目大意就不说了OWO 网上的题解都不怎么看得懂啊...好像写得都很乱?还是我太sb ...

  9. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. 并查集判树 poj 1308

    例题: poj 1308 题目大意比较简单,对任意两个点,有且仅有一条道路,也就是一棵树. 题解:一棵树中,肯定是不能有环的,而且只能由一个根节点.(没认真读题,只知道在那里判环....),所以这个题 ...

  2. F - Robot Motion 栈加BFS

    A robot has been programmed to follow the instructions in its path. Instructions for the next direct ...

  3. Python数据预处理:使用Dask和Numba并行化加速

    如果你善于使用Pandas变换数据.创建特征以及清洗数据等,那么你就能够轻松地使用Dask和Numba并行加速你的工作.单纯从速度上比较,Dask完胜Python,而Numba打败Dask,那么Num ...

  4. Docker 中如何安装配置 Nginx

    拉取 nginx 最新版镜像,然后简单启动一个 nginx 容器: docker pull nginx:latest docker run --name nginx01 -d -p 80:80 ngi ...

  5. 2019-2020-1 20199325《Linux内核原理与分析》第三周作业

    在实验楼当中进行实验3的实践:主要是针对cpu占用分配,使用时间片轮转算法进行分配 在Shell命令当中输入如下代码: $ cd ~/LinuxKernel/linux-3.9.4 $ rm -rf ...

  6. docker-数据管理(3)

    Docker 容器中管理数据主要有两种方式: 数据卷(Data volumes) 数据卷容器(Data volumes containers 数据卷是一个可供一个或者多个容器使用的特殊目录,它绕过UF ...

  7. openssl 查看证书

    查看证书 # 查看KEY信息 > openssl rsa -noout -text -in myserver.key # 查看CSR信息 > openssl req -noout -tex ...

  8. opencv-3-图片存储与相对路径

    opencv-3-图片存储与相对路径 opencvqtC++ 在上一篇文章opencv 显示第一副图像 中, 我们完成了一副图像的显示, 包括使用VS,和QT 进行显示.. 本文将展示如何进行图像的显 ...

  9. git常用命令/git 部分高级命令备忘录

    常用命令 克隆 - git clone  git@gitee.com:niunafei1/git_learning.git git 创建分支 - git checkout -b dev git 切换分 ...

  10. cut,xargs,sort,tr,rename命令解析

    cut 文件内容查看 显示行中的指定部分,删除文件中指定字段 显示文件的内容,类似于下的type命令. 语法: cut(选项)(参数) 选项: -b:仅显示行中指定直接范围的内容: -c:仅显示行中指 ...