【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;
}


你可能感兴趣的:(bzoj)