A. Substring and Subsequence
 

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 sy 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 ass = 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 sequencesp 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 sy 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 test(s)
input
aa
aa
output
5
input
codeforces
forceofcode
output
60
Note

Let's write down all pairs "x y" that form the answer in the first sample: "s[1...1] t[1]", "s[2...2] t[1]", "s[1...1] t[2]","s[2...2] t[2]", "s[1...2] t[1 2]".

题意: 

  给出两个串,问a的子串和b的子序列(可以不连续)相同的个数。

题解:

  dp[i][j]以a[i]结尾的以b[j]结尾相同个数。

  那么 if(a[i] == a[j]) dp[i][j] = dp[i-1][j-1] + dp[i-1][j-2] + ............+dp[i-1][1] + 1

此时当做前缀和处理就可以了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll;
const int N = + ;
const int mod = 1e9 + ;
ll dp[N][N];
int main() {
char a[N],b[N];
scanf("%s%s",a+,b+);
int len = strlen(a+);
int lenb = strlen(b+);
for(int i = ; i <= len; i++) {
for(int j = ; j <= lenb; j++) {
dp[i][j] = dp[i][j-] % mod;
if(a[i] == b[j]) dp[i][j] = (dp[i][j] + dp[i-][j-] + ) % mod;
}
}
ll ans = ;
for(int i = ; i <= len; i++) ans = (ans + dp[i][lenb]) %mod;
printf("%I64d\n",ans);
return ;
}

Codeforce 163 A. 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. CF-163A Substring and Subsequence 字符串DP

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

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

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

  4. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  5. Common Subsequence(dp)

    Common Subsequence Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 951  Solved: 374 Description A subs ...

  6. Codeforces 1015F Bracket Substring AC自动机 + dp

    Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...

  7. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  8. HDU 5677 ztr loves substring(Manacher+dp+二进制分解)

    题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...

  9. Educational Codeforces Round 9 D. Longest Subsequence dp

    D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...

随机推荐

  1. webservice为什么不能用List参数,而只能用数组代替,我想是否因为List没有具体的类型信息,但用泛型的List(如:List<customer>)为什么也不行。如果用作参数的类中含有List<T>字段该如何处理?webservice参数是否支持

    转自:https://social.microsoft.com/Forums/zh-CN/aded4301-b5f1-4aa6-aa46-16c46a60d05e/webservice20026201 ...

  2. 移动端H5页面编辑器开发实战--原理结构篇

    很久前的写的文章了,转载下发到这里 原文地址: https://blog.csdn.net/tech_meizu/article/details/52288797

  3. Ubuntu下推荐安装软件

    前言:都是全平台软件,通用性好. 1.搜狗输入法 官网下载: 不能双击.deb安装成功,需要安装依赖,可参考:https://www.cnblogs.com/chendeqiang/p/1017741 ...

  4. Oracle学习系类篇(二)

    1.Oracle对表的增删改 1.1添加列 1.2修改列 1.3 删除列 1.4 修改表名称 1.5 修改列名称 1.6 删除主键约束 1.7 添加主键约束 1.8 添加外键约束

  5. usaco 最少找零

    Description 约翰在镇上买了 T 元钱的东西,正在研究如何付钱.假设有 N 种钞票,第 i 种钞票的面值为 Vi,约翰身上带着这样的钞票 Ci 张.商店老板罗伯是个土豪,所有种类的钞票都有无 ...

  6. border-spacing和borer-collapse以及empty-cells属性

    1.border-spacing 用于设置表格中,单元格之间的间隙 示例1:border-spacing: 7px: 单元格水平方向7px间隔,垂直方向7px间隔 示例2:border-spacing ...

  7. MySQL构造测试数据

    构造测试数据(笛卡尔积,6 次100 万) create table t1(id int, val varchar(80)); set @i := 0;create table tmp as sele ...

  8. C++函数的高级特性——小结

    相对于C语言,C++增加了重载(overload).内联(inline).const和virtual四种新机制. 1 重载 只能靠参数列表而不能紧靠返回值类型的不同来区分重载函数.编译器根据参数列表为 ...

  9. BarTender无法连接到数据库?原来是微软补丁包捣的鬼

    近期有很多BarTender用户反映,在使用BarTender设计打印条码时,经常会出现错误消息6670 的提示,使得BarTender无法连接到数据库,究其原因,原来是微软补丁包捣的鬼.目前海鸥科技 ...

  10. JSP Java服务器页面

    大家好!好久不见!今日我们开始学习JSP了,一些记录基础性的知识在这里与大家分享. 先说下URL(Uniform Resource Locator 统一资源定位符). URL包括传输协议(http:/ ...