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$,如果至少满足以下条件之一: 字符串长度 ...
随机推荐
- SwiftUI 官方教程(四)
SwiftUI 官方教程(四) 4. 自定义 Image View 搞定名称和位置 view 后,我们来给地标添加图片. 这不需要添加很多代码,只需要创建一个自定义 view,然后给图片加上遮罩.边框 ...
- Tomcat安全设置与优化详解(非原创)
一.Tomcat简介二.Tomcat安全设置三.Tomcat优化四.参考文章 一.Tomcat简介 Tomcat 是 Apache软件基金会下的一个免费.开源的WEB应用服务器,它可以运行在 Li ...
- LeetCode Weekly Contest 18B
1. 496. Next Greater Element I 暴力的话,复杂度也就1000 * 1000 = 1e6, 在1s的时限内完全可以. 当然,有许多优化方法,利用stack维护递减序列的方法 ...
- WPF黑色背景下常用控件样式
平时工作用 自己整理的 代码等找到合适的上传空间在进行同步
- Centos7下Docker的使用
一.安装Docker 1.1.查看原有系统是否已经安装docker yum list installed | grep docker 1.2.如果有则不需要继续安装,想重新安装,先卸载 yum -y ...
- js 根据固定位置获取经纬度--腾讯地图
1.首先引入jq 和 腾讯地图js <script src="../js/jQuery.js"></script> <script charset=& ...
- Verilog之$sreadmemb
1 Memories Memories file format is shown below, the address is specified as @ <address> in he ...
- C++调用Matlab 注意事项
前言:脑残的使用了C++调用Matlab,没想到Matlab的使用者的智商还真TMD不一般, 竟然有这样的 plot(x_Abnorm_index,Vec2(Abnorm_index),'sb','l ...
- RabbitMQ学习之集群模式
由于RabbitMQ是用erlang开发的,RabbitMQ完全依赖Erlang的Cluster,因为erlang天生就是一门分布式语言,集群非常方便,但其本身并不支持负载均衡.Erlang的集群中各 ...
- Swift 字典 Dictionary基本用法
import UIKit /* 字典的介绍 1.字典允许按照某个键访问元素 2.字典是由两部分组成, 一个键(key)集合, 一个是值(value)集合 3.键集合是不能有重复的元素, 值集合可以有重 ...