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. [Codeforces Round495A] Sonya and Hotels

    [题目链接] https://codeforces.com/contest/1004/problem/A [算法] 直接按题意模拟即可 时间复杂度 :O(NlogN) [代码] #include< ...

  2. MySQL 1045登录失败(转)

    http://blog.csdn.net/bbirdsky/article/details/8134528# 当你登录MySQL数据库出现:Error 1045错误时(如下图),就表明你输入的用户名或 ...

  3. 2017北京国庆刷题Day1 morning T2

    T2火柴棒 (stick) Time Limit:1000ms   Memory Limit:128MB 题目描述 众所周知的是,火柴棒可以拼成各种各样的数字.具体可以看下图: 通过2根火柴棒可以拼出 ...

  4. [Swift通天遁地]八、媒体与动画-(6)使用开源类库快速实现滑入动画

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. CSS元素超出部分滚动,并隐藏滚动条

    方法一, 利用 css 3 的新特性  -webkit-scrollbar, 但是这种方式不兼容 火狐 和 IE <!DOCTYPE html> <html> <head ...

  6. Kafka详解与总结(五)

    Kafka持久化 1. 概述 Kafka大量依赖文件系统去存储和缓存消息.对于硬盘有个传统的观念是硬盘总是很慢,这使很多人怀疑基于文件系统的架构能否提供优异的性能.实际上硬盘的快慢完全取决于使用它的方 ...

  7. leetCode----day02---- 买卖股票的最佳时机 II

    要求: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必 ...

  8. P2068 统计和

    P2068 统计和 这题真的非常水了 如果不会 右转[模板]树状数组 2 基本上是一模一样的 #include <bits/stdc++.h> #define lowbit(x) x&am ...

  9. UE4源码版食用要记

    UE4源码版和预编译版不能共享工程,这和插件版是一样的. 一般来说我都是在VS中生成编辑器,于编辑器中添加新类,VS中编辑代码. 编译引擎的时候编译配置使用的是devepolmenteditor.开发 ...

  10. MySQL实现当前数据表的所有时间都增加或减少指定的时间间隔

    DATE_ADD() 函数向日期添加指定的时间间隔. 当前表所有数据都往后增加一天时间: UPDATE ACT_BlockNum SET CreateTime = DATE_ADD(CreateTim ...