QAQ http://acm.hdu.edu.cn/showproblem.php?pid=6068

  2017 Multi-University Training Contest - Team 4 - 1005

  贴一张官方题解

  

  其中S可以由O(n*m)的求前缀得到。

  求suf贼难,淦。

  求suf主要是通过类似DP的思想

  其中ismatch[i][j]表示T的匹配指针为i,匹配到字母j的时候,是否新匹配了一个完整的T串。

  nxt2[i][j]表示,当T的匹配指针为i,匹配到字母j的时候,T的指针转移到哪个位置

  然后未求前缀的suf数组就可以倒着DP出来了

  (思路来自某官方题解和某其他博客的题解)

  

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath> using namespace std; typedef long long ll; const int M=5e4+44;
const int N=244; int n,m,qur;
char S[M],T[N];
int nxt[N];
int preg[M];
int s[M][N];
int suf[M][N];
int ismatch[N][N],nxt2[N][N]; void init()
{
int i,j,k,tmp,tmpi,tmpj; //get the nxt of T
nxt[1]=0; k=0;
for(j=2;j<=m;j++)
{
while(k && T[k+1]!=T[j])
k=nxt[k];
if(T[k+1]==T[j])
k++;
nxt[j]=k;
} //get preg,s
j=0;
tmp=0;
memset(s,0,sizeof(s));
for(i=1;i<=n;i++)
{
while(j && T[j+1]!=S[i])
j=nxt[j];
if(T[j+1]==S[i])
j++;
if(j==m)
{
tmp++;
j=nxt[j];
}
preg[i]=tmp; s[i][j]++;
}
for(i=2;i<=n;i++)
for(j=0;j<m;j++)
s[i][j]+=s[i-1][j];
for(i=2;i<=n;i++)
preg[i]+=preg[i-1]; //get suf
for(i=0;i<m;i++)
for(j='a';j<='z';j++)
{
ismatch[i][j]=0;
k=i;
while(k!=0 && T[k+1]!=j)
k=nxt[k];
if(T[k+1]==j)
k++;
if(k==m)
{
ismatch[i][j]=1;
k=nxt[k];
}
nxt2[i][j]=k;
}
for(i=0;i<m;i++)
suf[n+1][i]=0;
for(i=n;i>=1;i--)
for(j=0;j<m;j++)
suf[i][j]=ismatch[j][S[i]]+suf[i+1][nxt2[j][S[i]]];
for(i=n;i>=1;i--)
for(j=0;j<m;j++)
suf[i][j]+=suf[i+1][j];
} void solve()
{
int i,j,L,R;
ll ans;
while(qur--)
{
scanf("%d%d",&L,&R);
ans=1ll*(n-R+1)*preg[L];
for(i=0;i<m;i++)
ans+=1ll*s[L][i]*suf[R][i];
printf("%lld\n",ans);
}
} int main()
{
int i,j,cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d%d",&n,&m,&qur);
scanf("%s%s",S+1,T+1);
init();
solve();
}
return 0;
}

  

hdu 6068 Classic Quotation的更多相关文章

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

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

  2. HDU 6068 Classic Quotation KMP+DP

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

  3. 2017 Multi-University Training Contest - Team 4 Classic Quotation

    Classic Quotation Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Othe ...

  4. hdu 6068--Classic Quotation(kmp+DP)

    题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...

  5. hdu 5761 Rower Bo 物理题

    Rower Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5761 Description There is a river on the Ca ...

  6. hdu 1015 Safecracker 水题一枚

    题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein s ...

  7. hdu 5188 zhx and contest [ 排序 + 背包 ]

    传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

随机推荐

  1. 【0.4】mysql版本特性(5.6-8.0)【转】

    转自:http://blog.itpub.net/15498/viewspace-2650661/ MySQL 5.6 1).支持GTID复制 2).支持无损复制 3).支持延迟复制 4).支持基于库 ...

  2. ES简介及特点

    1.ES是什么? ES是一个高度可伸缩的开源的全文检索和分析引擎,它允许你以近实时的方式快速存储.搜索.分析大量数据,ES是基于Lucence开发,隐藏其复杂性,提供了简单易用的restful api ...

  3. sql lesson21homework

    2017-08-15 18:03:17 mysql> show databases;+--------------------+| Database           |+---------- ...

  4. Pr PS 笔记

    1. 保存窗口配置    窗口-新建工作区 2. 添加快捷键  编辑-自定义快捷键 3. 添加关键帧,需要下拉轨道 4. 关闭PR声音 5. 视频稳定器 选中素材,右键选择嵌套,嵌套后在子序列把视频画 ...

  5. Const指针 、 指向const的指针 、引用、指针

    1. const指针和 指向const的指针 指向const的指针: 不允许通过指针来改变其指向的const值 const double *cptr *cptr = 42;  // error! 指针 ...

  6. vue中params-解决换路由不刷新问题

    因为依赖路由的params参数获取写在created生命周期里面,因为相同路由二次甚至多次加载的关系 没有达到监听,退出页面再进入另一个文章页面并不会运行created组件生命周期,导致文章数据还是第 ...

  7. rabbitmq 连接报错 An unexpected connection driver error occured

    转自:https://blog.csdn.net/zht741322694/article/details/82801873 在服务器上安装了一个RabbitMq,并新创建了一个用户授予了管理员角色, ...

  8. Java高并发程序设计学习笔记(九):锁的优化和注意事项

    转自:https://blog.csdn.net/dataiyangu/article/details/87612028 锁优化的思路和方法减少锁持有时间减小锁粒度锁分离锁粗化举个栗子举个栗子锁消除虚 ...

  9. count(*),count(1),count(字段)

    如果null参与聚集运算,则除count(*)之外其它聚集函数都忽略null. 如:    ID     DD     1      e     2    null    select  count( ...

  10. 4.ID主键生成策略

    保证唯一性(auto_increment) 一.xml方式 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping P ...