Prefix

Time Limit: 2000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 858    Accepted Submission(s): 256

Problem Description
Alice gets N strings. Now she has Q questions to ask you. For each question, she wanna know how many different prefix strings between Lth and Rth strings. It's so easy right? So solve it!
 
Input
The input contains multiple test cases.

For each test case, the first line contains one integer N(1≤N≤100000). Then next N lines contain N strings and the total length of N strings is between 1 and 100000. The next line contains one integer Q(1≤Q≤100000). We define a specail integer Z=0. For each query, you get two integer L, R(0=<L,R<N). Then the query interval [L,R] is [min((Z+L)%N,(Z+R)%N)+1,max((Z+L)%N,(Z+R)%N)+1]. And Z change to the answer of this query.

 
Output
For each question, output the answer.
 
Sample Input
3
abc
aba
baa
3
0 2
0 1
1 1
 
Sample Output
7
6
3
 

题目链接:HDU 5790

就是问你[L,R]中的字符串的前缀一共有多少种,那么我们可以把每一个字符串的前缀标号,然后记录这种字符串有几种前缀并更新到主席树上,每一次问[L,R]就变成询问[presum_kind[L-1]+1, presum_kind[R]]之间有几个不同的前缀标号,比如题目样例给前缀标号,就是[1,2,3][1,2,4][5,6,7]。

代码:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(ql) (ql<<1)
#define RC(ql) ((ql<<1)+1)
#define MID(ql,qr) ((ql+qr)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct Trie
{
int nxt[26];
int id;
void reset()
{
fill(nxt, nxt + 26, 0);
id = 0;
}
};
struct seg
{
int lson, rson;
int cnt;
void reset()
{
lson = rson = 0;
cnt = 0;
}
};
Trie L[N];
seg T[N * 36];
int sz, tot, tid, arr[N], Tot;
int lenth[N], last[N], root[N];
char s[N]; void init()
{
sz = 1;
tot = 0;
tid = 0;
Tot = 0;
CLR(last, 0);
L[0].reset();
}
void update(int &cur, int ori, int l, int r, int pos, int flag)
{
cur = ++tot;
T[cur] = T[ori];
T[cur].cnt += flag;
if (l == r)
return ;
int mid = MID(l, r);
if (pos <= mid)
update(T[cur].lson, T[ori].lson, l, mid, pos, flag);
else
update(T[cur].rson, T[ori].rson, mid + 1, r, pos, flag);
}
int query(int S, int E, int l, int r, int ql, int qr)
{
if (ql <= l && r <= qr)
return T[E].cnt - T[S].cnt;
else
{
int ret = 0;
int mid = MID(l, r);
if (ql <= mid)
ret += query(T[S].lson, T[E].lson, l, mid, ql, qr);
if (qr > mid)
ret += query(T[S].rson, T[E].rson, mid + 1, r, ql, qr);
return ret;
}
}
int insert(int id, char s[])
{
int len = strlen(s);
int u = 0;
for (int i = 0; i < len; ++i)
{
int v = s[i] - 'a';
if (!L[u].nxt[v])
{
L[sz].reset();
L[u].nxt[v] = sz++;
}
u = L[u].nxt[v];
if (L[u].id == 0)
L[u].id = ++tid;
arr[++Tot] = L[u].id;
}
lenth[id] = Tot;
return len;
}
int main(void)
{
int n, m, i, l, r;
while (~scanf("%d%d", &n, &m))
{
init();
for (i = 0; i < n; ++i)
{
scanf("%s", s);
insert(i, s);
}
for (i = 1; i <= Tot; ++i)
{
if (!last[arr[i]])
update(root[i], root[i - 1], 1, Tot, i, 1);
else
{
int tmp;
update(tmp, root[i - 1], 1, Tot, last[arr[i]], -1);
update(root[i], tmp, 1, Tot, i, 1);
}
last[arr[i]] = i;
}
scanf("%d", &m);
int ans = 0;
while (m--)
{
int L, R;
scanf("%d%d", &L, &R);
l = min((L + ans) % n, (R + ans) % n);
r = max((L + ans) % n, (R + ans) % n);
L = l - 1 >= 0 ? lenth[l - 1] : 0;
R = lenth[r];
printf("%d\n", ans = query(root[L], root[R], 1, Tot, L + 1, R));
}
}
return 0;
}

HDU 5790 Prefix(字典树+主席树)的更多相关文章

  1. HDU 5790 Prefix(Hash + 主席树)

    题目链接  Prefix 题意  给定一个字符串序列,求第$l$个字符串到第$r$个字符串之间有多少个不同的前缀 强制在线 考虑$Hash$ 首先把所有前缀都$hash$出来,按顺序组成一个长度不超过 ...

  2. 线段树简单入门 (含普通线段树, zkw线段树, 主席树)

    线段树简单入门 递归版线段树 线段树的定义 线段树, 顾名思义, 就是每个节点表示一个区间. 线段树通常维护一些区间的值, 例如区间和. 比如, 上图 \([2, 5]\) 区间的和, 为以下区间的和 ...

  3. HDU5790 Prefix 字典树+主席树

    分析:这个题和spoj的d_query是一个题,那个是求一段区间里有多少个不同的数字,这里是统计有多少个不同的前缀 用字典树进行判重,(和查询不同的数字一样)对于每个不同的前缀,只保留它最后一次出现的 ...

  4. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  6. hdu 4417 Super Mario (主席树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给你段长为n的序列,有q个询问,每次询问区间[l.r]内有多少个数小于等于k 思路: 之前用 ...

  7. hdu 4348 To the moon (主席树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4348 题意: 一个长度为n的数组,4种操作 : (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 . (2)Q ...

  8. HDU 4348 To the moon 主席树 在线更新

    http://acm.hdu.edu.cn/showproblem.php?pid=4348 以前做的主席树没有做过在线修改的题做一下(主席树这种东西正经用法难道不是在线修改吗),标记永久化比较方便. ...

  9. hdu5790 Prefix(Trie树+主席树)

    Problem Description Alice gets N strings. Now she has Q questions to ask you. For each question, she ...

随机推荐

  1. 查询删除的SAP凭证

    标准报表查询:RSSCD100 函数模块:CHANGEDOCUMENT_DISPLAY, Display Change Documents 数据表查询:CDHDR, Change document h ...

  2. arm-none-linux-gnueabi-gcc No such file or directory这个错误的解决方法

    这个gcc可执行文件是32位的版本,而在64位系统上需要安装32位兼容包才可以运行正常 .用file命令查看这个文件得到: 解决办法: 安装ia32-libs sudo apt-get install ...

  3. Linux段式管理与页式管理

    内存管理有2种机制:1.段式管理:2.页式管理 在80386CPU中增加了2个寄存器:1.全局性的段描述表寄存器GDTR 2.局部性的段描述表寄存器LDTR 段寄存器的高13位用于在全局或局部描述表项 ...

  4. python3 练习题100例 (二十)

    #!/usr/bin/env python3# -*- coding: utf-8 -*-"""练习二十:判断一个年份是否是闰年公历闰年计算方法:1.普通年能被4整除且不 ...

  5. C语言真正的编译过程(4个步骤~~预编译,编译,汇编,连接)

    转载自:https://www.cnblogs.com/wuyouxiaocai/p/5701088.html#commentform 说实话,很多人做了很久的C/C++,也用了很多IDE,但是对于可 ...

  6. C语言函数篇(四)函数的设计

    1. 函数设计的时候,如果使用到全局变量,就尽量通过参数的形式传递进来 也就是说,保持 函数 跟 外部的交互 只有 参数 和 返回值 2. 在有参数的情况下,或者有数值输入的时候,要先进行错误判断. ...

  7. Numpy数据存取与函数

    数据的CSV文件存取 多维数据的存取 NumPy的随机数函数 NumPy的统计函数 NumPy的梯度函数

  8. 20145202马超GDB调试汇编堆栈过程分析

    20145202马超GDB调试汇编堆栈过程分析 esc :w保存,:wq保存并退出 x:删除错误的单个字母 dw:删除整个单词 gcc hello.c -o hello:运行hello.c gcc - ...

  9. 8,Linux系统基础优化及常用命令

    Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ifconfig 查询.设置网卡和 ...

  10. mybatis异常:There is no getter for property named 'xxx' in 'xxx'

    在使用mybatis查询的时候出现了下面的异常: org.apache.ibatis.reflection.ReflectionException: There is no getter for pr ...