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;
}

转载于:https://www.cnblogs.com/Blackops/p/7228484.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值