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 ...
随机推荐
- /tmp/orbit-oracle/目录inode耗尽
[root@iZ25zpeock2Z orbit-oracle]# cd /[root@iZ25zpeock2Z /]# du -cks * |sort -nr|head -n 20du: canno ...
- Response.write()弹出窗口的问题!
今天偶然发现在.NET中使用Javascript语句弹出窗口时发现一个小小的问题! 例子如下: 1: Response.Write ("<script languge=javascri ...
- 南昌网络赛C.Angry FFF Party
南昌网络赛C.Angry FFF Party Describe In ACM labs, there are only few members who have girlfriends. And th ...
- IntelliJ IDEA Spring boot devtools 实现热部署
一.spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能就是自动部署新代码. 二.原理 使用了两个ClassLoader,一个ClassLoader用来加载那些不会变 ...
- Css文字效果
1.文字样式:font-family 字体:sans-serif(字体醒目):serif(笔画的开始和结束有额外的修饰部分,易读性较强) *页面设计最好有多个备选字体 2.文字大小:font-size ...
- 向PHP发送HTTP-Get请求
1.get.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- JavaScript使用纯函数避免bug
纯函数 一.纯函数 定义:纯函数是指不依赖并且不修改其作用域之外的函数.通过以下几个示例来认识纯函数: var a = 10; //纯函数 function foo(num){ return num ...
- Python 之 random模块
Python中的random模块用于生成随机数.1.random.random() #用于生成一个0到1的随机浮点数:0<= n < 1.0>>> random.ran ...
- HttpClient 源码阅读
在项目中常用的HttpClient,与我们非常的亲密,为了能处理遇到的Http问题,我们应该去了解里面的机制和实现. 官方文档:http://hc.apache.org/httpcomponents- ...
- Active Directory participation features and security extensions
Participation in the Active Directory Samba 3.0 series, as well as the OS since Windows 2000, is pos ...