BZOJ 2258 pku2758 Checking the Text 文本校对 Splay+Hash

23 篇文章 0 订阅
11 篇文章 0 订阅

题目大意:给定一个字符串,多次插入一个字符和询问某两个后缀的LCP

Splay+Hash。同1014

这逗比的询问。。。。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 50500
#define BASE 151
using namespace std;
typedef unsigned long long ll;
struct abcd{
	abcd *fa,*ls,*rs;
	int val,size;
	ll hash;
	abcd(int x);
	void Push_Up();
}*null=new abcd(0),*root=null,*tree[M];
int n,m;
char s[M];
ll power[M];
abcd :: abcd(int x)
{
	fa=ls=rs=null;
	hash=val=x;
	size=(bool)x;
}
void abcd :: Push_Up()
{
	size=ls->size+rs->size+1;
	hash=ls->hash*power[rs->size+1]+val*power[rs->size]+rs->hash;
}
void Zig(abcd *x)
{
	abcd *y=x->fa;
	y->ls=x->rs;
	x->rs->fa=y;
	x->rs=y;
	x->fa=y->fa;
	if(y==y->fa->ls)
		y->fa->ls=x;
	else
		y->fa->rs=x;
	y->fa=x;
	y->Push_Up();
	if(y==root)
		root=x;
}
void Zag(abcd *x)
{
	abcd *y=x->fa;
	y->rs=x->ls;
	x->ls->fa=y;
	x->ls=y;
	x->fa=y->fa;
	if(y==y->fa->ls)
		y->fa->ls=x;
	else
		y->fa->rs=x;
	y->fa=x;
	y->Push_Up();
	if(y==root)
		root=x;
}
void Splay(abcd *x,abcd *aim)
{
	while(1)
	{
		abcd *y=x->fa,*z=y->fa;
		if(y==aim)
			break;
		if(z==aim)
		{
			if(x==y->ls)
				Zig(x);
			else
				Zag(x);
			break;
		}
		if(x==y->ls)
		{
			if(y==z->ls)
				Zig(y);
			Zig(x);
		}
		else
		{
			if(y==x->rs)
				Zag(y);
			Zag(x);
		}
	}
	x->Push_Up();
}
void Find(abcd *x,int y,abcd *z)
{
	while(1)
	{
		if(y<=x->ls->size)
			x=x->ls;
		else
		{
			y-=x->ls->size;
			if(y==1)
				break;
			y--;x=x->rs;
		}
	}
	Splay(x,z);
}
void Insert(int pos,int x)
{
	Find(root,pos,null);
	Find(root,pos+1,root);
	(root->rs->ls=new abcd(x))->fa=root->rs;
	root->rs->Push_Up();
	root->Push_Up();
}
abcd* Build_Tree(int x,int y)
{
	if(x>y) return null;
	int mid=x+y>>1;
	abcd *re=new abcd(s[mid]);
	tree[mid]=re;
	(re->ls=Build_Tree(x,mid-1))->fa=re;
	(re->rs=Build_Tree(mid+1,y))->fa=re;
	return re->Push_Up(),re;
}
void Output(abcd *x)
{
	if(x==null)
		return ;
	Output(x->ls);
	printf("----------------------%c\n",x->val);
	Output(x->rs);
}
bool Check(int x,int y,int mid)
{
	Find(root,x,null);
	Find(root,x+mid+1,root);
	ll hash1=root->rs->ls->hash;
	Find(root,y,null);
	Find(root,y+mid+1,root);
	ll hash2=root->rs->ls->hash;
	return hash1==hash2;
}
int Bisection(int x,int y)
{
	int l=0,r=min(n-x+1,n-y+1);
	while(l+1<r)
	{
		int mid=l+r>>1;
		if( Check(x,y,mid) )
			l=mid;
		else
			r=mid;
	}
	if( Check(x,y,r) )
		return r;
	else
		return l;
}
int main()
{
	int i,x,y;
	char p[10];

	for(power[0]=1,i=1;i<M;i++)
		power[i]=power[i-1]*BASE;

	scanf("%s",s+1);n=strlen(s+1);

	root=new abcd(19980402);
	(root->rs=new abcd(19980402))->fa=root;
	(root->rs->ls=Build_Tree(1,n))->fa=root->rs;
	root->rs->Push_Up();
	root->Push_Up();

	cin>>m;
	for(i=1;i<=m;i++)
	{
		scanf("%s",p);
		if(p[0]=='Q')
		{
			scanf("%d%d",&x,&y);
			Splay(tree[x],null);x=root->ls->size;
			Splay(tree[y],null);y=root->ls->size;
			printf("%d\n", Bisection(x,y) );
		}
		else
		{
			scanf("%s%d",p,&x);
			if(x>n) x=n+1;
			Insert(x,p[0]);
			n++;
		}
		//Output(root);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值