[CF494B] 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 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.
此题两种DP方式。
先预处理出来b串在a串中匹配的位置,然后开始DP。
设$f[i]$表示考虑到$i$位置,且$i$的最后一个字符串与b串是匹配的方案数。
显然如果$i$不是b的匹配位置,$f[i]=f[i-1]$。
如果$i$是b的匹配位置,首先考虑只有一个串, 那么答案就是$i-lb+1$,因为$1$到$i-lb+1$的所有位置都可以作为一个开始。
那如果是多个串呢?如果我们设最后一个串从位置$k$开始,那么前面的所有的方案数就是$\large \sum_{i=1}^{k}f[i]$,对于每个位置k求和,就是$\large \sum_{k=1}^{i-lb} \sum_{j=1}^{k} f[j]$。
这样只用记录一下f的前缀和和f的前缀和的前缀和就可以快速转移啦。
代码在后面贴。
还有一种方法,状态的定义略微的有些不同,设$f[i]$表示,到第i个位置之前总共有多少方案,其实就是前缀和了一下。
每次记录上一个匹配点,从上一个匹配点开始转移。
代码贴后面了。
找匹配点可以用kmp,或者hash都行。
方法1:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define reg register
#define mod 1000000007
int la, lb;
char a[], b[];
unsigned long long hsha[], hshb[], fac[];
bool End[];
int f[], sum[], Ssum[];
int ans; int main()
{
scanf("%s%s", a + , b + );
la = strlen(a + ), lb = strlen(b + );
for (reg int i = ; i <= la ; i ++) hsha[i] = hsha[i - ] * + (a[i] - 'a' + );
for (reg int i = ; i <= lb ; i ++) hshb[i] = hshb[i - ] * + (b[i] - 'a' + );
fac[] = ;
for (reg int i = ; i <= max(la, lb) ; i ++) fac[i] = fac[i - ] * ;
for (reg int i = lb ; i <= la ; i ++)
if (hsha[i] - hsha[i - lb] * fac[lb] == hshb[lb]) End[i] = ;
for (reg int i = ; i <= la ; i ++)
{
if (!End[i]) f[i] = f[i-];
else f[i] = Ssum[i - lb] + i - lb + ;
sum[i] = sum[i-] + f[i];if(sum[i] >= mod) sum[i] -= mod;
Ssum[i] = Ssum[i-] + sum[i];if(Ssum[i] >= mod) Ssum[i] -= mod;
}
for (reg int i = ; i <= la ; i ++)
ans = (ans + f[i]) % mod;
cout << ans << endl;
return ;
}
方法2:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define reg register
#define mod 1000000007
int la, lb;
char a[], b[];
int nxt[];
bool End[];
int f[], sum[]; int main()
{
scanf("%s%s", a + , b + );
la = strlen(a + ), lb = strlen(b + );
int k = ;
for (reg int i = ; i <= lb ; i ++)
{
while(k and b[i] != b[k + ]) k = nxt[k];
if (b[k + ] == b[i]) k ++;
nxt[i] = k;
}
k = ;
for (reg int i = ; i <= la ; i ++)
{
while(k and a[i] != b[k + ]) k = nxt[k];
if (b[k + ] == a[i]) k ++;
if (k == lb) End[i] = ;
}
int lst = -;
for (reg int i = ; i <= la ; i ++)
{
f[i] += f[i-];
if (End[i]) lst = i - lb + ;
if (lst != -) f[i] += sum[lst - ] + lst;
if (f[i] >= mod) f[i] -= mod;
sum[i] = sum[i-] + f[i];
if (sum[i] >= mod) sum[i] -= mod;
}
cout << f[la] << endl;
return ;
}
[CF494B] Obsessive String的更多相关文章
- [Codeforces-div.1 494B]Obsessive String
[CF-div.1 B]Obsessive String 题目大意 两个字符串\(S,T\),求划分方案数使得一个集合中两两划分不相交且划分都包含字符串\(T\) 试题分析 kmp先求出那个位置匹配. ...
- [codeforces494B]Obsessive String
[codeforces494B]Obsessive String 试题描述 Hamed has recently found a string t and suddenly became quite ...
- 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 ...
- CodeForces 494B Obsessive String ——(字符串DP+KMP)
这题的题意就很晦涩.题意是:问有多少种方法,把字符串s划分成不重叠的子串(可以不使用完s的所有字符,但是这些子串必须不重叠),使得t串是所有这些新串的子串.譬如第一个样例,"ababa&qu ...
- 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, ...
- Codeforces 494B Obsessive String
http://www.codeforces.com/problemset/problem/494/B 题意:给出两个串S,T,求有几种将S分成若干个子串,满足T都是这若干个子串的子串. 思路:f[n] ...
- CF 494B 【Obsessive String】
很有趣的一道题 这道题提议很难懂,其实就是让你求合法的集合数目.合法的集合定义为: 1.集合中的所有串都是s的子串,且互不重叠 2.集合中的所有串都含有子串t. 看到网上很多题解说要用kmp,但我就不 ...
- 【codeforces #282(div 1)】AB题解
A. Treasure time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- DP × KMP
几道用到KMP的DP题: hdu 5763 hdu 3689 hdu 3336 codeforces 494B codevs 3945 关于KMP的nx数组: 如果在本文中看见 ...
随机推荐
- PHP 错误:Warning: Cannot modify header information - headers already sent by ...
PHP初学者容易遇到的错误:Warning: Cannot modify header information - headers already sent by ...: 通常是由不正确使用 hea ...
- Winform中使用FastReport的DesignReport时怎样设置Table的size自动调整
场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- 树、图、堆、STL(来自菜鸡的"炒鸡"干粮)
树.图.堆.STL 图论基础 简单图: 没有自环,两个顶点之间最多只有一条边. 完全图: 一个简单图,每两个顶点之间都有一条边.一共有(n-1)*n/2条边. 二分图: 一个简单图,设G=(V,E)是 ...
- solr java代码
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- CentOS7.2防火墙配置
一.查看firewall以及firewall服务的状态. # 查看firewall服务状态 systemctl status firewalld # 查看firewall状态 firewall-cmd ...
- OkHttp3使用教程,实现get、post请求发送,自动重试,打印响应日志。
一.创建线程安全的okhttp单例 import service.NetworkIntercepter;import service.RetryIntercepter;import okhttp3.* ...
- LeetCode 把二叉搜索树转换为累加树
第538题 给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和. 例如: 输入: 二叉 ...
- JavaScript阻止冒泡事件
事件兼容 event = event ? event : window.event; js停止冒泡· window.event? window.event.cancelBubble = true : ...
- BCD 码、Gray 码、ASCII 码都是什么呢?
BCD 码:即(Binary Coded Decimal)码,也称为 8421 码,是十进制代码中最常见的一种.每一位的 1 代表的十进制数称为这一位的权.BCD 码中每一位的权都是固定不变的,它属于 ...
- git远程操作相关命令(remote 、push、fetch 、pull)
git remote 为了便于管理,Git要求每个远程主机都必须指定一个主机名.为了便于管理,Git要求每个远程主机都必须指定一个主机名. git remote[查看创库名] git remote 在 ...