Reincarnation HDU - 4622
\(\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的更多相关文章
- Reincarnation HDU - 4622 (后缀自动机)
Reincarnation \[ Time Limit: 3000 ms\quad Memory Limit: 65536 kB \] 题意 给出一个字符串 \(S\),然后给出 \(m\) 次查询, ...
- hdu 4622 Reincarnation(后缀数组)
hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...
- HDU 4622 Reincarnation Hash解法详解
今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...
- HDU 4622 (后缀自动机)
HDU 4622 Reincarnation Problem : 给一个串S(n <= 2000), 有Q个询问(q <= 10000),每次询问一个区间内本质不同的串的个数. Solut ...
- hdu 4622 Reincarnation
http://acm.hdu.edu.cn/showproblem.php?pid=4622 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...
- hdu 4622 Reincarnation trie树+树状数组/dp
题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...
- hdu 4622 Reincarnation SAM模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有Q次区间查询(Q <= 10000),问区 ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
- HDU 4622 Reincarnation(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...
随机推荐
- shell脚本 回顾 小练习
1.把/OPT目录下(包含子目录)下所有后缀为“.sh”的文件后缀变更为“.shell” 2.将A.B.C目录下的文件A1.A2.A3文件改名为A4.A5.A63.如何在vi模式下将文件中的aa字符串 ...
- 重新认识synchronized(上)
synchronized在JDK5之前一直被称为重量级锁,是一个较为鸡肋的设计,而在JDK6对synchronized内在机制进行了大量显著的优化,加入了CAS,轻量级锁和偏向锁的功能,性能上已经跟R ...
- python_class21
#!/usr/bin/env python # -*- coding: utf-8 -*- """ @version: 3.5 @author: morgana @lic ...
- JSP+Servlet 无数据库模拟登录过程
程序目录结构: index.jsp: <%@ page language="java" contentType="text/html; charset=utf-8& ...
- Android编译系统产品线
1.Android源码中的产品线解析 通常产品厂商在拿到Android源码后会在Android源码基础上进行定制修改,以匹配适应自己的产品.这就引入了产品线的概念.Android系统源码中,产品相关的 ...
- 斐波那契数列-java实现
1,1,2,3,5,8,13,21...... 以上的数列叫斐波那契数列,今天的面试第一题,输出前50个,这里记录下. 方式一 package com.geenk.demo.my; /** * @au ...
- linux终端后台运行
nohup command &(然后X退出即可) &也可用来在终端中同时执行几条命令(并行,最后面不要忘记加&) command1 & command2 & c ...
- 按钮控件JButton的使用
---------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestButton. ...
- C++——override
override关键字作用: 如果派生类在虚函数声明时使用了override描述符,那么该函数必须重载其基类中的同名函数,否则代码将无法通过编译.举例子说明 struct Base { virtual ...
- ???Spring集成MyBatis02 【不推荐使用,了解即可】
2017年5月19日09:31:22 由于该种方法比较麻烦,所以三少暂时不更新,哈哈哈:待更新...