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. css3 animate写的超炫3D转换

    上一篇中介绍了animate的基本的属性,这一篇讲的则是关于animate以及transforms的使用 <!DOCTYPE html><html lang="en&quo ...

  2. BPM控制控件的隐藏与显示

    在BPM官方给的属性中控件属性DisplayRule中设置条件控制显示或者隐藏的时候,只能控制选中的,标题也要重新设置,这样就比较麻烦,不如自己来写javascript代码. 举一个简单的例子: 通过 ...

  3. JAVA 常用集合接口List、Set、Map总结

    java中频繁使用List.Set.Map接口,将其总结如下 它们的继承与实现关系如下: Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└ ...

  4. Javascript Proxy对象 简介

    Javascript Proxy对象 简介 Javascript Proxy对象 改变你操作对象的方式 Proxies 是Javasript对象的中间件 ...或者说至少是那种很早的版本. ES6 中 ...

  5. BeautifulSoup 库的使用记录

    BeautifulSoup 有何用途 如果我们需要通过脚本来抓取网络中的数据时,使用传统的字符解析等方法时是非常低效的,而BeautifulSoup则可以方便的通过接口来获取标签中所想要得到的数据.主 ...

  6. Android Fragment 初步解析

    Fragment经常在我们的开发中见到,但是自我感觉对Fragment的理解还是处于初级的阶段,接下来我将用几篇文章尽量深的解析Fragment 让我们开始吧!!! Fragment的生命周期 Fra ...

  7. 【WebApp】IOS兼容问题

    最近,主要是基于Framework7 + RequireJs框架,移动端团队开发Webview框架,内嵌Web的模式进行WebApp开发. 在开发过程中不得不遇到一些 兼容性问题. 1.现象:IOS环 ...

  8. apiCloud中aui获取不到高度,pos.h为0,offsetHeight为0问题

    apiCloud中aui获取不到高度,pos.h为0,offsetHeight为0问题 原HTML <div class="row aui-text-center"> ...

  9. Java中数组的定义方式

    数组定义方式一 动态方式(指定数组的长度) 格式: 数组存储的数据类型[]数组名字 = new 数组存储的数据类型[长度]; [] : 表示数组. 数组名字:为定义的数组起个变量名,满足标识符规范,可 ...

  10. codeforces 789 B. Masha and geometric

    链接 B. Masha and geometric depression 题意 给你一个等比数列的首项和公比q,然后给出一个上限l,m个数字,在这个等比数列里,小于l且没有在m个数字里面出现过的可以写 ...