[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的更多相关文章

  1. 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 ...

  2. Codeforces 494B Obsessive String

    http://www.codeforces.com/problemset/problem/494/B 题意:给出两个串S,T,求有几种将S分成若干个子串,满足T都是这若干个子串的子串. 思路:f[n] ...

  3. CodeForces 494B Obsessive String ——(字符串DP+KMP)

    这题的题意就很晦涩.题意是:问有多少种方法,把字符串s划分成不重叠的子串(可以不使用完s的所有字符,但是这些子串必须不重叠),使得t串是所有这些新串的子串.譬如第一个样例,"ababa&qu ...

  4. 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, ...

  5. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  6. [codeforces494B]Obsessive String

    [codeforces494B]Obsessive String 试题描述 Hamed has recently found a string t and suddenly became quite ...

  7. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  8. Codeforces Round #303 (Div. 2) B. Equidistant String 水题

    B. Equidistant String Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...

  9. 【Codeforces Round #423 (Div. 2) C】String Reconstruction

    [Link]:http://codeforces.com/contest/828/problem/C [Description] 让你猜一个字符串原来是什么; 你知道这个字符串的n个子串; 且知道第i ...

随机推荐

  1. 最短路之spfa系列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t ...

  2. setTimeout()和setInterval()方法的区别

    setTimeout(); //5秒后执行yourFunction(),只执行一次 setInterval(); //每隔5秒执行一次 1.setTimeout(funhander,time)的作用是 ...

  3. Android--hardwareAccelerated 硬件加速详解 android:largeHeap="true"

    做项目时,引导页面的ViewPager报了OOM异常,图片并不大,在清单文件Application节点中添加了两行代码就解决了这个问题 android:hardwareAccelerated=&quo ...

  4. 设计模式之Factory

    设计模式总共有23种模式这仅仅是为了一个目的:解耦+解耦+解耦...(高内聚低耦合满足开闭原则) 介绍: Factory Pattern有3种当然是全部是creational pattern. 1.S ...

  5. 寻找kernel32.dll的地址

    为了寻找kernel32.dll的地址,可以直接输出,也可以通过TEB,PEB等查找. 寻找TEB: dt _TEB nt!_TEB +0x000 NtTib : _NT_TIB +0x01c Env ...

  6. linux内核网络接收数据流程图【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-3141409.html 4.3 数据接收流程图   各层主要函数以及位置功能说明:          1)s ...

  7. FIS3 大白话【一】

    1.fis3可以用fis.set进行一些全局的配置,包括忽略文件.文件后缀处理类型.源码过滤等等,用fis3.get可以得到配置信息,详见: http://fis.baidu.com/fis3/doc ...

  8. HDU 6118 度度熊的交易计划 最大费用可行流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6118 题意:中文题 分析: 最小费用最大流,首先建立源点 s ,与超级汇点 t .因为生产一个商品需要 ...

  9. 从一个R语言案例学线性回归

    线性回归简介 如下图所示,如果把自变量(也叫independent variable)和因变量(也叫dependent variable)画在二维坐标上,则每条记录对应一个点.线性回规最常见的应用场景 ...

  10. jdbc简单小登陆demo

    package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultS ...