Codeforces 494B Obsessive String
http://www.codeforces.com/problemset/problem/494/B
题意:给出两个串S,T,求有几种将S分成若干个子串,满足T都是这若干个子串的子串。
思路:f[n]代表前n个字符来划分,有多少种划分方式,sum[i]代表1到i的f的和,转移就是:对于i这个位置,可以不选,因此首先
f[i]=f[i-1],然后若是选了i,那一定至少是在有匹配位置的左边开始匹配,假设L为小于i的最右边的匹配位置,则
F[i]+=ΣF[j] (1<=j<=L-1),然后也有可能自己组成一个新的独立的串,那么就要让F[i]+=L。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
char s[],t[];
int len1,len2,f[],sum[],p[],pd[];
const int Mod=;
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void kmp(){
p[]=;int j=;
for (int i=;i<=len2;i++){
while (j>&&t[j+]!=t[i]) j=p[j];
if (t[j+]==t[i]) j++;
p[i]=j;
}
j=;
for (int i=;i<=len1;i++){
while (j>&&t[j+]!=s[i]) j=p[j];
if (t[j+]==s[i])j++;
if (j==len2){
pd[i]=i-len2+;
}
}
for (int i=;i<=len1;i++)
if (!pd[i]) pd[i]=pd[i-];
}
int main(){
scanf("%s",s+);scanf("%s",t+);
len1=strlen(s+);len2=strlen(t+);
kmp();
for (int i=;i<=len1;i++){
f[i]=f[i-];
int l=pd[i];
if (!l) continue;
(f[i]+=(sum[l-]+l)%Mod)%=Mod;
sum[i]=(sum[i-]+f[i])%Mod;
}
printf("%d\n",f[len1]);
}
Codeforces 494B Obsessive String的更多相关文章
- CodeForces 494B Obsessive String ——(字符串DP+KMP)
这题的题意就很晦涩.题意是:问有多少种方法,把字符串s划分成不重叠的子串(可以不使用完s的所有字符,但是这些子串必须不重叠),使得t串是所有这些新串的子串.譬如第一个样例,"ababa&qu ...
- [Codeforces-div.1 494B]Obsessive String
[CF-div.1 B]Obsessive String 题目大意 两个字符串\(S,T\),求划分方案数使得一个集合中两两划分不相交且划分都包含字符串\(T\) 试题分析 kmp先求出那个位置匹配. ...
- 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 ...
- [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 --DP
题意: 给两个串S,T,问能找出多少的S的(a1,b1)(a2,b2)..(ak,bk),使Sa1---Sb1,...Sak---Sbk都包含子串T,其中k>=1,且(a1,b1)...(ak, ...
- CF 494B 【Obsessive String】
很有趣的一道题 这道题提议很难懂,其实就是让你求合法的集合数目.合法的集合定义为: 1.集合中的所有串都是s的子串,且互不重叠 2.集合中的所有串都含有子串t. 看到网上很多题解说要用kmp,但我就不 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- Codeforces 827E Rusty String - 快速傅里叶变换 - 暴力
Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consist ...
- Codeforces 797C - Minimal string
C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...
随机推荐
- 关于android:focusable属性
http://www.cnblogs.com/Gaojiecai/archive/2013/06/18/3142783.html Android属性 android:focusableInTouchM ...
- c++ windows下declspec
一.declspec #ifdef STATIC_LIBS #define DLL_API static #else #define DLL_API __declspec (dllexport) #e ...
- 前Google人谈团队管理:针对不同员工的情境管理法和如何选择合理的团队规模
本文作者Tomasz Tunguz是Redpoint Ventures的风险投资人,曾在Google担任产品经理并参与过AdSense项目. 我有一个朋友,他创立了一家很成功的公司,而且还在迅速发展. ...
- python部分排序算法(网友提供)
// 冒泡排序 def bubble(x,n): '''This function orders the original items x x is list,n is the length o ...
- python高级编程之(类级):子类内建类型
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...
- Android Studio 导入Eclipse工程
eclipse:workspace对应多个project:而android studio是project对应多个module:故,在android studio中的工程project实际上是eclip ...
- H5移动端性能优化
概述 1. PC优化手段在Mobile侧同样适用 2. 在Mobile侧我们提出三秒种渲染完成首屏指标 3. 基于第二点,首屏加载3秒完成或使用Loading 4. 基于联通3G网络平均338KB/s ...
- 爆出错误:The Geometry has no Z values
ArcGis添加地图标注,爆出错误:The Geometry has no Z values 解决方法如下: public bool AddFeature( ESRI.ArcGIS.Geometry. ...
- WCF之旅
转载:创建一个简单的WCF程序 http://www.cnblogs.com/artech/archive/2007/02/26/656901.html Endpoint Overview http ...
- 数据结构——左高树
一.扩充二叉树 考察一棵二叉树,它有一类特殊的节点叫做外部节点( external node),用来代替树中的空子树,其余节点叫做内部节点( internal node).增加了外部节点的二叉树被称为 ...