2016多校训练Contest5: 1010 Prefix hdu5790

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


当时考场上题目看错了。。以为每个长度都是10W,结果其实是一共长10W

如果没看错也许就不会写1005而写这题了。。

这题就是个主席树

我们统计每个字符串出现的前缀,相同的就记录在最后出现的字符串上

依次枚举字符串,然后维护各个串前缀出现次数

那么如果枚举到R,我们要询问区间[L,R]的话,只需要直接区间求和就可以了

但是因为询问是在线的,所以我们需要用主席树来维护枚举到R时候线段树的结果

最后,对于不同前缀的统计可以用字典树。我的写法是双hash+map维护的

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long mod1=1000000007,mod2=1000000003;
struct tree
{
	int ll,rr;
	int x;
}tr[4000001];
int root[100001];
int tot;
inline void build(int l,int r)
{
	int p=tot;
	if(l!=r)
	{
		int mid=(l+r)/2;
		tot++;
		tr[p].ll=tot;
		build(l,mid);
		tot++;
		tr[p].rr=tot;
		build(mid+1,r);
	}
}
inline int inc(int p,int l,int r,int x,int lt,int rt)
{
	if(p==0)
		return 0;
	if(l==lt&&rt==r)
	{
		tot++;
		tr[tot].x=tr[p].x+x;
		return tot;
	}
	else
	{
		int mid=(lt+rt)/2;
		tot++;
		int pp=tot;
		if(l<=mid)
			tr[pp].ll=inc(tr[p].ll,l,r,x,lt,mid);
		else
			tr[pp].ll=tr[p].ll;
		if(r>mid)
			tr[pp].rr=inc(tr[p].rr,l,r,x,mid+1,rt);
		else
			tr[pp].rr=tr[p].rr;
		tr[pp].x=tr[tr[pp].ll].x+tr[tr[pp].rr].x;
		return pp;
	}
}
inline int ask(int p,int l,int r,int lt,int rt)
{
	if(l<=lt&&rt<=r)
		return tr[p].x;
	else
	{
		int mid=(lt+rt)/2;
		int ans=0;
		if(l<=mid)
			ans+=ask(tr[p].ll,l,r,lt,mid);
		if(r>mid)
			ans+=ask(tr[p].rr,l,r,mid+1,rt);
		return ans;
	}
}
string x;
map<pair<long long,long long>,int> M;
map<pair<long long,long long>,int>::iterator it;
int cnt;
int fx[200001];
int rt;
int n;
inline void prepare(int d)
{
	int lx=x.size();
	int i;
	long long ha1=0,ha2=0;
	for(i=0;i<=lx-1;i++)
	{
		ha1=ha1*(long long)27+(long long)(x[i]-'a'+1);
		ha1%=mod1;
		ha2=ha2*(long long)27+(long long)(x[i]-'a'+1);
		ha2%=mod2;
		pair<long long,long long> t=make_pair(ha1,ha2);
		it=M.find(t);
		if(it==M.end())
		{
			cnt++;
			M[t]=cnt;
			int tt=tot+1;
			inc(rt,d,d,1,1,n);
			rt=tt;
			fx[cnt]=d;
		}
		else
		{
			int dx=M[t];
			int tt=tot+1;
			inc(rt,fx[dx],fx[dx],-1,1,n);
			rt=tt;
			fx[dx]=d;
			tt=tot+1;
			inc(rt,d,d,1,1,n);
			rt=tt;
		}
	}
	root[d]=rt;
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		memset(tr,0,sizeof(tr));
		memset(root,0,sizeof(root));
		tot=1;
		cnt=0;
		it=M.begin();
		while(!M.empty())
			M.erase(it++);
		M.clear();
		build(1,n);
		root[0]=1;
		rt=1;
		int i;
		for(i=1;i<=n;i++)
		{
			cin>>x;
			prepare(i);
		}
		int m;
		scanf("%d",&m);
		int l,r;
		int z=0;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&l,&r);
			int ll,rr;
			ll=min((z+l)%n,(z+r)%n)+1;
			rr=max((z+l)%n,(z+r)%n)+1;
			z=ask(root[rr],ll,rr,1,n);
			printf("%d\n",z);
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值