Classic Quotation

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

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
 分析:首先,对于某一对(l,r),我们可以求出答案为preg l + suf r,pref l;
   其中preg表示前缀l中T的个数,pref l表示匹配完前缀l指针所在位置,suf r,pref l表示从r开始的后缀中从pref l指针开始匹配得到的T的个数;
   因为要求所有的贡献和,l<=L,r>=R,所以考虑前缀和与后缀和;
   ans=∑​i=1​~L​​∑​j=R~​n​ ​preg​i​​ + suf​j,pref​i​​​​=(n−R+1)preg​L​​+∑​i=0~​m−1​​ s​L,i​​×suf​R,i​​
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define mod 998244353
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls rt<<1
#define rs rt<<1|1
#define all(x) x.begin(),x.end()
const int maxn=5e4+;
const int N=5e4+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=qmul(f,p,mo)%mo;p=qmul(p,p,mo)%mo;q>>=;}return f;}
int n,m,k,t,nxt[maxn],nxt1[][];
ll pref[maxn],preg[maxn],s[maxn][],suf[maxn][];
char a[maxn],b[maxn];
void init(char *a,char *b)
{
for(int i=;i<=n;i++)
{
pref[i]=preg[i]=;
for(int j=;j<=m;j++)
{
s[i][j]=suf[i][j]=;
}
}
nxt[]=-;
int j=-;
for(int i=;i<=m;i++)
{
while(!(j==-||b[j]==b[i]))j=nxt[j];
nxt[i+]=++j;
}
j=;
for(int i=;i<n;i++)
{
while(!(j==-||a[i]==b[j]))j=nxt[j];
if(i)preg[i]=preg[i-];
pref[i]=++j;
s[i][j]++;
if(j==m)preg[i]++;
}
for(int i=;i<n;i++)
{
preg[i]+=preg[i-];
for(int j=;j<=m;j++)
{
s[i][j]+=s[i-][j];
}
}
for(int i=;i<=m;i++)
{
for(int j='a';j<='z';j++)
{
int k=i;
while(!(k==-||j==b[k]))k=nxt[k];
nxt1[i][j-'a']=k+;
}
}
for(int i=n-;i>=;i--)
{
for(int j=;j<=m;j++)
{
int tmp=nxt1[j][a[i]-'a'];
suf[i][j]+=suf[i+][tmp];
if(tmp==m)suf[i][j]++;
}
}
for(int i=n-;i>=;i--)
{
for(int j=;j<=m;j++)
{
suf[i][j]+=suf[i+][j];
}
}
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
scanf("%s%s",a,b);
init(a,b);
while(k--)
{
int x,y;
scanf("%d%d",&x,&y);
ll ret=(n-y+)*preg[x-];
for(int i=;i<=m;i++)
{
ret+=s[x-][i]*suf[y-][i];
}
printf("%lld\n",ret);
}
}
return ;
}

2017 Multi-University Training Contest - Team 4 Classic Quotation的更多相关文章

  1. 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】

    FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】

    Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  3. 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】

    CSGO Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  4. 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】

    Ch’s gift Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  5. 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  6. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】

    Function Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  8. 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】

    KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

随机推荐

  1. Java Socket编程 深入讲解?你之前真的学懂了吗

    很多人学习Socket往往会碰到这样哪样的问题,可以看看下面,加深理解.能看懂多少看懂多少. Socket是Java网络编程的基础,了解还是有好处的,这篇文章主要讲解Socket的基础编程.Socke ...

  2. Spring中常用的注解,你知道几个呢?

    今天给大家分享下Spring中一般常用的注解都有哪些.可能很多人做了很长是了但有些还是不知道一些注解,不过没有关系,你接着往下看. Spring部分 1.声明bean的注解 @Component 组件 ...

  3. Windows(7/8/10)搭建Elasticsearch 6.x版本

    今天公司用到了Elasticsearch ,记录一下单机版搭建的流程. 首先我们来看下什么是Elasticsearch : ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分 ...

  4. RT-Thread 设备驱动I2C浅析及使用

    由于 I2C 可以控制多从机的属性,设备驱动模型分为  I2C总线设备(类似与Linux里面的I2C适配器) + I2C从设备: 系统I2C设备驱动主要实现 I2C 总线设备驱动,而具体的I2C 从设 ...

  5. centos安装 memcached

    .安装 yum -y update yum install -y libevent libevent-devel yum install -y memcached 2.配置 vi /etc/sysco ...

  6. Sum It Up -- 深搜 ---较难

    每一行都是一组测试案例   第一个数字 表示总和 第二个数字表示 一共有几个可用数据  现在 按照从小到大的顺序   输出  那些数字中若干数字之和为总和的  信息 /. 很好很明显的  遍历痕迹 , ...

  7. scrapy 简单操作

    1.创建一个简单的scrapy项目 scrapy startproject search(项目名称)按照提示cd searchscrapy genspider serachname search.co ...

  8. 【Leetcode 86】 Partition List

    问题描述: 给定一个list, 将所有小于x的node放到左边,剩下的保持原样. 问题解决: 闲的无聊,用c++和python都做了一遍. 代码如下: # Definition for singly- ...

  9. Java学习笔记_网络(2)

    单向的客户端发送服务器端接收并显示,没有使用多线程 import javax.swing.*; import java.awt.*; import java.io.*; import java.net ...

  10. 给定的逗号分隔的数字字符串转换为Table

    --将给定的逗号分隔的数字字符串转换为Table CREATE FUNCTION [dbo].[fu_Split](@strString nvarchar(4000)) RETURNS @Result ...