poj 2758: Checking the Text(二分+Hash)

Time Limit: 8000MS Memory Limit: 65536K
Total Submissions: 3502 Accepted: 795

Description

Wind's birthday is approaching. In order to buy a really really fantastic gift for her, Jiajia has to take a boring yet money-making job - a text checker. 
This job is very humdrum. Jiajia will be given a string of text what is English letters and he must count the maximum number of letters that can be matched, starting from two position of the current text simultanously. The matching proceeds from left to right, one character by one. 
Even worse, sometimes the boss will insert some characters before, after or within the text. Jiajia wants to write a program to do his job automatically, this program should be fast enough, because there are only few days to Wind's birthday.

Input

The first line of input file contains initial text. 
The second line contains then number of commands n. And the following n lines describe each command. There are two formats of commands: 
I ch p: Insert a character ch before the p-th. if p is larger than the current length of text, then insert at end of the text. 
Q i j: Ask the length of matching started from the i-th and j-th character of the initial text, which doesn't include the inserted characters. 
You can assume that the length of initial text will not exceed 50000, the number of I command will not exceed 200, the number of Q command will not exceed 20000.

Output

Print one line for each Q command, contain the max length of matching.

Sample Input

abaab
5
Q 1 2
Q 1 3
I a 2
Q 1 2
Q 1 3

Sample Output

0
1
0
3


问题概述:输入1个字符串,之后若干次操作:①I a b表示在第b个位置插入一个字符a;②Q a b表示查询下标a和b开始两个子串的最长前缀,注意这个下标是原先数列的下标!!例如字符串abcde对应下标为12345,插入若干次字符之后字符串变为axxbcdxxe,那么对应下标为1xx234xx5(x表示对应位置的字符没有下标)


Hash+二分应该是最简单的方法了

每次二分答案(长度),然后判断这个长度下的两个子串是否完全相同

因为字符串过长,不能strcmp,所以用Hash

修改次数<200,可以暴力修改重新计算Hash

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
#define mod 73939133
char str[51005];
LL n, Hash[51005], Pow[51005] = {1}, id[51005];
LL Jud(LL x, LL y)
{
	LL l, r, m;
	l = 0, r = n-max(x, y)+1;
	while(l<r)
	{
		m = (l+r+1)/2;
		if((Hash[x+m-1]-Hash[x-1]*Pow[m]%mod+mod)%mod==(Hash[y+m-1]-Hash[y-1]*Pow[m]%mod+mod)%mod)
			l = m;
		else
			r = m-1;
	}
	return l;
}
int main(void)
{
	char ch;
	LL m, q, i, x, y;
	for(i=1;i<=51000;i++)
		Pow[i] = Pow[i-1]*128%mod;
	while(scanf("%s", str+1)!=EOF)
	{
		m = n = strlen(str+1);
		for(i=1;i<=n;i++)
		{
			Hash[i] = (Hash[i-1]*128+str[i])%mod;
			id[i] = i;
		}
		scanf("%lld", &q);
		while(q--)
		{
			scanf(" %c", &ch);
			if(ch=='I')
			{
				scanf(" %c%lld", &ch, &x);
				x = min(n+1, x);
				memcpy(str+x+1, str+x, n-x+1);
				str[x] = ch;
				for(i=m;id[i]>=x;i--)
					id[i]++;
				n++;
				for(i=x;i<=n;i++)
					Hash[i] = (Hash[i-1]*128+str[i])%mod;
			}
			else
			{
				scanf("%lld%lld", &x, &y);
				printf("%lld\n", Jud(id[x], id[y]));
			}
		}
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值