Classic Quotation

Problem Description
When online chatting, we can save what somebody said to form his ''Classic Quotation''. Little Q does this, too. What's more? He even changes the original words. Formally, we can assume what somebody said as a string S whose length is n. He will choose a continuous substring of S(or choose nothing), and remove it, then merge the remain parts into a complete one without changing order, marked as S′. For example, he might remove ''not'' from the string ''I am not SB.'', so that the new string S′ will be ''I am SB.'', which makes it funnier.

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.

 
Input
The first line of the input contains an integer C(1≤C≤15), denoting the number of test cases.

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.

 
Output
For each question, print a single line containing an integer, denoting the answer.
 
Sample Input
1
8 5 4
iamnotsb
iamsb
4 7
3 7
3 8
2 7
 
Sample Output
1
1
0
0
 

题意:

  给两个字符串只包含小写字母,长度分别为n,m

  k个询问,每次询问给出一个L,R

  任意的 ( i , j ) ( 1 ≤ i ≤ L , R ≤ j ≤ n ) 删除S串范围(i+1,j-1)内的字符,求出T串在新串内出现的次数总和

题解:

  我还是照搬官方题解吧

  

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 6e4+, M = 2e2+,inf = 2e9; int zfail[N],ffail[N];
LL dp[N][M],f[N][M],sumdp[N][M],sumf[N][M],dp2[N][M],f2[N][M];
char a[N],b[N];
int n,m,k,T;
LL solve(int ll,int rr) {
LL ret = ;
ret += 1LL * sumdp[ll][m] * (n - rr + ) + 1LL * sumf[rr][] * (ll);
for(int i = ; i < m; ++i) {
ret += 1LL*dp2[ll][i] * f2[rr][i+];
}
return ret;
}
void init() {
for(int j = ; j <= m+; ++j) zfail[j] = ,ffail[j] = m+;
for(int i = ; i <= n+; ++i)
for(int j = ; j <= m+; ++j)
dp[i][j] = ,f[i][j] = ,sumdp[i][j] = ,sumf[i][j] = ;
int j = ;
for(int i = ; i <= m; ++i) {
while(j&&b[j+]!=b[i]) j = zfail[j];
if(b[j+] == b[i]) j++;
zfail[i] = j;
}
j = m+;
for(int i = m-; i >= ; --i) {
while(j<=m&&b[j-]!=b[i]) j = ffail[j];
if(b[j-] == b[i]) j--;
ffail[i] = j;
}
j = ;
for(int i = ; i <= n; ++i) {
while(j&&a[i]!=b[j+]) j = zfail[j];
if(b[j+] == a[i]) j++;
dp[i][j] += ;
}
j = m+;
for(int i = n; i >= ; --i) {
while(j<=m&&b[j-]!=a[i]) j = ffail[j];
if(b[j-] == a[i]) j--;
f[i][j] += ;
} for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
dp[i][j] += dp[i-][j];
sumdp[i][j] += sumdp[i-][j]+dp[i][j];
}
}
for(int i = n; i >= ; --i) {
for(int j = m; j >= ; --j) {
f[i][j] += f[i+][j];
sumf[i][j] += sumf[i+][j]+f[i][j];
}
}
} void init2() {
for(int i = ; i <= n+; ++i)
for(int j = ; j <= m+; ++j)
dp2[i][j] = ,f2[i][j] = ;
for(int i = ; i <= n+; ++i)
dp2[i][] = ,f2[i][m+] = ; for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
if(a[i] == b[j] && dp2[i-][j-])
dp2[i][j] = ;
}
}
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
dp2[i][j] += dp2[i-][j];
}
}
for(int i = n; i >= ; --i) {
for(int j = m; j >= ; --j) {
if(a[i] == b[j] && f2[i+][j+])
f2[i][j] = ;
}
}
for(int i = n; i >= ; --i) {
for(int j = m; j >= ; --j) {
f2[i][j] += f2[i+][j];
}
}
} int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d%d%s%s",&n,&m,&k,a+,b+);
init();
init2();
while(k--) {
int L,R;
scanf("%d%d",&L,&R);
printf("%lld\n",solve(L,R));
}
}
return ;
}

HDU 6068 Classic Quotation KMP+DP的更多相关文章

  1. HDU 6068 - Classic Quotation | 2017 Multi-University Training Contest 4

    /* HDU 6068 - Classic Quotation [ KMP,DP ] | 2017 Multi-University Training Contest 4 题意: 给出两个字符串 S[ ...

  2. hdu 6068 Classic Quotation

    题 QAQ http://acm.hdu.edu.cn/showproblem.php?pid=6068 2017 Multi-University Training Contest - Team 4 ...

  3. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

  4. HDU 5763 Another Meaning KMP+DP

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Time Limit: 2000/1000 MS (Java/ ...

  5. hdu 6068--Classic Quotation(kmp+DP)

    题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...

  6. [kmp+dp] hdu 4628 Pieces

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Ot ...

  7. [HDOJ5763]Another Meaning(KMP, DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...

  8. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  9. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

随机推荐

  1. 九度oj 题目1367:二叉搜索树的后序遍历序列

    题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 输入: 每个测试案例包括2行: 第一行为1个整数 ...

  2. Shell脚本学习指南 [ 第三、四章 ] 查找与替换、文本处理工具

    摘要:第三章讨论的是编写Shell脚本时经常用到的两个基本操作.第四章总共介绍了约30种处理文本文件的好用工具. 第三章 查找与替换 概括:本章讨论的是编写Shell脚本时经常用到的两个基本操作:文本 ...

  3. [luoguP2053] [SCOI2007]修车(最小费用最大流)

    传送门 网络流的建图真的好难! 将一个点拆分成多个点的思想还需要加强. 题解 代码和题解中的图略不一样. #include <queue> #include <cstdio> ...

  4. 刷题总结——教主的魔法(bzoj3343)

    题目: Description 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. ...

  5. 使用镜像源安装EASY_INSTALL和PIP教程

    使用easy_install和pip可以让python的模块的安装和管理变得非常方便.我一般在新的Linux系统上,先easy_install pip然后就用pip安装其他的模块了. 不过,在国内用官 ...

  6. 【bzoj1299】[LLH邀请赛]巧克力棒 博弈+模拟

    Description TBL和X用巧克力棒玩游戏.每次一人可以从盒子里取出若干条巧克力棒,或是将一根取出的巧克力棒吃掉正整数长度.TBL先手两人轮流,无法操作的人输. 他们以最佳策略一共进行了10轮 ...

  7. lnux 下 core文件

    1. core文件的简单介绍在一个程序崩溃时,它一般会在指定目录下生成一个core文件.core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的. 2. 开启或关闭core文件的生成用以下 ...

  8. PAT (Advanced Level) 1089. Insert or Merge (25)

    简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  9. codevs——2693 上学路线(施工)

    2693 上学路线(施工)  时间限制: 2 s  空间限制: 16000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 问题描述 你所在的城市街道好像一个 ...

  10. http状态码的分类

    状态码分类 转自http://www.cnblogs.com/TankXiao/archive/2013/01/08/2818542.html HTTP状态码被分为五大类, 目前我们使用的HTTP协议 ...