zoukankan      html  css  js  c++  java
  • hdu 5790 Prefix 字典树 主席树

    Prefix

    题目连接:

    (http://acm.hdu.edu.cn/showproblem.php?pid=5790)

    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

    Hint

    题意

    问从第L个到第R个的字符串中不重复的前缀个数
    强制在线

    题解:

    建一棵字典树,维护这个节点最迟出现的时间,当再次更新到这个节点的时候,这个时间就是上次这个前缀出现的时间。那么问题转化为求区间小于L的个数,用这个建一棵主席树。
    另一种做法:直接把所有前缀哈希,就是询问区间内不同的哈希值的个数,也是主席树

    代码

    //#include <bits/stdc++.h>
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <set>
    #include <map>
    #include <stack>
    #include <bitset>
    #include <string>
    #include <time.h>
    using namespace std;
    long double esp=1e-11;
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #define fi first
    #define se second
    #define all(a) (a).begin(),(a).end()
    #define cle(a) while(!a.empty())a.pop()
    #define mem(p,c) memset(p,c,sizeof(p))
    #define mp(A, B) make_pair(A, B)
    #define pb push_back
    #define lson l , m , rt << 1
    #define rson m + 1 , r , rt << 1 | 1
    typedef long long int LL;
    const long double PI = acos((long double)-1);
    const LL INF=0x3f3f3f3fll;
    const int MOD =1000000007ll;
    const int maxn=100010;
    int root[maxn],cnt,n;
    struct node{int l,r,sum;}tr[maxn*40];
    void update(int l,int r,int x,int &y,int pos)       //(l,r,root[x],root[x+1],pos)
    {
        tr[++cnt]=tr[x],tr[cnt].sum++,y=cnt;
        if(l==r)return;
        int mid=(l+r)/2;
        if(mid>=pos)update(l,mid,tr[x].l,tr[y].l,pos);
        else update(mid+1,r,tr[x].r,tr[y].r,pos);
    }
    int query(int l,int r,int x,int y,int k)
    {
        if(l==r)return tr[y].sum-tr[x].sum;
        int mid=(l+r)/2;
        if(k>mid)return tr[tr[y].l].sum-tr[tr[x].l].sum+query(mid+1,r,tr[x].r,tr[y].r,k);
        else return query(l,mid,tr[x].l,tr[y].l,k);
    }
    char s[maxn];
    struct TRIE{
    	int cn,pos;
    	struct Trie{
    	int p[26],s;
    	}tr[maxn];
    	void init(){pos=1;cn=0;mem(tr[0].p,0);tr[0].s=0;}
    	void insert(int time)
    	{
    		//L[time]=pos;
    		int len=strlen(s),t=0;
    		int t1=root[time-1],t2;
    
    		for(int x=0;x<len;x++)
    		{
    			if(tr[t].p[s[x]-'a']){
    				t=tr[t].p[s[x]-'a'];
    				//num[pos++]=tr[t].s;
    				update(1,n,t1,t2,tr[t].s+1);
    				t1=t2;
    				tr[t].s=time;
    			}
    			else{
    				t=tr[t].p[s[x]-'a']=++cn;
    				//num[pos++]=tr[t].s;
    				update(1,n,t1,t2,1);
    				t1=t2;
    				tr[t].s=time;
    				mem(tr[t].p,0);
    			}
    		}
    		root[time]=t2;
    	}
    }trie;
    int main()
    {
        //freopen("in.txt", "r", stdin);
        //freopen("1010.in", "r", stdin);
        //freopen("out.txt", "w", stdout);
        //::iterator iter;                  %I64d
        //for(int x=1;x<=n;x++)
        //for(int y=1;y<=n;y++)
        //scanf("%d",&a);
        //printf("%d
    ",ans);
    	while(scanf("%d",&n)!=EOF)
    	{
    		trie.init();
    		cnt=0;
    		root[0]=0;
    		tr[0].l=tr[0].r=tr[0].sum=0;
    		for(int x=1;x<=n;x++)
    		{
    			scanf("%s",s);
    			trie.insert(x);
    		}
    		s[0]=0;
    		trie.insert(n+1);
    		int z=0,l,r;
    		int m;
    		scanf("%d",&m);
    		for(int x=1;x<=m;x++)
    		{
    			scanf("%d%d",&l,&r);
    			l = (z+l)%n+1; r = (z+r)%n+1;
                if(l > r) swap(l, r);
    									//printf("%d %d
    ",root[l-1],root[r]);
    			z=query(1,n,root[l-1],root[r],l);
    			printf("%d
    ",z);
    		}
    		//for(int x=0;x<cnt;x++)printf("%d %d %d %d
    ",x,tr[x].l,tr[x].r,tr[x].sum);
    	}
        return 0;
    }
  • 相关阅读:
    Spring 声明式事务管理(11)
    Spring JdbcTemplate详解(9)
    Spring 基于xml配置方式的AOP(8)
    Spring AspectJ 切入点语法详解(7)
    Spring 基于Aspectj切面表达式(6)
    spring AOP 编程--AspectJ注解方式 (4)
    Spring 切面优先级(5)
    Spring 泛型依赖注入(3)
    python反射/自省 (目前是转载)
    flask_requirements
  • 原文地址:https://www.cnblogs.com/femsub/p/5730556.html
Copyright © 2011-2022 走看看