\(\color{#0066ff}{ 题目描述 }\)

给定一个字符串,多次询问某一字串的f值

f(s)代表s的不同字串数量

\(\color{#0066ff}{输入格式}\)

第一行T,代表数据组数\(T\leq 5\)

每组数据第一行一个字符串\(1\leq len \leq 2000\)

然后一个数字m(\(1\leq m \leq 10000\)),表示有m个询问

接下来m行,每行两个整数l,r,表示询问[l,r]的字串的答案

\(\color{#0066ff}{输出格式}\)

对于每个询问,输出一行表示答案

\(\color{#0066ff}{输入样例}\)

2
bbaba
5
3 4
2 2
2 5
2 4
1 4
baaba
5
3 3
3 4
1 4
3 5
5 5

\(\color{#0066ff}{输出样例}\)

3
1
7
5
8
1
3
8
5
1

\(\color{#0066ff}{数据范围与提示}\)

本题不卡hash, 但是正解不是hash

\(\color{#0066ff}{ 题解 }\)

考虑没有询问的时候,对于查询不同字串个数,见一个SAM就没事了

本题询问有10000个,考虑优化

因为长度是2000的,\(O(n^2)\)显然可以

所以我们开一个二维数组暴力预处理出所有的ans, 然后\(O(1)\)查询

\(O(nq) \to O(n^2 + q)\)

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
char ch; int x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 5555;
struct SAM {
protected:
struct node {
node *ch[26], *fa;
int len, siz;
node(int len = 0, int siz = 0): fa(NULL), len(len), siz(siz) {
memset(ch, 0, sizeof ch);
}
};
node *root, *tail, *lst;
node pool[maxn];
public:
node *extend(int c) {
node *o = new(tail++) node(lst->len + 1, 1), *v = lst;
for(; v && !v->ch[c]; v = v->fa) v->ch[c] = o;
if(!v) o->fa = root;
else if(v->len + 1 == v->ch[c]->len) o->fa = v->ch[c];
else {
node *n = new(tail++) node(v->len + 1), *d = v->ch[c];
std::copy(d->ch, d->ch + 26, n->ch);
n->fa = d->fa, d->fa = o->fa = n;
for(; v && v->ch[c] == d; v = v->fa) v->ch[c] = n;
}
return lst = o;
}
void clr() {
tail = pool;
root = lst = new(tail++) node();
}
SAM() { clr(); }
}sam;
LL ans[2050][2050];
char s[maxn];
int main() {
for(int T = in(); T --> 0;) {
scanf("%s", s + 1);
int len = strlen(s + 1);
for(int i = 1; i <= len; i++) {
for(int j = i; j <= len; j++) {
auto o = sam.extend(s[j] - 'a');
ans[i][j] = ans[i][j - 1] + o->len - o->fa->len;
}
sam.clr();
}
for(int m = in(); m --> 0;) {
int l = in(), r = in();
printf("%lld\n", ans[l][r]);
}
}
return 0;
}

Reincarnation HDU - 4622的更多相关文章

  1. Reincarnation HDU - 4622 (后缀自动机)

    Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...

  2. hdu 4622 Reincarnation(后缀数组)

    hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...

  3. HDU 4622 Reincarnation Hash解法详解

    今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...

  4. HDU 4622 (后缀自动机)

    HDU 4622 Reincarnation Problem : 给一个串S(n <= 2000), 有Q个询问(q <= 10000),每次询问一个区间内本质不同的串的个数. Solut ...

  5. hdu 4622 Reincarnation

    http://acm.hdu.edu.cn/showproblem.php?pid=4622 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...

  6. hdu 4622 Reincarnation trie树+树状数组/dp

    题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...

  7. hdu 4622 Reincarnation SAM模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...

  8. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

  9. HDU 4622 Reincarnation(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...

随机推荐

  1. git教程(远程仓库和管理分支)

    在github上新建了一个仓库,然后相与本地的仓库联系起来 $ Git remote add origin https://github.com/liona329/learngit.git fatal ...

  2. Oracle Management Packs

    http://kerryosborne.oracle-guy.com/2008/10/oracle-management-packs/ There has been quite a bit of co ...

  3. 部署和调优 1.1 nfs部署和优化-1

    NFS服务会经常用到,用于在网络上共享存储.举一个例子来说明一下 NFS .假如有三台机器 A.B.C,它们需要访问同一个目录,目录中都是图片,传统的做法是把这些图片分别放到 A.B.C.但是,若使用 ...

  4. ffmpeg部分编译选项

    -enable-neon  (如果使用了arm公司提供的neon加速技术,则可以并行的读取多个元素.参考网址:http://www.cnblogs.com/hrlnw/p/3723072.html) ...

  5. LinearLayout线性布局搭配权重属性的使用

    在开发中,我们是通过布局来完成应用界面的搭配的,通过各种布局,我们可以完成各种复杂的界面设计.而LinearLayout也就是我们说的线性布局,这个比较简单而且使用很广泛的一种布局.下面我们通过一个D ...

  6. spring bean属性及子元素使用总结

    spring bean属性及子元素使用总结 2016-08-03 00:00 97人阅读 评论(0) 收藏 举报  分类: Spring&SpringMVC(17)  版权声明:本文为博主原创 ...

  7. tomcat使用manager管理app时需要身份验证问题

    我们可以通过图形用户界面来管理tomcat,启动tomcat,在地址栏中输入: Java代码 http://localhost:8080 就可以看见tomcat的欢迎页面,点击左边的tomcat ma ...

  8. Jquery前端选择器

    ----------------------祖先后代选择器------------------------------ 1.祖先 后代:根据一个元素可以取得指定的所有子元素(不管中间有多少后代)$(& ...

  9. day17 11.JdbcUtils工具抽取

    连接数据库的四个必要条件:driverclass.url.username.password. package cn.itcast.utils; import java.sql.Connection; ...

  10. koa的跨域访问

    koa跨域访问:1.安装插件 npm install koa-cors --save-dev2.项目的app.js中var cors = require('koa-cors'); app.use(co ...