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. 树状数组 gcd 查询 Different GCD Subarray Query

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  2. 【java基础 11】java集合框架学习

    导读:本篇博客主要是从整体上了解java的集合框架,然后主要介绍几个自己在项目中用到的结构,比如说:hashtable.hashmap.hashset.arraylist等! 一.宏观预览 从宏观上看 ...

  3. [UOJ#127][BZOJ4195][NOI2015]程序自动分析

    [UOJ#127][BZOJ4195][NOI2015]程序自动分析 试题描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2, ...

  4. BZOJ 3309 DZY Loves Math ——莫比乌斯反演

    枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...

  5. excel批量导入数据库SQL server

    思路: 第一是文件上传,可以参照Jakarta的FileUpload组件,用普通的Post也就行了.第二是Excel解析,用JSL或者POI都行第三是数据保存,这个应该简单吧,一个循环,一行对应一条数 ...

  6. docker使用 命令

    Dockerfile FROM golang:alpine3. AS build-stage WORKDIR /go/src/mypro.exportReport COPY . . RUN go bu ...

  7. max file descriptors [4096] for elasticsearch proess is too low, increase to at least [65536]

    修改文件 /etc/security/limits.conf 加入以下两行: sonar hard nofile 65536 sonar soft nofile  65536 #备注:sonar这里是 ...

  8. Windows下配置scrapy需要MVC的14.0版本(转载)

    转载于--http://blog.csdn.net/MrWilliamVs/article/details/77130965 杨煜冬煜杨的博客,他的博客比较杂,Java.Python都有--http: ...

  9. Chrome 浏览器安装Vue插件方法 (十分详细)

    博主最近在研究Vue,无奈新手想安装Chrome的Vue插件,整理下安装流程: 1.首先去github下载vue.zip文件插件(还有npm安装方法这里就不介绍了自行百度)下载地址:https://g ...

  10. IO流(1)-键盘录入学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件

    1.先写一个Student类 public class Student { private String name; private int chinese; private int math; pr ...