[codeforces494B]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, ..., bksatisfying the following requirements:

  • k ≥ 1
  •   t is a substring of string sa_isa_i + 1... sb_i (string s is considered as 1-indexed).

As the number of ways can be rather large print it modulo 109 + 7.

输入

Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.

输出

Print the answer in a single line.

输入示例

welcometoroundtwohundredandeightytwo
d

输出示例


数据规模及约定

见“输入

题解

首先用 KMP 预处理一波数组 pos[i],表示串 s 第 i 位左边最靠右的那次对于 t 的完整匹配的左端点(有点拗口,举个栗子 s = "ababa",那么 pos[i] = {0, 0, 1, 1, 3})。

有了 pos 数组,就可以 dp 了。我们考虑每个位置放置一个左端点,或是一个右端点,或者不放(注意不放有两种情况,一种是刚放完左端点,一种是刚放完右端点);于是设 f[i][0] 表示最后一次放的是左端点,放在了位置 i 或者 i 的左边;f[i][1] 表示最后一次放的是右端点,放在了位置 i 或 i 的左边。转移时就是考虑当前这个位置放置还是不放,具体转移方程留给读者思考。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 100010
#define MOD 1000000007 char S[maxn], T[maxn];
int Fail[maxn], pos[maxn], f[maxn][2]; int main() {
scanf("%s%s", S + 1, T + 1); int n = strlen(S + 1), m = strlen(T + 1);
for(int i = 2; i <= m + 1; i++) {
int j = Fail[i-1];
while(j > 1 && T[j] != T[i-1]) j = Fail[j];
Fail[i] = T[j] == T[i-1] ? j + 1 : 1;
}
int p = 1;
for(int i = 1; i <= n; i++) {
while(p > 1 && T[p] != S[i]) p = Fail[p];
p = T[p] == S[i] ? p + 1 : 1;
if(p == m + 1) pos[i] = i - m + 1;
}
for(int i = 1; i <= n; i++) if(!pos[i]) pos[i] = pos[i-1];
// for(int i = 1; i <= n; i++) printf("%d%c", pos[i], i < n ? ' ' : '\n');
f[0][1] = 1;
for(int i = 1; i <= n; i++) {
f[i][0] = (f[i-1][0] + f[i-1][1]) % MOD;
f[i][1] = (f[i-1][1] + (pos[i] ? f[pos[i]][0] : 0)) % MOD;
// printf("(%d, %d)%c", f[i][0], f[i][1], i < n ? ' ' : '\n');
} printf("%d\n", (f[n][1] ? f[n][1] : MOD) - 1); return 0;
}

[codeforces494B]Obsessive String的更多相关文章

  1. [Codeforces-div.1 494B]Obsessive String

    [CF-div.1 B]Obsessive String 题目大意 两个字符串\(S,T\),求划分方案数使得一个集合中两两划分不相交且划分都包含字符串\(T\) 试题分析 kmp先求出那个位置匹配. ...

  2. Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP

    B. Obsessive String   Hamed has recently found a string t and suddenly became quite fond of it. He s ...

  3. [CF494B] Obsessive String

    Hamed has recently found a string t and suddenly became quite fond of it. He spent several days tryi ...

  4. CodeForces 494B Obsessive String ——(字符串DP+KMP)

    这题的题意就很晦涩.题意是:问有多少种方法,把字符串s划分成不重叠的子串(可以不使用完s的所有字符,但是这些子串必须不重叠),使得t串是所有这些新串的子串.譬如第一个样例,"ababa&qu ...

  5. 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, ...

  6. Codeforces 494B Obsessive String

    http://www.codeforces.com/problemset/problem/494/B 题意:给出两个串S,T,求有几种将S分成若干个子串,满足T都是这若干个子串的子串. 思路:f[n] ...

  7. CF 494B 【Obsessive String】

    很有趣的一道题 这道题提议很难懂,其实就是让你求合法的集合数目.合法的集合定义为: 1.集合中的所有串都是s的子串,且互不重叠 2.集合中的所有串都含有子串t. 看到网上很多题解说要用kmp,但我就不 ...

  8. 【codeforces #282(div 1)】AB题解

    A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  9. DP × KMP

    几道用到KMP的DP题: hdu 5763    hdu 3689    hdu 3336    codeforces 494B    codevs 3945 关于KMP的nx数组: 如果在本文中看见 ...

随机推荐

  1. Mondriaan's Dream POJ - 2411

    Mondriaan's Dream POJ - 2411 可以用状压dp,但是要打一下表.暴力枚举行.这一行的状态.上一行的状态,判断如果上一行的状态能转移到这一行的状态就转移. 状态定义:ans[i ...

  2. 题解报告:hdu 1261 字串数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1261 Problem Description 一个A和两个B一共可以组成三种字符串:"ABB ...

  3. C#中的list的System.Predicate<in T>和System.Comparison<in T>的应用

    public class Data { ; ; ; ; public Data() { count++; ma = count; } } //一句话删除满足要求的集合 Asm.RemoveAll((D ...

  4. d3学习笔记

    (1)使用enter()函数. 当要创建新的绑定数据的元素,必须使用enter().这个方法会分析当前选择的DOM元素和传给它的数据,如果数据值比对应的DOM元素多,就创建一个新的占位元素.然后把这个 ...

  5. Normal equations 正规方程组

    前面我们通过Gradient Descent的方法进行了线性回归,但是梯度下降有如下特点: (1)需要预先选定Learning rate: (2)需要多次iteration: (3)需要Feature ...

  6. DEV—【GridControl主从表】

    先附上效果图,不是想要这个效果的朋友就不用可以继续寻找了. DEV—GridControl制作主从表: (注:此例没有用到数据库,只是单纯的在内存中操作数据.) 写这一笔,是为了能更好的理解主从表,的 ...

  7. LoadRunner 11中Record无法自动生成脚本——解决办法

    [问题描述] 安装loadRunner 11, 使用IE为默认浏览器,打开一个页面进行脚本录制:录制完成后,无法生成脚本. [问题现象] 控制台输出如下: ****** Start Log Messa ...

  8. 数据分析师入门|Python安装MAC版

    最近在学数据分析师入门课,看了大纲,感觉终于不再慌乱踩坑了,开始存档最粗暴版学习笔记,遇到停止的地方按照下文红字直接输入就OK,方便和我一样的小伙伴参考呀,老师讲的很适合我这种初学者,PUSH了很多资 ...

  9. automount - 配置autofs的挂载点

    命令概要(SYNOPSIS) automount [options] mount-point map-type[,format] map [map-options] 描述(DESCRIPTION) a ...

  10. CAD参数绘制点(com接口)

    点在CAD中的作用除了可以分割对象外,还能测量对象,点不仅表示一个小的实体,而且通过点作为绘图的参考标记. pdmode是一个控制point的形式的系统变量,当pdmode=0时是可见的一个点,当pd ...