Hamed has recently found a string t and suddenly became quite fond of it. He spent several days trying to find all occurrences of t in other strings he had. Finally he became tired and started thinking about the following problem. Given a string s how many ways are there to extract k ≥ 1 non-overlapping substrings from it such that each of them contains string t as a substring? More formally, you need to calculate the number of ways to choose two sequences a1, a2, …, ak and b1, b2, …, bk satisfying the following requirements:
k ≥ 1
t is a substring of string saisai + 1… sbi (string s is considered as 1-indexed).
As the number of ways can be rather large print it modulo 109 + 7.
Input
Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.
Output
Print the answer in a single line.
Sample test(s)
input
ababa
aba
output
5
input
welcometoroundtwohundredandeightytwo
d
output
274201
input
ddd
d
output
12
题目意思让我读了好久,可怜我渣渣英语。 题意大概是 先给你一个串 S 然后再给你一个串 T ,那么如果一个集合,这个集合里面有一些串 分别为 a1,a2,a3,a4…ax
如果 这些串都属于 S的子串并且 这些串不能含有相同的第某个字符,即是不能重复 且 T 也是这些串每个串的子串,那么这个集合就是一个合法集合,问有多少个合法集合
举个例子吧 ababa 和 aba 那么符合条件的集合 有5个 分别 是 { aba } ,{abab} ,{ababa}. {baba},{aba}.
而且这是子串 就是中间不能隔开的是完整的,substring 子串,subquence 子序列 子序列可以不连续
f[n]代表前n个字符来划分,有多少种划分方式,sum[i]代表1到i的f的和,
先用kmp记录每个t子串的开始位置。然后根据这样的位置进行转移
f[i] 代表在匹配串里选了i作为匹配串的一部分加上前面l个字符取法的情况。sum代表前面有多少种划分方法。f[i]=f[i-1] 上一步有的划分方法肯定都有。加上当前步的划分方法。 当前的划分方法来源于,上一个匹配串确定了 加上 l种取法。加上i本身,所有的原有取法加上i本身就可以多算一种取法。
#include <bits/stdc++.h>
using namespace std;
char s[200005],t[200005];
int len1,len2,f[2000005],sum[200005],p[1200005],pd[200005];
const int Mod = 1e9+7;
int read()
{
int t=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;ch=getchar();
while('0'<=ch&&ch>='9')
{
t=t*10+ch-'0';ch=getchar();
}
return t*f;
}
}
void kmp()
{
p[1]=0;int j=0;
for(int i=2;i<=len2;i++)
{
while(j>0&&t[j+1]!=t[i]) j=p[j];
if(t[j+1]==t[i]) j++;
p[i]=j;
}
j=0;
for(int i=1;i<=len1;i++)
{
while(j>0&&t[j+1]!=s[i]) j=p[j];
if(t[j+1]==s[i]) j++;
if(j==len2)
pd[i]=i-len2+1;
}
for(int i=1;i<=len1;i++)
{
if(!pd[i]) pd[i]=pd[i-1];
}
}
int main()
{
scanf("%s",s+1);scanf("%s",t+1);
len1=strlen(s+1);len2=strlen(t+1);
kmp();
for(int i=1;i<=len1;i++)
{
f[i]=f[i-1];
int l=pd[i];
if(!l) continue;
(f[i]+=(sum[l-1]+l)%Mod)%=Mod;
sum[i]=(sum[i-1]+f[i])%Mod;
}
printf("%d\n",f[len1] );
}