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

Checking the Text
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
#include
#include
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=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;
}


你可能感兴趣的:(字符串)