HDU 6068 Classic Quotation KMP+DP
Classic Quotation

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.
8 5 4
iamnotsb
iamsb
4 7
3 7
3 8
2 7
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的更多相关文章
- HDU 6068 - Classic Quotation | 2017 Multi-University Training Contest 4
/* HDU 6068 - Classic Quotation [ KMP,DP ] | 2017 Multi-University Training Contest 4 题意: 给出两个字符串 S[ ...
- hdu 6068 Classic Quotation
题 QAQ http://acm.hdu.edu.cn/showproblem.php?pid=6068 2017 Multi-University Training Contest - Team 4 ...
- HDU 6153 A Secret ( KMP&&DP || 拓展KMP )
题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...
- HDU 5763 Another Meaning KMP+DP
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Another Meaning Time Limit: 2000/1000 MS (Java/ ...
- hdu 6068--Classic Quotation(kmp+DP)
题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...
- [kmp+dp] hdu 4628 Pieces
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 Reincarnation Time Limit: 6000/3000 MS (Java/Ot ...
- [HDOJ5763]Another Meaning(KMP, DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- POJ 3336 Count the string (KMP+DP,好题)
参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...
随机推荐
- Flutter 发布APK时进行代码/资源混淆的坑
Flutter 发布APK时进行代码/资源混淆的坑 @author ixenos 1. 关键点 proguard是Java的代码混淆工具,但是当用第三方库的时候,必须要告诉proguard不要检查,因 ...
- SQLSERVER 差异备份、全备份
--exec BackUPDatabase_LeeHG语句参数说明: -- 示例:exec BackUPDatabase_LeeHG '参数一','参数二','参数三','参数四','参数五',' 参 ...
- BASH重定向问题
APUE 3.5关于重定向有个容易迷惑人的问题: ./a.out > outfile 2>&1 ./a.out 2>&1 > outfile 问两者区别? in ...
- iOS代理转移
在控制器里面想调用一个视图的子视图的代理方法很简单 1.找到子视图的协议和代理属性 2.给你想调用的控件添加代理属性,遵守的协议和子视图的一样 3.重写代理属性的set方法
- BZOJ 3238 [Ahoi2013]差异 ——后缀自动机
后缀自动机的parent树就是反串的后缀树. 所以只需要反向构建出后缀树,就可以乱搞了. #include <cstdio> #include <cstring> #inclu ...
- POJ1635 树的最小表示法(判断同构)
Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, ther ...
- 【前端学习笔记】关于CSS通过一个块改变另一个块的样式
<body><div id="a" style="background:#0F0; height:100px; width:100px;"&g ...
- mode(BZOJ 2456)
Description 给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数. Input 第1行一个正整数n.第2行n个正整数用空格隔开. Output 一行一个正整数表 ...
- POJ 2279
线性DP 本题的正解是杨氏矩阵与钩子定理 但是这道题用DP的思想非常好 但是这样会MLE... #include <iostream> #include <cstdio> #i ...
- Codeforces891C. Envy
$n \leq 5e5$,$m \leq 5e5$的无向边权图,$q \leq 5e5$个询问,每次问一系列边是否能同时存在于某棵最小生成树上. 一条边在最小生成树上,当比他小的边都加入后,加入他会使 ...