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. iOS第三方网络图片加载- SDWebImage笔记(转)

    SDWebImage托管在github上.https://github.com/rs/SDWebImage 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下 ...

  2. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 【莫队算法】

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  3. JS控制背景音乐 没有界面

    建立一个HTML5页面,放置<audio>标签,设置音频文件源,设置循环播放.准备两张图片,分别表示开启和暂停背景音乐两种状态,可以点击. <audio id="music ...

  4. CI(CodeIgniter)框架中的增删改查操作

    我们创建一个模型( 项目目录/models/),请注意:模型名与文件名相同且必须继承数据核心类CI_Model,同时重载父类中的构造方法 CodeIgniter的数据函数类在 \models\User ...

  5. ElasticSearch 中 REST API 详解

    本文主要内容: 1 ElasticSearch常用的操作 2 ElasticSearchbulk命令 ES REST API elasticsearch支持多种通讯,其中包括http请求响应服务,因此 ...

  6. Perl语言--一些关于赋值、引用的东西

    引用详解 一.定义引用有两种: 1.用斜线“\” 2.匿名引用 1.用反斜线的引用:数组.哈希.标量 数组的引用 my @array = (1,2,3); my $aref = \@array; 哈希 ...

  7. 一个关于 jquery 和 php 的 jsonp 例子(与后台PHP成功通信)

    <script> $(document).ready(function(){ $.ajax({ url:'http://localhost/test/jsonp.php', dataTyp ...

  8. itext A4纸张横向创建PDF

    import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException; import com.lowagie ...

  9. Java8期间及持续时间

    原文:http://www.yiibai.com/java8/java8_periodduration.html 使用Java8,两个专门类引入来处理时间差. Period - 处理有关基于时间的日期 ...

  10. ios实现下载图片的裁减和显示

    使用如下的方法可以裁减的同时保证了不丢失像素. - (void)connectionDidFinishLoading:(NSURLConnection *)connection{    // Set ...