题目链接

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
 
 
题意:有小写字符串s和t,现在在s中去掉连续子串后,剩余s[1…i] 和 s[j…n] 连在一起构成一个新s串,计算t串在新s串中出现了几次。现在q次询问,每次输入L和R,去掉连续子串后s[1…i]和s[j...n]拼接成新串s,1<=i<=L && R<=j<=n,求t串在这些新串中出现的次数和?
 
思路:
          
 
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=;
char s[N],t[];
int pre[N],num[N][];
int suf[N][];
int next1[];
int next2[][],flag[][];
int n,m,q; void KMP()
{
next1[]=;
for(int i=,k=; i<m; ++i)
{
while(k> && t[i]!=t[k]) k=next1[k-];
if(t[i]==t[k]) k++;
next1[i]=k;
}
} void cal()
{
memset(flag,,sizeof(flag));
for(int i=;i<m;i++)
{
for(int j=;j<;j++)
{
char x=j+'a';
int k=i;
while(k> && t[k]!=x) k=next1[k-];
if(t[k]==x) k++;
next2[i][j]=k;
if(k==m) flag[i][j]=,next2[i][j]=next1[m-];
}
} memset(pre,,sizeof(pre));
memset(num,,sizeof(num));
for(int i=,k=;i<n;i++)
{
while(k>&&t[k]!=s[i]) k=next1[k-];
if(t[k]==s[i]) k++;
if(k==m) pre[i]++,num[i][next1[m-]]=;
else num[i][k]=;
pre[i]+=pre[i-];
}
for(int i=;i<n;i++)
for(int j=;j<m;j++)
num[i][j]+=num[i-][j];
for(int i=;i<n;i++) pre[i]+=pre[i-];///前缀和; memset(suf,,sizeof(suf));
for(int i=n-;i>=;i--)
{
int x=s[i]-'a';
for(int j=;j<m;j++)
suf[i][j]=flag[j][x]+suf[i+][next2[j][x]];
}
for(int j=;j<m;j++) ///后缀和;
for(int i=n-;i>=;i--)
suf[i][j]+=suf[i+][j];
} int main()
{
int T; cin>>T;
while(T--)
{
scanf("%d%d%d",&n,&m,&q);
scanf("%s%s",s,t);
KMP();
cal();
while(q--)
{
int L,R; scanf("%d%d",&L,&R);
LL ans=(LL)pre[L-]*(LL)(n-R+);
for(int i=;i<m;i++)
{
ans+=(LL)num[L-][i]*(LL)suf[R-][i];
}
printf("%lld\n",ans);
}
}
return ;
}
/**
2342
8 3 3463
abcababc
abc
8 3 234
aabbcccbbb
aaabb 4
10 3 23
ababcababc
aba
3 5
*/
 

hdu 6068--Classic Quotation(kmp+DP)的更多相关文章

  1. HDU 5763 Another Meaning (kmp + dp)

    Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...

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

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

  3. 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)

    2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...

  4. HDU 6068 Classic Quotation KMP+DP

    Classic Quotation Problem Description When online chatting, we can save what somebody said to form h ...

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

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

  6. HDU 4035:Maze(概率DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description   When w ...

  7. HDU 3565 Bi-peak Number(数位DP)题解

    题意:我们定义每一位先严格递增(第一位不为0)后严格递减的数为峰(比如1231),一个数由两个峰组成称为双峰,一个双峰的价值为每一位位数和,问L~R双峰最大价值 思路:数位DP.显然这个问题和pos有 ...

  8. HDU 4169 Wealthy Family(树形DP)

    Problem Description While studying the history of royal families, you want to know how wealthy each ...

  9. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

随机推荐

  1. 【YII】Yii入门

    1. 入门博客 http://blog.csdn.net/zm2714/article/category/1359776/2 2. 创建运行demo http://blog.csdn.net/zhou ...

  2. QT QT creator QTsdk的区别

    Qt是一个跨平台的C++图形用户界面应用程序框架.它提供给应用程序开发者建立艺术级的图形用户界面所需的所用功能.Qt是完全面向对象的,很容易扩展,并且允许真正地组件编程. QT Creator 跨平台 ...

  3. angular JS中使用jquery datatable添加ng-click事件

    'use strict'; app.controller('DataTableCtrl', function ($scope, $compile) { $scope.show = function ( ...

  4. 微信小程序开发基础知识总结

    微信小程序在无论在功能.文档及相关支持方面,都是优于前面几种微信账号类型,它提供了很多原生程序才有的接口,使得我们的小程序在很多方面突破H5页面应用的限制,更加接近原生程序的功能,因此微信小程序具有很 ...

  5. node调用phantomjs-node爬取复杂页面

    什么是phantomjs phantomjs官网是这么说的,'整站测试,屏幕捕获,自动翻页,网络监控',目前比较流行用来爬取复杂的,难以通过api或正则匹配的页面,比如页面是通过异步加载.phanto ...

  6. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)RESTful API实战笔记(接口设计及Java后端实现)

    写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...

  7. java中Comparable和Comparator两种比较器的区别

    Comparable和Comparator接口都是为了对类进行比较,众所周知,诸如Integer,double等基本数据类型,java可以对他们进行比较,而对于类的比较,需要人工定义比较用到的字段比较 ...

  8. 初学Python之 字符串 索引 分片

    字符串是字符的有序集合,可以通过其位置来获得具体的元素. 在python中,字符串中的字符是通过索引来提取的,索引从0开始. python可以取负值,表示从末尾提取,最后一个为-1,倒数第二个为-2, ...

  9. <未来世界的幸存者> 读后感(现实篇和职业篇)

    摘要: 前几天有幸看到阮老师的 <未来世界的幸存者)>,花了几晚的时间阅读完毕,内心受到了很大的触动,现在将感觉不错的地方记录下. 职业篇 1. 为什么雇佣制度对工人不利? 雇佣制度是一种 ...

  10. getNextElement( )函数——获取下一个特定的元素节点

    function getNextElement(node){ //定义getNextElement()函数 if (node.nodeType==){ //条件:如果node参数nodetype属性为 ...