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:// ...
随机推荐
- 2017年 湘潭邀请赛(湖南)or 江苏省赛
这套题是叉姐出的,好难啊,先扫一遍好像没有会做的题了,仔细一想好像D最容易哎 Super Resolution Accepted : 112 Submit : 178 Time Limit : 1 ...
- 最短路POJ 1062 昂贵的聘礼
C - 昂贵的聘礼 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit St ...
- 自动化运维之shell通配符,转义符,和元字符(二)
1 shell通配符 通配符看起来有点象正则表达式语句,但是它与正则表达式不同的,不能相互混淆.把通配符理解为shell特殊代号字符就可. 二.shell元字符 shell除了有通配符之外,由shel ...
- 【Luogu】P2331最大子矩阵(DP)
题目链接 这题的状态转移方程真是粗鄙. f[i][j][k]表示前i行用了j个矩阵状态为k的时候的最大值. k=0:两列都不选. k=1:取左弃右. k=2:选右弃左. k=3:左右都选,但分属于两个 ...
- BZOJ1924 [Sdoi2010]所驼门王的宝藏 【建图 + tarjan】
题目 输入格式 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室,类型为 Ti.Ti ...
- java.util.ResourceBundle 用法小介
java中读取配置文件的信息可以采用properties这个类,但是当遇到国际化问题的时候还是不好解决,因而还是最好使用 ResourceBundle这个类,其实ResourceBundle本质上和P ...
- readonly和disabled的异同
好久木有写博客了,今来逛逛 话说今天搞form表单的时候,主管让俺把手机号设成只读的.当时我就...咳咳,然后我就问了下万能的百度君,果断还是有解决方法的嘛,那么,今就谈谈readonly和disab ...
- HTML网页滚动加载--mark一下
console控制台: >: function stroll(){ window.scrollTo(, document.body.scrollHeight); }; >: window. ...
- 将数字转换成Excel表头格式的字母序号
/** * 从0开始算起,0-25转A-Z * @param num * @return Character.valueOf((char)((num-1)+65))+&quo ...
- Python种使用Excel
今天用到Excel的相关操作,看了一些资料,借此为自己保存一些用法. 参考资料: python excel 的相关操作 python操作excel之xlrd python操作Excel读写--使用xl ...