Codeforce 163 A. Substring and Subsequence DP
2016-01-21 19:59
561 查看
[b]A. Substring and Subsequence[/b]
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.
[b]Input[/b]
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.
[b]Output[/b]
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).
[b]Sample test(s)[/b]
input
output
input
output
[b]Note[/b]
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]".
[b]题意: [/b]
给出两个串,问a的子串和b的子序列(可以不连续)相同的个数。
[b]题解:[/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
此时当做前缀和处理就可以了
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.
[b]Input[/b]
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.
[b]Output[/b]
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).
[b]Sample test(s)[/b]
input
aa aa
output
5
input
codeforces forceofcode
output
60
[b]Note[/b]
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]".
[b]题意: [/b]
给出两个串,问a的子串和b的子序列(可以不连续)相同的个数。
[b]题解:[/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 = 5000 + 10; const int mod = 1e9 + 7; ll dp ; int main() { char a ,b ; scanf("%s%s",a+1,b+1); int len = strlen(a+1); int lenb = strlen(b+1); for(int i = 1; i <= len; i++) { for(int j = 1; j <= lenb; j++) { dp[i][j] = dp[i][j-1] % mod; if(a[i] == b[j]) dp[i][j] = (dp[i][j] + dp[i-1][j-1] + 1) % mod; } } ll ans = 0; for(int i = 1; i <= len; i++) ans = (ans + dp[i][lenb]) %mod; printf("%I64d\n",ans); return 0; }
相关文章推荐
- 使用WebRequest类下载文件
- iOS学习__UI测试题及答案
- UI组件之ProgressBar及其子类(二)SeekBar拖动条和RatingBar星级评分条的使用
- UIImagePickerController从拍照、图库、相册获取图片
- 2016.01.21 UITabBarController
- Easyui的Dialog的toolbar的自定义添加
- NSURLRequestCachePolicy 缓存策略
- UI课程(复习之前知识)
- There is already a statement named maxAmountLimit.queryList in this SqlMap
- UITabbBar的使用及属性
- UIImagePickerController拍照与摄像
- require CORE_ROOT.'fore/'.$file.'.php'; 后echo 'abc';die;无法显示'abc'
- file not found: /Users/***/Library/Developer/Xcode/DerivedData/***-febavuacartc/Build/Products/Debug
- [原创]浅谈Web UI自动化测试
- 设计模式之建造者模式(Builder)以及构建器
- NGUI 圆形 裁剪
- UITabBarController+微博简单模拟1
- CodeForces 163A Substring and Subsequence dp
- 【UEditor】 UEditor整合项目上传资源到阿里云服务器
- 蓝牙学习之旅——专业术语简述