HDU 5790 Prefix(字典树+主席树)

Posted Blackops

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5790 Prefix(字典树+主席树)相关的知识,希望对你有一定的参考价值。

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(1N100000). 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(1Q100000). 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(字典树+主席树)的主要内容,如果未能解决你的问题,请参考以下文章

Prefix HDU - 5790

HDU 1247 Hat’s Words(字典树)

HDU 4557 Tree(可持久化字典树 + LCA)

主席树 hdu 4348

hdu6621--主席树

hdu 4417 Super Mario (主席树)