B. Obsessive String
 

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.

Examples
input
ababa
aba
output
5
 

题意:

给两个串S,T,问能找出多少的S的(a1,b1)(a2,b2)..(ak,bk),使Sa1---Sb1,...Sak---Sbk都包含子串T,其中k>=1,且(a1,b1)...(ak,bk)互不相交。

题解:

  kmp预处理匹配点。。。

  f[i]表示前i个的合法划分数。。

  f[i]=f[i–1] (表示将最后一个舍弃)

  sum[i]=∑ f[k] (k<=i)

  设上一个匹配点为last

  f[i]+=sum[last–1]+last

  即有两种情况

  [1….L-1(这部分任意,只要合法,允许舍弃末尾)] [L…i] 这样划分

  或者直接 k=1 即只有一个划分[L…i]

  L<=last

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
using namespace std;
const int N = 1e5+, M = , mod = , inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0 ll next[N],dp[N],f[N],sum[N];
char s[N],t[N];
int main() {
scanf("%s%s",t+,s+);
int n = strlen(t+),m = strlen(s+);
int k = ;
for(int i=;i<=m;i++) {
while(k>&&s[k+]!=s[i]) k = next[k];
if(s[k+]==s[i])k++;
next[i] = k;
}
k = ;
for(int i=;i<=n;i++) {
while(k>&&s[k+]!=t[i]) k = next[k];
if(s[k+]==t[i]) k++;
if(k==m) k = next[k],f[i] = ;
}
int last = -;
for(int i=;i<=n;i++) {
dp[i] = dp[i-];
if(f[i]) last = i-m+;
if(last!=-) dp[i]+=sum[last-]+last;
sum[i] = sum[i-]+dp[i];
dp[i]%=mod,sum[i]%=mod;
}
cout<<dp[n]<<endl;
}

Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP的更多相关文章

  1. Codeforces Round #282 Div.1 B Obsessive String --DP

    题意: 给两个串S,T,问能找出多少的S的(a1,b1)(a2,b2)..(ak,bk),使Sa1---Sb1,...Sak---Sbk都包含子串T,其中k>=1,且(a1,b1)...(ak, ...

  2. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  3. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  4. Codeforces Round #282 (Div. 1) A. Treasure 水题

    A. Treasure Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/494/problem/A ...

  5. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  6. Codeforces Round #303 (Div. 2) B. Equidistant String 水题

    B. Equidistant String Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...

  7. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

  8. Codeforces Round #604 (Div. 2) A. Beautiful String

    链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...

  9. Codeforces Round #656 (Div. 3) D. a-Good String

    题目链接:https://codeforces.com/contest/1385/problem/D 题意 一个小写字母串称为 $c-good\ string$,如果至少满足以下条件之一: 字符串长度 ...

随机推荐

  1. Elasticsearch日志收集

    Install pip if necessary curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py&q ...

  2. Redis(三)、Redis主从复制

    一.主从复制 主从复制:主节点负责写数据,从节点负责读数据,从而实现读写分离,提高redis的高可用性. 让一个服务器去复制(replicate)另一个服务器,我们称呼被复制的服务器为主节点(mast ...

  3. maven使用杂记

    maven test使用记录 运行指定的测试类:     >mvn test -Dtest=[ClassName] 运行测试类中指定的方法:(这个需要maven-surefire-plugin: ...

  4. tp5数据库操作 模型层

    一.数据模型作用 相同功能代码不用重复写多次 二.创建方式 在模块下建立model文件夹,php文件,文件名为数据库表名,其中类为数据库表名,继承Model类,模型层即为此表 namespace ap ...

  5. android 自定义空间 组合控件中 TextView 不支持drawableLeft属性

    android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...

  6. sql中数据库连接与断开式连接有什么区别?

    连接式指的是对数据的操作在 conn.Open() 与 conn.Close()之间: 断开式连接指的是 conn.Open()打开连接之后,先将数据放入adapter中,然后关闭连接(conn.Cl ...

  7. linux网络路由配置

    网卡配置文件介绍: # vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 (描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为 ...

  8. caffe学习笔记--跑个SampleCode

    Caffe默认情况会安装在CAFFERROOT,就是解压到那个目录,例如: home/username/caffe-master, 所以下面的工作,默认已经切换到了该工作目录.下面的工作主要是,用于测 ...

  9. openlayers5学习笔记-001

    tmp.initPoint = function (items) { //初始化所有农户点坐标,聚合 var count = items.length; var features = new Arra ...

  10. python tips:类的动态绑定

    使用实例引用类的属性时,会发生动态绑定.即python会在实例每次引用类属性时,将对应的类属性绑定到实例上. 动态绑定的例子: class A: def test1(self): print(&quo ...