[Codeforces-div.1 494B]Obsessive String
[CF-div.1 B]Obsessive String
题目大意
两个字符串\(S,T\),求划分方案数使得一个集合中两两划分不相交且划分都包含字符串\(T\)
试题分析
kmp先求出那个位置匹配。
然后怎么办呢?显然\(f_i\)表示考虑到第i个字符的答案。
传统思想:考虑这个地方加不加。
不加:\(f_{i-1}\)。
加的话分两种情况讨论,一种是加入之前的某一个集合,就是\(\sum_{j=1}^{pre_i-1} f_j\),还有就是自己创建一个集合:\(pre_{i-1}\)
\(pre_i\)代表\(i\)前面的匹配头位置在哪里,满足\(pre_i+lenT-1\leq i\)
代码和分析可能有出入,另外还需要前缀和优化。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define LL long long
inline int read(){
int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int INF = 2147483600;
const int MAXN = 100010;
const int Mod = 1e9+9;
int nxt[MAXN+1]; char S[MAXN+1],T[MAXN+1];
int pre[MAXN+1],s[MAXN+1],f[MAXN+1]; int M;
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%s",S+1); int lens=strlen(S+1);
scanf("%s",T+1); int lent=strlen(T+1); nxt[0]=0;
for(int i=2;i<=lent;i++){
int j=nxt[i-1]; while(j&&T[i]!=T[j+1]) j=nxt[j];
if(T[i]==T[j+1]) ++j; nxt[i]=j;
} int j=0;
for(int i=1;i<=lens;i++){
while(j&&S[i]!=T[j+1]) j=nxt[j];
if(T[j+1]==S[i]) ++j; pre[i]=pre[i-1];
if(j==lent) pre[i]=i,j=nxt[j];
}
for(int i=1;i<=lens;i++){
f[i]=f[i-1];
if(pre[i]) f[i]=(f[i]+s[pre[i]-lent]+pre[i]-lent+1)%Mod;
s[i]=(s[i-1]+f[i])%Mod;
} printf("%d\n",f[lens]%Mod);
return 0;
}
[Codeforces-div.1 494B]Obsessive String的更多相关文章
- 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
http://www.codeforces.com/problemset/problem/494/B 题意:给出两个串S,T,求有几种将S分成若干个子串,满足T都是这若干个子串的子串. 思路:f[n] ...
- 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 Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- [codeforces494B]Obsessive String
[codeforces494B]Obsessive String 试题描述 Hamed has recently found a string t and suddenly became quite ...
- 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 ...
随机推荐
- 取(m堆)石子游戏 HDU2176(Nim博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176 题目: Problem Description m堆石子,两人轮流取.只能在1堆中取.取完者胜. ...
- div 超出高度滚动条,超出宽度点点点
超出高度滚动条 style="width:230px; height: 180px; overflow: auto;" 超出宽度点点点 style="width: 220 ...
- 双关键字LIS
首先对于双关键字的LIS有一个比较暴力的方法,就是线段树套平衡树,我们把双关键字的LIS抽象成二维坐标系中的点,这样我们对于当前转移的点i(x,y),需要找的就是在(xx,yy)xx<x,yy& ...
- 2017-2018-1 《Linux内核原理与设计》第十二周作业
<linux内核原理与设计>第十二周作业 Sql注入基础原理介绍 分组: 和20179215袁琳完成实验 一.实验说明 SQL注入攻击通过构建特殊的输入作为参数传入Web应用程序,而这 ...
- 项目评审ppt的纲要
1.prd不能模糊,产品的问题全部明确 2.收益在哪里 3.设计体现业务4.怎样保证数据的前后协作5.异常如何处理6.技术解决的痛点7.对外部依赖8.性能指标预期(响应时间)9.
- Oracle 获取ddl语句
--得到所有表空间的ddl语句 SELECT DBMS_METADATA.GET_DDL('TABLESPACE', TS.tablespace_name)FROM DBA_TABLESPACES T ...
- STL之顺序容器 deque 动态数组
deque是一个动态数组,deque与vector非常类似,vector是一个单向开口的连续线性空间,deque则是双向开口的连续线性空间.两者唯一的区别是deque可以在数组的开头和末尾插入和删除数 ...
- dockerfile实例--安装nginx
[root@localhost ~]# vi Dockerfile //ADD FROM centos_with_net MAINTAINER frankie onez0714@.com RUN yu ...
- The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collectio
The content of element type "resultMap" must match "(constructor?,id*,result*,associa ...
- cf786a
title: CodeForces 786A Berzerk data: 2018-3-3 10:29:40 tags: 博弈论 bfs 无限 with draw copyright: true ca ...