hdu 6068 Classic Quotation
题
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的更多相关文章
- HDU 6068 - Classic Quotation | 2017 Multi-University Training Contest 4
/* HDU 6068 - Classic Quotation [ KMP,DP ] | 2017 Multi-University Training Contest 4 题意: 给出两个字符串 S[ ...
- HDU 6068 Classic Quotation KMP+DP
Classic Quotation Problem Description When online chatting, we can save what somebody said to form h ...
- 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 ...
- hdu 6068--Classic Quotation(kmp+DP)
题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...
- hdu 5761 Rower Bo 物理题
Rower Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5761 Description There is a river on the Ca ...
- hdu 1015 Safecracker 水题一枚
题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein s ...
- hdu 5188 zhx and contest [ 排序 + 背包 ]
传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
随机推荐
- 【Python】【demo实验12】【练习实例】【列表的复制】
#!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- # 将一个列表复制到另外一个列表中: # 分析:可以使用[:] L = [0,3, ...
- python -- TypeError: 'module' object is not callable
文件: 代码: import pprintmessge = 'It was a bringht cold day in April,and the clocks were striking thrir ...
- PAT A1042 Shuffling Machine
自己思路,没通过 #include <cstdio> #define N 54 int main() { #ifdef ONLINE_JUDGE #else freopen("1 ...
- PHP给图片添加文字水印实例
PHP给图片添加文字水印实例,支持中文文字水印,是否覆盖原图,自定义设置水印背景色.文字颜色.字体等. 水印类water.class.php var $Path = "./"; / ...
- T100 —— 凭证打印时排序
capr110_g01,按扣账日期打印排序: 在adzp188——“字段”中加入pmds001的话,产生的代码的变量是:pmds_t_pmds001 :当在“群组”—“印出排序” 再添加pmds001 ...
- B2C电商平台开发心得(asp.net+bootstrap)
Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 html.css.javascript的,专为 web 应用设计,包含了移动设备优先的样式, 其响应式 ...
- git pull文件时和本地文件冲突 方法之一
1.先将本地修改存储起来 2.pull内容 3.还原暂存的内容 4.解决文件中冲突的的部分 打开 dsa.txt 文件手动解决冲突. 其中Updated upstream 和=====之间的内容就是p ...
- java技术面试之面试题大全
转载自:http://blog.csdn.net/lijizhi19950123/article/details/77679489 Java 面试知识点总结 本篇文章会对面试中常遇到的Java技术点进 ...
- 【Java】 BIO与NIO以及AIO分析
一.BIO与NIO以及AIO的概念 BIO是同步阻塞式的IO NIO是同步非阻塞的IO (NIO1.0,JDK1.4) AIO是非同步非阻塞的IO(NIO2.0,JDK1.7) 二.BIO简单分析 1 ...
- mint-ui下拉加载min和上拉刷新(demo实例)
<template> <div class="share"> <div class="header"> <div cl ...