A8392 | Substring and Subsequence
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 as $s=s_{1}s_{2}...\ s_{|s|}$ .
A substring of $s$ is a non-empty string $x=s[a...\ b]=s_{a}s_{a+1}...\ s_{b}$ ( $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[p_{1}p_{2}...\ p_{|y|}]=s_{p1}s_{p2}...\ s_{p|y|}$ ( $1<=p_{1}<p_{2}<...<p_{|y|}<=|s|$ ). For example, "coders" is a subsequence of "codeforces". Two subsequences $u=s[p_{1}p_{2}...\ p_{|u|}]$ and $v=s[q_{1}q_{2}...\ q_{|v|}]$ are considered different if the sequences $p$ and $q$ are different.
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=s_{1}s_{2}...\ s_{|s|}$ .
A substring of $s$ is a non-empty string $x=s[a...\ b]=s_{a}s_{a+1}...\ s_{b}$ ( $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[p_{1}p_{2}...\ p_{|y|}]=s_{p1}s_{p2}...\ s_{p|y|}$ ( $1<=p_{1}<p_{2}<...<p_{|y|}<=|s|$ ). For example, "coders" is a subsequence of "codeforces". Two subsequences $u=s[p_{1}p_{2}...\ p_{|u|}]$ and $v=s[q_{1}q_{2}...\ q_{|v|}]$ are considered different if the sequences $p$ 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$ $(10^{9}+7)$ .
输入输出样例
输入 #1
aa aa
输出 #1
5
输入 #2
codeforces forceofcode
输出 #2
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]$ ".
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted