Codeforces 494B. Obsessive String KMP+DP

70 篇文章 0 订阅


dp[i]表示到第i个可以分的段数,dp[i]=dp[i-1]+ ( dp[j]+1 ) ( 0<=j<=near[i] ) near[i]是距离i最靠右的一个含有T串的位置

B. Obsessive String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Input consists of two lines containing strings s and t (1 ≤ |s|, |t| ≤ 105). Each string consists of lowercase Latin letters.

Output

Print the answer in a single line.

Sample test(s)
input
ababa
aba
output
5
input
welcometoroundtwohundredandeightytwo
d
output
274201
input
ddd
d
output
12

/* ***********************************************
Author        :CKboss
Created Time  :2015年03月20日 星期五 19时30分59秒
File Name     :CF494B.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=100100;
const int mod=1e9+7;

char s[maxn],t[maxn];
int fail[maxn];
int near[maxn];
int dp[maxn],sum[maxn];

void getfail(char* p,int* f)
{
	int m=strlen(p);
	f[0]=f[1]=0;
	for(int i=1;i<m;i++)
	{
		int j=f[i];
		while(j&&p[j]!=p[i]) j=f[j];
		f[i+1]=(p[i]==p[j])?j+1:0;
	}
}

void kmp(char* t,char* p,int* f)
{
	int n=strlen(t),m=strlen(p);
	getfail(p,f);
	int j=0;
	for(int i=0;i<n;i++)
	{
		while(j&&p[j]!=t[i]) j=f[j];
		if(p[j]==t[i]) j++;
		if(j==m) { near[i+1]=i-m+2; j=f[j]; }
	}
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	scanf("%s",s); scanf("%s",t);

	kmp(s,t,fail);
	int n=strlen(s),m=strlen(t);

	for(int i=1;i<=n;i++)
		if(near[i]==0) near[i]=near[i-1];

	for(int i=1;i<=n;i++)
	{
		dp[i]=dp[i-1];
		if(near[i])
		{
			dp[i]=(dp[i]+sum[near[i]-1]+near[i])%mod;
		}
		sum[i]=(sum[i-1]+dp[i])%mod;
	}
	printf("%d\n",dp[n]);
    
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值