题目链接

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. call和apply和bind区别

    call和apply特征一样 都是用来调用函数 立即调用 但是可以在调用函数的同时 通过第一个参数指定函数内部this的指向 call 调用的时候 参数必须以参数列表的形式进行传递 也就是以逗号分隔的 ...

  2. 那些年,用C#调用过的外部Dll

    经常有人找到我咨询以前在csdn资源里分享的dll调用.算算也写过N多接口程序.翻一翻试试写篇随笔. 明华IC读写器DLL 爱迪尔门锁接口DLL 通用OPOS指令打印之北洋pos打印机dll 明泰非接 ...

  3. Presto向分区表快速插入数据时出现'target directory already exists'的原因

    因为项目使用Presto作为ETL使用,需要将关系库中的数据导入到Hive中.目前关系库中的数据每天导入一次,在Hive中以天为间隔创建新的分区.思路是正确的,但是在使用的过程中,发现将少量关系库中的 ...

  4. grunt 的安装和简单使用

    安装Grunt命令行 npm install -g grunt-cli 创建package.json,如果有package.json包,可以直接npm install加载依赖组件 npm init 安 ...

  5. 求m区间内的最小值

    洛谷P1440 求m区间内的最小值 ............................................................................... 以上 ...

  6. 自定义Git之忽略特殊文件

    有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们,比如保存了数据库密码的配置文件啦,等等,每次git status都会显示Untracked files ...,有强迫症的童鞋心里肯定 ...

  7. 无法远程连接服务器上的mysql

    使用mysql管理工具连接服务器删过得mysql,显示连接被拒绝,但是在服务器上是可以登录mysql的. 无法远程连接通常以下几种情况: 首先,关闭mysql.        service mysq ...

  8. akoj-1074-人见人爱A^B

    人见人爱A^B Time Limit:1000MS  Memory Limit:65536K Total Submit:91 Accepted:55 Description 求A^B的最后三位数表示的 ...

  9. 【有意思的BUG】反转的水印

    今天无意中看到一个图集,翻着翻着感觉到哪儿不对劲.是的,水印打反了,怎么会出现这样的局面我也不知道,可能就是手抖了吧. 通过与懂这方面知识的人请教,120%的可能是因为图片本身就自带水印,而不是因为后 ...

  10. JavaScript版—贪吃蛇小组件

    最近在学习JavaScript,利用2周的时间看完了<JavaScript高级编程>,了解了Js是一门面向原型编程的语言,没有像C#语言中的class,也没有私有.公有.保护等访问限制的级 ...