题目链接

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. 【CSS3】使用CSS3线性渐变实现图片闪光划过效果(转)

    原文:http://www.nowamagic.net/librarys/veda/detail/2600 资料参考: http://www.cnblogs.com/lhb25/archive/201 ...

  2. Mysql的JDBC

    Java程序可以通过JDBC链接数据库,通过JDBC可以方便的访问数据库,不必为特定的数据库编写专门的程序. 需要先配置mysql-connector-java-5.0.8-bin.jar 使用JDB ...

  3. [COGS 1065] 绿豆蛙的归宿

    先贴题面w 1065. [Nescafe19] 绿豆蛙的归宿 ★   输入文件:ldfrog.in   输出文件:ldfrog.out   简单对比时间限制:1 s   内存限制:128 MB 随着新 ...

  4. ExtJs的expand和collapse

    最近在研究ExtJs的窗口组件(Ext.window),关于扩展显示expand和折叠显示collapse的一点心得记录一下,以便后查. var win2 = new Ext.window({ id ...

  5. Linux 下实时查看日志

    Linux 下实时查看日志 cat /var/log/*.log 如果日志在更新,如何实时查看 tail -f /var/log/messages 还可以使用 watch -d -n 1 cat /v ...

  6. PAT1074 Reversing Linked List (25)详细题解

    02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. [算法题] Search in Rotated Sorted Array

    题目内容 本题来源LeetCode Suppose an array sorted in ascending order is rotated at some pivot unknown to you ...

  8. python编程基础知识—列表(一)

    1 列表 用[]来表示列表,并用逗号分隔其中的元素.如: B=['trek','cannondale','redline','specialized'] print(B) ['trek', 'cann ...

  9. RMAN基础恢复测试

    --RMAN恢复测试实战   RMAN> list backup;   using target database control file instead of recovery catalo ...

  10. 《Java从入门到放弃》入门篇:hibernate中的多表对应关系

    hibernate中的对应关系其实就是数据库中表的对应关系, 就跟某些电影中的某些场景是一样一样滴. 比如可以是一男一女,还可以是一男多女, 更可以是多男一女,最后最后最后还可以是多男多女!!! 有些 ...