hdu 6068--Classic Quotation(kmp+DP)

After doing lots of such things, Little Q finds out that string T occurs as a continuous substring of S′ very often.
Now given strings S and T, Little Q has k questions. Each question is, given L and R, Little Q will remove a substring so that the remain parts are S[1..i] and S[j..n], what is the expected times that T occurs as a continuous substring of S′ if he choose every possible pair of (i,j)(1≤i≤L,R≤j≤n) equiprobably? Your task is to find the answer E, and report E×L×(n−R+1) to him.
Note : When counting occurrences, T can overlap with each other.
In each test case, there are 3 integers n,m,k(1≤n≤50000,1≤m≤100,1≤k≤50000) in the first line, denoting the length of S, the length of T and the number of questions.
In the next line, there is a string S consists of n lower-case English letters.
Then in the next line, there is a string T consists of m lower-case English letters.
In the following k lines, there are 2 integers L,R(1≤L<R≤n) in each line, denoting a question.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=;
char s[N],t[];
int pre[N],num[N][];
int suf[N][];
int next1[];
int next2[][],flag[][];
int n,m,q; void KMP()
{
next1[]=;
for(int i=,k=; i<m; ++i)
{
while(k> && t[i]!=t[k]) k=next1[k-];
if(t[i]==t[k]) k++;
next1[i]=k;
}
} void cal()
{
memset(flag,,sizeof(flag));
for(int i=;i<m;i++)
{
for(int j=;j<;j++)
{
char x=j+'a';
int k=i;
while(k> && t[k]!=x) k=next1[k-];
if(t[k]==x) k++;
next2[i][j]=k;
if(k==m) flag[i][j]=,next2[i][j]=next1[m-];
}
} memset(pre,,sizeof(pre));
memset(num,,sizeof(num));
for(int i=,k=;i<n;i++)
{
while(k>&&t[k]!=s[i]) k=next1[k-];
if(t[k]==s[i]) k++;
if(k==m) pre[i]++,num[i][next1[m-]]=;
else num[i][k]=;
pre[i]+=pre[i-];
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
num[i][j]+=num[i-][j];
for(int i=;i<n;i++) pre[i]+=pre[i-];///前缀和; memset(suf,,sizeof(suf));
for(int i=n-;i>=;i--)
{
int x=s[i]-'a';
for(int j=;j<m;j++)
suf[i][j]=flag[j][x]+suf[i+][next2[j][x]];
}
for(int j=;j<m;j++) ///后缀和;
for(int i=n-;i>=;i--)
suf[i][j]+=suf[i+][j];
} int main()
{
int T; cin>>T;
while(T--)
{
scanf("%d%d%d",&n,&m,&q);
scanf("%s%s",s,t);
KMP();
cal();
while(q--)
{
int L,R; scanf("%d%d",&L,&R);
LL ans=(LL)pre[L-]*(LL)(n-R+);
for(int i=;i<m;i++)
{
ans+=(LL)num[L-][i]*(LL)suf[R-][i];
}
printf("%lld\n",ans);
}
}
return ;
}
/**
2342
8 3 3463
abcababc
abc
8 3 234
aabbcccbbb
aaabb 4
10 3 23
ababcababc
aba
3 5
*/
hdu 6068--Classic Quotation(kmp+DP)的更多相关文章
- HDU 5763  Another Meaning (kmp + dp)
		Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ... 
- HDU 6068 - Classic Quotation   |  2017 Multi-University Training Contest 4
		/* HDU 6068 - Classic Quotation [ KMP,DP ] | 2017 Multi-University Training Contest 4 题意: 给出两个字符串 S[ ... 
- 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)
		2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ... 
- HDU 6068 Classic Quotation KMP+DP
		Classic Quotation Problem Description When online chatting, we can save what somebody said to form h ... 
- [HDOJ5763]Another Meaning(KMP, DP)
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ... 
- HDU 4035:Maze(概率DP)
		http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description When w ... 
- HDU 3565 Bi-peak Number(数位DP)题解
		题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有 ... 
- HDU 4169 Wealthy Family(树形DP)
		Problem Description While studying the history of royal families, you want to know how wealthy each ... 
- hdu 3336  count the string(KMP+dp)
		题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ... 
随机推荐
- C语言指针2(空指针,野指针)
			//最近,有朋友开玩笑问 int *p *是指针还是p是指针还是*p是指针,当然了,知道的都知道p是指针 //野指针----->>>指没有指向一个地址的指针(指针指向地址请参考上一 ... 
- 坏块管理(Bad Block Management,BBM)
			看了很多坏块管理的文章,加上自己的理解,把整个坏块管理做了个总结. 坏块分类 1.出厂坏块 又叫初始坏块,厂商会给点最小有效块值(NVB,mininum number of valid blocks) ... 
- [技术] 如何正确食用cnblogs的CSS定制
			用过cnblogs的估计都知道cnblogs提供了相对比较开放的个性化选项,其中最为突出的估计就是页面CSS定制了.但是没学过Web前端的人可能并不会用这个东西... 所以我打算在此分享一些定制CSS ... 
- 初学 Python(十五)——装饰器
			初学 Python(十五)--装饰器 初学 Python,主要整理一些学习到的知识点,这次是生成器. #-*- coding:utf-8 -*- import functools def curren ... 
- sort排序错乱问题
			对于sort排序 之前就遇到过这种问题 不过没有在意 今天遇到 就找了一下原理 在这种sort排序中可以看到排序几乎没有什么问题 就是5比较特殊 会在20是的后面 ~ sort()方法开始的时候会 ... 
- HDU5744 Keep On Movin (思维题,水题)
			Problem Description Professor Zhang has kinds of characters and the quantity of the i-th character i ... 
- vue基础一
			一.vue的编写步骤 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ... 
- JS 无法清除Cookie的解决方法
			JS 无法清除Cookie的解决方法 项目中使用sdmenu.js时,需要在登录时清除Cookie,而sdmenu默认是会保存Cookie的 下面是sdmenu.js保存Cookie的方法 doc ... 
- jquery左右切换的无缝滚动轮播图
			1.HTML结构: <head> <script type="text/javascript" src="../jquery-1.8.3/jquery. ... 
- Python3 知识库
			Python3 标准库概览 Python3 日期和时间 Python3 JSON 数据解析 Python3 XML解析 Python3 多线程 Python3 SMTP发送邮件 Python3 网络编 ... 
