CodeForces 163A Substring and Subsequence dp
A. Substring and Subsequence
题目连接:
http://codeforces.com/contest/163/problem/A
Description
One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "x y" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string s or different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences.
The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2... s|s|.
A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a ≠ c or b ≠ d. For example, if s="codeforces", s[2...2] and s[6...6] are different, though their content is the same.
A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≤ p1 < p2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequences p and q are different.
Input
The input consists of two lines. The first of them contains s (1 ≤ |s| ≤ 5000), and the second one contains t (1 ≤ |t| ≤ 5000). Both strings consist of lowercase Latin letters.
Output
Print a single number — the number of different pairs "x y" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7).
Sample Input
codeforces
forceofcode
Sample Output
60
Hint
题意
给你两个字符串,然后问你第一个字符串的子串和第二个子序列有多少对相同的串
题解:
dp[i][j]表示第一个串以i结尾,第二个串以j结尾的方案数
最简单的dp就是dp[i][j]=1+dp[i-1][j-1]+dp[i-1][j-2]+....+dp[i-1][1]
然后后面这个累加可以直接维护成前缀和就好了,这样复杂度就降下来了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5005;
const int mod = 1e9+7;
int dp[maxn][maxn];
char a[maxn],b[maxn];
int updata(int x,int y)
{
return (x+y)%mod;
}
int main()
{
scanf("%s%s",a+1,b+1);
int len1 = strlen(a+1),len2 = strlen(b+1);
for(int i=1;i<=len1;i++)
{
for(int j=1;j<=len2;j++)
{
if(a[i]==b[j])
dp[i][j]=updata(dp[i][j],dp[i-1][j-1]+1);
dp[i][j]=updata(dp[i][j],dp[i][j-1]);
}
}
long long ans = 0;
for(int i=1;i<=len1;i++)
ans=updata(ans,dp[i][len2]);
printf("%d\n",ans);
}
CodeForces 163A Substring and Subsequence dp的更多相关文章
- Codeforces 163A Substring and Subsequence:dp【子串与子序列匹配】
题目链接:http://codeforces.com/problemset/problem/163/A 题意: 给你两个字符串a,b,问你有多少对"(a的子串,b的子序列)"可以匹 ...
- 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条有向边,求图中路径上最长出现的相同字母数. 分析:首先如果这张图中有环,则可以取无限大的字符数,在求拓扑排序的同时可以确定是否存在环. 之后在拓扑排序的结 ...
- 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中所有权 ...
- Educational Codeforces Round 9 D. Longest Subsequence dp
D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...
- Codeforces 1015F Bracket Substring AC自动机 + dp
Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...
- Consecutive Subsequence CodeForces - 977F (map优化DP)·
You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...
随机推荐
- dos攻击
概念理解 DoS到底是什么?接触PC机较早的同志会直接想到微软磁盘操作系统的DOS--DiskOperationSystem?不,此DoS非彼DOS也,DoS即DenialOfService,拒绝服务 ...
- Linux上修改weblogic的内存大小
我们经常在使用WebLoigc部署应用程序后,发现程序运行速度并不是很快,遇到这种情况我们可以尝试调整启动时分配的内存,设置方法有两种: 一.在../domain/setDomainEnv.sh文件中 ...
- matlab 之基础使用
dir(xxx,'.jpg') :读取某文件所有jpg格式的图片,并获取图片属性信息 size(x,1) :获得x矩阵多少行 cell(): 申明数组 注释选定代码的快捷操作:Ctrl+R
- (转载)OC学习篇之---类目的概念和使用
上一篇文章介绍了OC中的@class关键字的使用,这一篇我们介绍一下,OC中的一个特有的亮点:类目 首先我们来看一下场景,如果我们现在想对一个类进行功能的扩充,我们该怎么做? 对于面向对象编程的话,首 ...
- 用js完成blog项目
//前台调用 var $ = function (args) { return new Base(args); } //基础库 function Base(args) { //创建一个数组,来保存获取 ...
- 服务器进程为何通常fork()两次
首先,要了解什么叫僵尸进程,什么叫孤儿进程,以及服务器进程运行所需要的一些条件.两次fork()就是为了解决这些相关的问题而出现的一种编程方法. 孤儿进程 孤儿进程是指父进程在子进程结束之前死亡(re ...
- 第二百三十天 how can I 坚持
上周日去蟒山摘的松子吗?应该是松子吧,裂开了呢.为啥呢.原来博客园可以上传图片,只是上传起来好费劲啊. 今天程哥问给我分的活多不多,我竟然说了句好多,哎.其实很多问题可以用还好来回答,还好,还行,哈哈 ...
- Options for Debugging Your Program or GCC
[Options for Debugging Your Program or GCC] -g Produce debugging information in the operating system ...
- UVaLive 6805 Pantun Grader (恶心的模拟)
题意:给定 n 首诗,然后让你给他打分,有四种规则,第一种是按音节打,如果音节在8-12之间,就加10,第二种就是按押韵打,如果13,24一个押韵就加20分. 第三种按音节数打,13,24有一个一样的 ...
- Jsch
JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use por ...