【BZOJ】【P2258】【pku2758 Checking the Text 文本校对】【题解】【hash】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2258

明天就初赛了

刷水娱♂乐身心

Code:

#include<cstdio>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=50300;
typedef unsigned long long UL;
UL base=233;
UL hash[maxn];
UL hash_l[maxn];
char s[maxn];
int len,m;
void hash_init(int be){
	UL val=hash[be-1];
	for(int i=be;i<=len;i++){
		val=val*base+s[i]-'a';
		hash[i]=val;
	}
}
int mp[maxn+200];
int main(){
	scanf("%s",s+1);s[0]='#';
	len=strlen(s+1);hash_l[0]=1;
	for(int i=1;i<=len+200;i++)hash_l[i]=hash_l[i-1]*base,mp[i]=i;
	scanf("%d",&m);
	hash_init(1);
	while(m--){
		char op=getchar();while(op!='Q'&&op!='I')op=getchar();
		if(op=='I'){
			char x=getchar();while(!isalpha(x))x=getchar();
			int ps;scanf("%d",&ps);if(ps>len)ps=len+1;
			for(int i=len+1;i>ps;i--)s[i]=s[i-1];
			for(int j=lower_bound(mp+1,mp+1+len,ps)-mp,i=j;i<=len+1;i++)mp[i]++;
			s[ps]=x;len++;s[len+1]='\0';
			hash_init(ps);
		}else{
			int a,b;scanf("%d%d",&a,&b);a=mp[a];b=mp[b];
			int l=0,r=len-b+2;
			while(l<r){
				int mid=(l+r)>>1;
				if(hash[a+mid-1]-hash[a-1]*hash_l[mid]==hash[b+mid-1]-hash[b-1]*hash_l[mid])
				l=mid+1;
				else r=mid;
			}printf("%d\n",l-1);
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值