HDU4622_Reincarnation
题目给出一个长为2000的字符串,和10000询问,每次询问从第l到第r个字符中间有多少个不同的子串。
其实,全部预处理。f[i][j]表示从i到j个字符的子串数。重构2000遍SAM。
对于新加入的字符,其所对应的last点,新增加的新子串数位step[last]-step[pre[last]]。原因嘛,自己想想就知道了。
不知道hdu上那种100ms+的代码是咋写出来的,求指教。
召唤代码君:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 10010
using namespace std; int next[maxn][26],pre[maxn],step[maxn];
int f[2002][2002];
char s[maxn];
int Q,N,last,T,l,r;
int p,q,np,nq; int add()
{
N++;
for (int i=0; i<26; i++) next[N][i]=0;
pre[N]=step[N]=0;
return N;
} int insert(int x)
{
p=last,np=add(),step[np]=step[last]+1,last=np;
while (p!=-1 && next[p][x]==0) next[p][x]=np,p=pre[p];
if (p==-1) return step[np];
q=next[p][x];
if (step[q]==step[p]+1) { pre[np]=q; return step[np]-step[pre[np]]; }
nq=add(),step[nq]=step[p]+1,pre[nq]=pre[q];
for (int i=0; i<26; i++) next[nq][i]=next[q][i];
pre[np]=pre[q]=nq;
while (p!=-1 && next[p][x]==q) next[p][x]=nq,p=pre[p];
return step[np]-step[pre[np]];
} int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%s",s+1);
for (int i=1; s[i]; i++)
{
N=-1;N=add();last=0;pre[0]=-1;
for (int j=i; s[j]; j++) f[i][j]=insert(s[j]-'a');
}
for (int i=1; s[i]; i++)
for (int j=i+1; s[j]; j++) f[i][j]+=f[i][j-1];
scanf("%d",&Q);
while (Q--)
{
scanf("%d%d",&l,&r);
printf("%d\n",f[l][r]);
}
}
return 0;
}
HDU4622_Reincarnation的更多相关文章
随机推荐
- Spring restTemplate
什么是RestTemplate RestTemplate是Spring提供的用于访问Rest服务的客户端,提供了多种便捷访问远程HTTP服务的方法,能够大大提高客户端的编写效率. 项目中注入Res ...
- 【JUC源码解析】SynchronousQueue
简介 SynchronousQueue是一种特殊的阻塞队列,该队列没有容量. [存数据线程]到达队列后,若发现没有[取数据线程]在此等待,则[存数据线程]便入队等待,直到有[取数据线程]来取数据,并释 ...
- Win7搭建FTP服务器
“控制面板” -> “程序和功能” -> “打开或关闭Windows 功能”: 1.展开“Internet 信息服务” 2.勾选“Internet Information Services ...
- CTF MISC-USB流量分析出题记录
USB流量分析 USB接口是目前最为通用的外设接口之一,通过监听该接口的流量,可以得到很多有意思的东西,例如键盘击键,鼠标移动与点击,存储设备的明文传输通信.USB无线网卡网络传输内容等. 1.USB ...
- Python中remove,pop,del的区别
先上题:写出最终打印的结果 a = [1, 2, 3, 4] for x in a: a.remove(x) print(a) print("=" * 20) b = [1, 2, ...
- SICP读书笔记 3.1
SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...
- yocto-sumo源码解析(六): setup_bitbake
1. 创造日志handler: 在status_only模式,不需要日志以及UI # Ensure logging messages get sent to the UI as events hand ...
- vue入门之单文件组件
介绍 在很多 Vue 项目中,我们使用 Vue.component 来定义全局组件,紧接着用 new Vue({ el: '#container '}) 在每个页面内指定一个容器元素. 这种方式在很多 ...
- linux简单命令常用随记
//查看网络信息 ifconfig //修改ip地址 ifconfig eth0 123.123.123.123 netmask 255.255.255.0 //网关设置 route add defa ...
- Ambiguous mapping. Cannot map 'labelInfoController' method
使用springboot项目中,启动时出现Ambiguous mapping. Cannot map 'labelInfoController' method , 原因是,@RequestMappin ...