[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数组: 如果在本文中看见 ...
随机推荐
- SqlServer 2014 Enterprise 企业版安装程序下载与安装教程
场景 SqlServer2014 企业版比标准版 多一些功能,比如在企业版中能使用分区函数,但是在标准版Express 中就不能使用. SqlServer 2014 企业版安装包下载: 关注公众号: ...
- Python连载38-协程、可迭代、迭代器、生产者消费者模型
一.生产者消费者模型 import multiprocessing from time import ctime def consumer(input_q): print("Into con ...
- 安全性测试:OWASP ZAP 2.8 使用指南(二):ZAP基础操作
ZAP桌面应用 ZAP桌面应用由以下元素组成: 1. 菜单栏 – 提供多种自动化和手动工具的访问 2. 工具栏 – 提供快速访问最常用组件的用户接口 3. 树结构窗口 – 展示被测网站树结构和脚 ...
- 实战spring自定义属性(schema)
关于spring自定义属性(schema) 在开发Dubbo应用的时候,我们会在xml中做以下类似的配置: <dubbo:application name="dubbo_service ...
- 松软科技课堂:sqlserver--数据类型
SQL Server 数据类型(文章来源:松软科技www.sysoft.net.cn) Character 字符串: 数据类型 描述 存储 char(n) 固定长度的字符串.最多 8,000 个字符. ...
- 让我们一起学习如何使用AIDL,它其实并不难(Android)
前言 该篇文件讲述的是AIDL最基本的使用(创建.调用),关于对于AIDL更深的认识,在后续的随笔中,会持续与大家分享并探讨. 正文 AIDL的定义(什么是AIDL?) AIDL的应用场景(AIDL可 ...
- 年年有余之java求余的技巧集合
背景 传说里玉皇大帝派龙王马上降雨到共光一带,龙王接到玉皇大帝命令,立马从海上调水,跑去共光施云布雨,但粗心又着急的龙王不小心把海里的鲸鱼随着雨水一起降落在了共光,龙王怕玉皇大帝责怪,灵机一动便声称他 ...
- Oracle中RMAN基本命令教程
一.target--连接数据库 1.本地: [oracle@oracle ~]$ rman target / 2.远程: [oracle@oracle ~]$ rman target sys/orac ...
- Django REST Framework之频率限制
开放平台的API接口调用需要限制其频率,以节约服务器资源和避免恶意的频繁调用 使用 自定义频率限制组件:utils/thottle.py class MyThrottle(BaseThrottle): ...
- Centos7搭建Scrapy爬虫环境
写在前面 因为之前的爬虫环境一直是部署在我自己本地的电脑上的,最近,写了一个监控别人空间的爬虫,需要一直线上24小时运行,所有就打算云服务器上部署环境,也捣鼓了好一会才弄好,还是有一些坑,这里先记录一 ...