CodeForces 163A Substring and Subsequence(DP)
2016-04-13 16:04
429 查看
题意:给你两个字符串,然后问你第一个字符串的子串和第二个子序列有多少对相同的串
思路:dp[i][j]表示第一个串以i结尾,第二个串以j结尾的方案数,最简单的dp就是dp[i][j]=1+dp[i-1][j-1]+dp[i-1][j-2]+....+dp[i-1][1],然后把前面的弄成前缀和就可以了
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 "xy" 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 "xy" 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
Input
Output
Input
Output
Hint
Let's write down all pairs "xy" 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]".
思路: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; #define LL long long const int maxn = 5005; const int mod = 1e9+7; int dp[maxn][maxn]; char a[maxn],b[maxn]; int main() { scanf("%s%s",a+1,b+1); int len1 = strlen(a+1); int 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]=(dp[i][j]+dp[i-1][j-1]+1)%mod; dp[i][j]=(dp[i][j]+dp[i][j-1])%mod; } } LL ans = 0; for (int i = 1;i<=len1;i++) ans = (ans+dp[i][len2])%mod; printf("%lld\n",ans); }
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 "xy" 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 "xy" 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
Input
aa aa
Output
5
Input
codeforces forceofcode
Output
60
Hint
Let's write down all pairs "xy" 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]".
相关文章推荐
- Uestc ABCDE 1037
- StringBuilder和stringBuffer的区别
- [LeetCode]Longest Increasing Subsequence
- -[UIWindow endDisablingInterfaceAutorotationAnimated:]
- 期望+DP ZOJ 3929 Deque and Balls
- UITableView官方文档翻译
- 175.Which two statements are true regarding savepoints? (Choose two.)
- 可展开的UITableViewCell
- hue安装
- WWDC 2013 Session笔记 - UIKit Dynamics入门
- UIRefreshControl控件
- duilib 快捷键发送消息
- Request 的常用方法
- UIView 层次,层次,最顶部 最底部
- iOS UIFont 字体名字
- 第六章 consul UI
- configure: error: failed to find required module google.protobuf
- iOS开发-去掉UITableView HeaderView或FooterView随tableView 移动的黏性(sticky
- Invalid layout of java.lang.String at value
- 关于easyui tree控件树节点的搜索实现