Codeforce 163 A. Substring and Subsequence DP
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 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.
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.
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).
aa
aa
5
codeforces
forceofcode
60
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的更多相关文章
- CodeForces 163A Substring and Subsequence dp
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...
- CF-163A Substring and Subsequence 字符串DP
Substring and Subsequence 题意 给出两个字符串s,t,求出有多少对s的子串和t的子序列相等. 思路 类似于最长公共子序列的dp数组. dp[i][j]表示s中以i为结尾的子串 ...
- Codeforces 163A Substring and Subsequence:dp【子串与子序列匹配】
题目链接:http://codeforces.com/problemset/problem/163/A 题意: 给你两个字符串a,b,问你有多少对"(a的子串,b的子序列)"可以匹 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- Common Subsequence(dp)
Common Subsequence Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 951 Solved: 374 Description A subs ...
- Codeforces 1015F Bracket Substring AC自动机 + dp
Bracket Substring 这么垃圾的题怎么以前都不会写啊, 现在一眼怎么就会啊.... 考虑dp[ i ][ j ][ k ][ op ] 表示 已经填了 i 个空格, 末尾串匹配到 所给串 ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- HDU 5677 ztr loves substring(Manacher+dp+二进制分解)
题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...
- Educational Codeforces Round 9 D. Longest Subsequence dp
D. Longest Subsequence 题目连接: http://www.codeforces.com/contest/632/problem/D Description You are giv ...
随机推荐
- 并发编程网 - ifeve.com
并发编程网 - ifeve.com 让天下没有难学的技术 首页 JAVA 深入浅出ClassLoader 深入浅出ClassLoader Dedicate to Molly. 你真的了解ClassLo ...
- 2015.05.15,外语,学习笔记-《Word Power Made Easy》 02 “如何谈论医生”
包括Sessions 4-6: Prefix Person,nous,etc. Practice,etc. Adjective internus内部 internist [ɪn'tɝnɪst] n.内 ...
- 16.unix网络编程一卷 unp.h
unix网络编程 --ubuntu下建立编译环境 1.安装编译器,安装build-essential sudo apt-get install build-essential 2.下载本书的头文件 下 ...
- 循环引用的weak和assgin
__weak 当对象销毁后weakSelf指向的地址为nil __unsafe_unretained 挡圈对象销毁后,weakSelf执行的地址为不变,而地址对应对象已经销毁,再次访问该对象就崩溃,相 ...
- Linux命令学习-curl
作用 curl是利用URL语法的一款强大的网络工具,你可以使用它完成上传下载文件等操作. curl http://www.cnblogs.com 上诉的命令即可将页面内容打印到屏幕上. 常用参数 -o ...
- composer的一些操作
版本更新 命令行下:composer self-update 设置中国镜像 composer config -g repo.packagist composer https://packagist.p ...
- 跟着8张思维导图学习javascript (转)
学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...
- 无意中发现destoon5商城处理订单时的一些bug
最新的destoon5在商城的商品中加入了商品属性的功能,可以使用三个商品属性 而在代码中用如“49-3-0-0”来标记所选择的的商品及属性,其中第一个数字是商品的id,后三个表示的是商品对应的属性值 ...
- UNP学习笔记1——基本TCP套接字编程
1 套接字地址结构 大多数套接字函数都需要一个指向套接字地址结构的指针作为参数.每个协议族都定义了自己的套接字结构.这些套接字的结构以sockaddr_开头,以每个协议族唯一的后缀名结尾. 1.1 I ...
- RocketMQ学习笔记(11)----RocketMQ的PushConsumer和PullConsumer
1. PushConsumer 推,Broker主动向Consumer推消息,它Consumer的一种,应用通常向对象注册一个Listener接口,一旦接收到消息,Consumer对象立刻回调Lins ...