CF 163 A Substring and Subsequence(dp)
2015-08-28 10:15
1446 查看
A. Substring and Subsequence
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
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 test(s)
input
output
input
output
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]".
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
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 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[i]==b[j]的地方统计
*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define bug printf("hihi\n")
#define mod 1000000007
#define eps 1e-8
typedef __int64 ll;
using namespace std;
#define N 5005#define mod 1000000007
char a
,b
;
int lena,lenb;
ll dp
;
int main()
{
int i,j;
while(~scanf("%s%s",b+1,a+1))
{
lena=strlen(a+1);
lenb=strlen(b+1);
ll ans=0;
for(i=1;i<=lena;i++) //a 是不连续的
{
for(j=1;j<=lenb;j++) //b是连续的
{
dp[i][j]=dp[i-1][j]; //dp[i][j]记录和前i-1和 j匹配的情况有多少种
if(a[i]==b[j]) //当a[i]==b[j] 那么就是满足新的条件的
{
dp[i][j]=(dp[i][j]+dp[i-1][j-1]+1)%mod;
ans=(ans+dp[i-1][j-1]+1)%mod;
}
}
}
printf("%I64d\n",ans);
}
return 0;
}
相关文章推荐
- 【Material Design视觉设计语言】UI组件设计(三):纸片
- AmazeUI常用组件
- 关于UIView和CALayer的区别和联系
- 【Material Design视觉设计语言】UI组件设计(二):卡片
- UIPickerView 地区解析 -- 全国省、市、区 plist 解析 -- 读取UIPickerView 当前显示内容
- [Liferay6.2.2]AUI的小坑:input的type属性
- qlite操作时出现下面错误 android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of
- 修改UISegmentedControl navigationBar的字体大小、颜色
- 一款js、css压缩工具yuicompressor
- iPhone开发之UIScrollView滚动组件的使用(三) 拖线实现按钮控制大图移动之contentInsets增加内容边距
- IOS简单的选择器实现UIPickerView(省+市+区)
- PHP的Include和require
- POJ 2533 Longest Ordered Subsequence(LIS)
- 如何让segue失去跳转功能
- UI设计-不简单?简单(1)
- EasyUI datagrid datetimebox扩展
- 你给我解析清楚,都有了CALayer了,为什么还要UIView
- iOS NSValue
- 详解 CALayer 和 UIView 的区别和联系
- 利用UIRefreshControl实现tableView下拉刷新