hdu5790 Prefix(Trie树+主席树)

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

题意:给你n个字符串,问你第L个字符串到R个字符串中不同前缀的个数,且强制在线。

思路:这题和之前d-query这题很相似,那题问的是区间内不同数的种类。这题问的是不同前缀个数,所以我们可以先把所有的字符串插入到Trie树中,然后每次插入维护每一个节点最后被遍历到的时刻,然后用主席树维护下就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define Key_value ch[ch[root][1]][0]
#define maxn 100050
#define maxnode 1000050
char s[maxn];
int n;
int ch[maxnode][28];
int val[maxnode];
int sz;

#define M 1000500*30
int lson[M],rson[M],c[M],T[M];
int th;

int build(int l,int r)
{
    int i,j,newroot=++th,mid;
    c[newroot]=0;
    if(l!=r){
        mid=(l+r)/2;
        lson[newroot]=build(l,mid);
        rson[newroot]=build(mid+1,r);
    }
    return newroot;
}

int update(int root,int zhi,int value)
{
    int i,j,newroot=++th;
    int tmp=newroot;
    int l=1,r=n,mid;
    c[newroot]=c[root]+value;
    while(l<r){
        mid=(l+r)/2;
        if(zhi<=mid){
            r=mid;
            lson[newroot]=++th;rson[newroot]=rson[root];
            newroot=lson[newroot];root=lson[root];
        }
        else{
            l=mid+1;
            lson[newroot]=lson[root];rson[newroot]=++th;
            newroot=rson[newroot];root=rson[root];

        }
        c[newroot]=c[root]+value;
    }
    return tmp;
}

int question(int root,int pos)
{
    int i,j;
    int sum=0;
    int l=1,r=n,mid;
    while(l<r){
        mid=(l+r)/2;
        if(pos<=mid){
            r=mid;
            sum+=c[rson[root] ];
            root=lson[root];
        }
        else{
            l=mid+1;
            root=rson[root];
        }

    }
    sum+=c[root];
    return sum;
}



void init(){
    sz=0;memset(ch[0],0,sizeof(ch[0]));
    memset(val,0,sizeof(val));
}
int idx(char c){
    return c-'a';
}



void charu(char *s,int tm){
    int u=0,len=strlen(s),i,c;
    T[tm]=T[tm-1];
    for(i=0;i<len;i++){
        c=idx(s[i]);
        if(!ch[u][c]){
            sz++;
            memset(ch[sz],0,sizeof(ch[sz]));
            val[sz]=tm;
            T[tm]=update(T[tm],tm,1);
            ch[u][c]=sz;
            u=ch[u][c];
        }
        else if(ch[u][c]){
            T[tm]=update(T[tm],val[ch[u][c] ],-1);
            val[ch[u][c] ]=tm;
            T[tm]=update(T[tm],tm,1);
            u=ch[u][c];
        }
    }
}



int main()
{
    int m,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        th=0;
        T[0]=build(1,n);
        for(i=1;i<=n;i++){
            scanf("%s",s);
            charu(s,i);
        }
        scanf("%d",&m);
        int l,r,z=0,t1,t2;
        for(i=1;i<=m;i++){
            scanf("%d%d",&l,&r);
            l=(z+l)%n+1;
            r=(z+r)%n+1;
            if(l>r)swap(l,r);
            z=question(T[r],l);
            printf("%d\n",z);
        }



    }
    return 0;
}
    原文作者:Trie树
    原文地址: https://blog.csdn.net/Kirito_Acmer/article/details/52106539
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞