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\n",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\n",root[l-1],root[r]);
			z=query(1,n,root[l-1],root[r],l);
			printf("%d\n",z);
		}
		//for(int x=0;x<cnt;x++)printf("%d %d %d %d\n",x,tr[x].l,tr[x].r,tr[x].sum);
	}
    return 0;
}
posted @ 2016-08-02 20:07  femsub  阅读(470)  评论(0编辑  收藏  举报