题目给出一个长为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的更多相关文章

随机推荐

  1. Object C学习笔记4-内存管理

    Object-C的内存管理和.NET有些不一样,.NET的内存回收机制是使用GC自动处理回收,而Object-C本质上还是C语言,所以很多时候还是需要手动去管理内存回收. 1. Object-C生成一 ...

  2. How to: Provide Credentials for the Dashboards Module when Using External Data Sources

    XAF中使用dashboard模块时,如果使用了sql数据源,可以使用此方法提供连接信息 https://www.devexpress.com/Support/Center/Question/Deta ...

  3. fiddler对安卓APP进行抓包

    操作流程: 1.fiddler导出ca证书 操作路径: Tools -> Fiddler Options -> HTTPS -> Export Fiddler Root Certif ...

  4. Unity在OpenGL模式下Shader编译报错

    报错信息 GLSL compilation failed: 0(21) : error C7528: OpenGL reserves names containing '__' 双击报错VS自动打开V ...

  5. CocoStuff—基于Deeplab训练数据的标定工具【一、翻译】(未完)

    一.CocoStuff简介 CocoStuff是一款为deeplab设计的,运行在Matlab中的语义标定工具,其标定结果和结合Deeplab训练出的结果均为mat文件格式,该项目源码已在github ...

  6. 利用saltstack一键部署多台zookeeper

    以上是saltstack上面sls文件存放zookeeper的路径和文件 以上是入口文件把文件夹做成包 重要安装配置在zoo.sls,以下是该sls的内容 zookeeper: file.manage ...

  7. python—启动自带shell时报错(丢失api-ms-win-crt-runtime-l1-1-0.dll)已解决

    备注: 有的伙伴安装完1后重启,问题可以解决,summer儿在安装完1依然未能解决,于是又进行了2的安装再次重启后问题解决!! 1,安装vc-redist.x64,微软官网搜索免费下载,安装后重启. ...

  8. NodeJS http模块

    Node.js提供了http模块,用于搭建HTTP服务端和客户端. 创建Web服务器 /** * node-http 服务端 */ let http = require('http'); let ur ...

  9. CentOS赋予一个普通用户root权限

    http://www.linuxidc.com/Linux/2012-07/64530.htm

  10. to_char

    to_date(to_char(to_date(#{conds.currentTime,jdbcType=VARCHAR},'YYYY-MM-DD hh24:mi:ss'),'hh24:mi:ss') ...