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. 【Python】【demo实验7】【练习实例】【完全平方数相关】

    题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. ...

  2. Flask:上下文管理

    1. werkzurg from werkzur.serving import run_simple def run(environ,start_response): reuturn [b'hello ...

  3. 汉明码(hamming code)

    hamming code用于磁盘RAID 2中, 关于汉明码的讲解可以看这篇博文,介绍的很详细.最重要是最后的结论: 汉明码属于分组奇偶校验,P4P2P1=000,说明接收方生成的校验位和收到的校验位 ...

  4. java字符串大小写转换

    String test="SHA34cccddee";    System.out.println(test.toUpperCase());//小写转大写 String test= ...

  5. 【ExtJs】ext前台中的日期控件传输时间到后台的转换保存过程

    //前台日期选择框 {fieldLabel:, padding: ',afterLabelTextTpl: required,allowBlank: false,format: 'Y-m-d H:i: ...

  6. git 多账户添加ssh秘钥

    生成秘钥的步骤: ssh-keygen -t rsa -C "xxxx@qq.com" 添加秘钥 在不同的域中添加相同的秘钥是没有问题的,比如 github.com / code. ...

  7. PotPlayer直播源分享

    添加直播源方法: 央视CCTV1综合HD-1,rtsp://113.136.42.45:554/PLTV/88888888/224/3221226087/10000100000000060000000 ...

  8. redis编译和安装出现错误

    redis编译到时候出现错误,记录一下原因 1.下载redis,https://redis.io/download ,一般选择稳定版本,稳定版的版本号是偶数,当前最新版本是5.0.4,Linux可直接 ...

  9. UDS报文解读

    UDS(Unified Diagnostic Services,统一的诊断服务)诊断协议是ISO 15765 和ISO 14229 定义的一种汽车通用诊断协议,位于OSI模型中的应用层,它可在不同的汽 ...

  10. python函数:装饰器、修正、语法糖、有参装饰器、global与nonlocal

    一.装饰器 二.装饰器修正1 三.装饰器修正2 四.装饰器的语法糖 五.有参.无参装饰器 六.global与nonlocal 一.装饰器 ''' 1 什么是装饰器 器=>工具 装饰=>指的 ...