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. Nightmare BFS

    Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The la ...

  2. 带权值的LCA

    例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...

  3. Python 3.9 性能优化:更快的 list()、dict() 和 range() 等内置类型

    Python 的 3.9.0 版本正在开发中,计划在 2020-10-05 发布 final 版本. 官方在 changelog 中披露了很多细节,其中有一项"vectorcall" ...

  4. idea 激活方法

    转载自: https://www.jianshu.com/p/7d60ea5e51e9

  5. [linux][MongoDB] mongodb学习(二):命令使用数据库

    使用数据库 # 查看数据库 > show dbs admin 0.000GB local 0.000GB # 查看表(集合) > show tables # 删除集合 > db.us ...

  6. IDEA惊天bug:进程已结束,退出代码-1073741819 (0xC0000005)

    由于昨天要写的文章没有写完,于是今天早上我四点半就"自然醒"了,心里面有事,睡觉也不安稳.洗漱完毕后,我打开电脑,正襟危坐,摆出一副要干架的态势,不能再拖了. 要写的文章中涉及到一 ...

  7. mysql5.7免安装版配置

    解压之后,新建一个my.ini 内容是: [mysql] # 设置mysql客户端默认字符集 default-character-set = utf8 [mysqld] #安装目录 basedir = ...

  8. 在 Azure CentOS VM 中配置 SQL Server 2019 AG - (上)

    前文 假定您对Azure和SQL Server HA具有基础知识 假定您对Azure Cli具有基础知识 目标是在Azure Linux VM上创建一个具有三个副本的可用性组,并实现侦听器和Fenci ...

  9. 10w+QPS 的 Redis 真的只是因为单线程和内存?360&#176; 深入底层设计为你揭开 Redis 神秘面纱!

    原文链接:10w+QPS 的 Redis 真的只是因为单线程和内存?360° 深入底层设计为你揭开 Redis 神秘面纱! 你以为 Redis 这么快仅仅因为单线程和基于内存? 那么你想得太少了,我个 ...

  10. tp5中的input助手函数

    详见手册:https://www.kancloud.cn/manual/thinkphp5/118044