题目给出一个长为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;
}

  

最新文章

  1. 添加AppWidget功能
  2. html5 浏览器端数据库
  3. python面向对象基础
  4. 2015多校.MZL&#39;s endless loop(欧拉回路的机智应用 || 构造)
  5. spring使用elasticsearch 5.x
  6. django-CSRF verification failed. Request aborted
  7. lintcode:两数组的交 II
  8. IOS UIActivityIndicatorView 等待指示器
  9. PHP学习系列(1)&mdash;&mdash;字符串处理函数(5)
  10. offsetParent 到底是哪一个?
  11. 2016 SyScan360 国际前瞻信息安全会议 多角度探讨信息安全
  12. CRUD操作(20161017)
  13. 【leetcode】148. Sort List
  14. 到底什么是集群&amp;分布式
  15. Neutron vxlan network--L2 Population
  16. vue-router的学习
  17. 【书籍推荐】java初级到中级书籍推荐
  18. Python 字符编码简记
  19. Angular 个人深究(五)【外部包引用 Leaflet 简单实用】
  20. 【BZOJ1293】[SCOI2009]生日礼物(单调队列)

热门文章

  1. 对ThreadLocal的源码解读
  2. Object C学习笔记7-字符串NSString之一
  3. Java JDK下载安装及配置
  4. 工作之路---记录LZ如何在两年半的时间内升为PM
  5. Node JS World
  6. linux分区满了,如何进行扩容
  7. Appium 安装详细版教程
  8. eclipse以MapReduce本地模式运行程序
  9. bitcoin PoW原理及区块创建过程
  10. XSS(Cross Site Script)