Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP
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.
ababa
aba
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的更多相关文章
- 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 Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #282 (Div. 1) A. Treasure 水题
A. Treasure Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/494/problem/A ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- Codeforces Round #303 (Div. 2) B. Equidistant String 水题
B. Equidistant String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...
- 【Codeforces Round #423 (Div. 2) C】String Reconstruction
[Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...
- Codeforces Round #604 (Div. 2) A. Beautiful String
链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...
- Codeforces Round #656 (Div. 3) D. a-Good String
题目链接:https://codeforces.com/contest/1385/problem/D 题意 一个小写字母串称为 $c-good\ string$,如果至少满足以下条件之一: 字符串长度 ...
随机推荐
- 记录,javascript中对象的属性名是字符串,却可以不用引号
问题描述:今日看书,里面介绍js的对象的属性名是包括空字符串在内的所以字符串 问题来了,我们平时定义的对象如下,是没有引号""or’'的 var someone = { f ...
- POJ 1416 DFS
题目翻译: 公司现在要发明一种新的碎纸机,要求新的碎纸机能够把纸条上的数字切成最接近而不超过target值.比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分,分别是1.2 ...
- Excel数据迁移到SQL Server遇到的若干问题
系统环境为:Windows Server 2008 r2 SQL Server 2012 1.建表过程中,如果用图形化的方式修改表结构会遇到问题: '不允许保存更改.您所做的更改要求删除并重新创建以下 ...
- markdown 计算器
计算器 分四种运算(加减乘除).括号.去除最后括号.验证等式是否计算完成 bracket = re.compile(r'\([^()]+\)') # 找括号 multiplys = re.compil ...
- java控制台输入输出字符串
一.实例说明 本实例通过输入流(System.in)实现从控制台接受用户输入信息,并将该信息输出到控制台. 运行效果如下图: 二.实现代码 三.要点说明 该实例的关键就是用到了System类的输入流, ...
- vim 常用操作笔记
跳转最后一行 :$ 或 shift+g 跳转第一行 :1 或 gg 设置自动换行 :set wrap 设置不自动换行 :set nowrap
- poj 2955 Brackets 【 区间dp 】
话说这题自己折腾好久还是没有推出转移的公式来啊------------------ 只想出了dp[i][j]表示i到j的最大括号匹配的数目--ค(TㅅT)------------------- 后来搜 ...
- jquery选择器的一些处理
本文不讨论用jquery选择器具体怎么选择页面元素,而讨论选择元素后后的一些处理 jquery的选择器选择元素的时候,即使没有选择到指定的对象,页面并不会报错,例子: <!doctype htm ...
- Centos 7 安装图形化界面
Centos 7 默认是没有图形化界面的,但我们很多人在习惯了 Windows 的图形化界面之后,总是希望有一个图形化界面从而方便我们使用,这里介绍一下 CentOS7安装图形化桌面系统的方法. 一. ...
- JXLS生成excel并自定义单元格样式
本人最近需要每天统计数据表,并每周一发送统计结果的邮件,所以写了个springboot老自动完成工作.项目地址为:https://github.com/707293891/springboot 其中某 ...