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[ ...
随机推荐
- POJ 3190 Stall Reservations贪心
POJ 3190 Stall Reservations贪心 Description Oh those picky N (1 <= N <= 50,000) cows! They are s ...
- Making the Grade (bzoj1592)
题目描述 FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了N ...
- POJ-1915 Knight Moves (BFS)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26952 Accepted: 12721 De ...
- 数据结构二叉树的所有基本功能实现。(C++版)
本人刚学数据结构,对树的基本功能网上找不到C++代码 便自己写了一份,贴出方便大家进行测试和学习. 大部分功能未测试,如有错误或者BUG,请高手们指教一下,谢谢. 结点声明: BinTreeNode. ...
- [Scoi2014]方伯伯的玉米田 二维树状数组+动态规划
考试最后半个小时才做这道题.十分钟写了个暴力还写挂了..最后默默输出n.菜鸡一只. 这道题比较好看出来是动规.首先我们要明确一点.因为能拔高长度任意的一段区域,所以如果从i开始拔高,那么一直拔高到n比 ...
- oracle 内连接,外连接
--内连接 inner join ... on --左外连接 left join ... on --右外连接 right join ... on 列: select * from stud ...
- 【有意思的BUG】反转的水印
今天无意中看到一个图集,翻着翻着感觉到哪儿不对劲.是的,水印打反了,怎么会出现这样的局面我也不知道,可能就是手抖了吧. 通过与懂这方面知识的人请教,120%的可能是因为图片本身就自带水印,而不是因为后 ...
- CSS3 基础(1)——选择器详解
CSS3选择器详解 一. 属性选择器 在CSS3中,追加了三个属性选择器分别为:[att*=val].[att^=val]和[att$=val],使得属性选择器有了通配符的概念. 选择器 示例 描述 ...
- WAMP环境 apache 2.4.23 局域网访问
参考了很多大神的分享解答, 现在不知道是参考的哪个人的 如有抄袭 侵权请告知, 加上链接或者删除!!! 最近想按个分屏提高学习代码效率, 晚上突然想到可以用本本连接WiFI 然后通过 局域网访问PC ...
- 使用ant插件打包生成jar文件
1.环境 新版Eclipse中已经集成了Ant插件,在Preferences中可以看到.(2017版) 旧版本的Eclipse如果没有集成Ant插件,安装配置方法在文档最后. 2.使用教程 (1)新建 ...