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. VUE-挂载点-实例成员-数据-过滤器-文本指令-事件指令-属性指令-表单指令-01

    目录 路飞项目 vue vue 导读 vue 的优势 渐进式框架 引入 vue 实例成员 - 挂载点 el js 对象(字典)补充 实例成员 - 数据 data 实例成员 - 过滤器 filters ...

  2. Spring 的 AOP 概述和底层实现

    Spring 的 AOP 概述和底层实现 1. 什么是 AOP AOP (Aspect Oriented Programing),即面向切面编程 AOP 采取横向抽取机制,取代了传统纵向继承体系重复性 ...

  3. 10-Perl 循环

    1.Perl 循环一般情况下,语句是按顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推.有的时候,可能需要多次执行同一块代码.编程语言提供了更为复杂执行路径的多种控制结构.循环语句允许 ...

  4. Spring 配置文件注入

    一.Spring配置文件注入 package com.zxguan.demo; public class Person { private String name; private int age; ...

  5. Java Web DNS域名解析

    一.什么是DNS DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串 ...

  6. JS基础_打印99乘法表

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 如何调试Python程序 通过IDLE

    在python3.3环境下 1.写一个简单地Python源文件,比如test.py,内容如下: import sys, osdef test(arg1, arg2): print "begi ...

  8. Oracle连接字符串总结(转)

    Oracle XE 标准连接 Oracle XE(或者"Oracle Database 10g Express Edition")是一个简单免费发布的版本. 以下是语法格式: Dr ...

  9. Winfrom 极简版贪吃蛇源码

    该源码是我在百度知识库借助前辈的的经验,加上自己的一点小改动写的一个非常简陋的贪吃蛇小程序.如果你们有更好的改动方案,欢迎评论. 进入主题吧! 1.创建一个桌面应运程序,拖一个定时器控件.这样,程序界 ...

  10. c3p0似有属性配置

    c3p0私有属性配置: 始化时创建的连接数,应在minPoolSize与maxPoolSize之间取值.默认为3 initialPoolSize=20 接池中保留的最大连接数.默认为15 maxPoo ...