Language:
Checking the Text
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 Source
POJ Monthly--2006.02.26,zgl & twb
|
【题目分析】
有题目给的数据范围来说,插入的次数并不是很多。那么就可以暴力插入,完全没有技术含量。然后需要记录下它的原始下标(这题十分猥琐,插入和查询用的不是同一个下标)在查找时,用RK-Hash的方法来比较两个字符串是否相同。但是也不能暴力的比较,需要进行二分。这样就会快很多。
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#define P 1000000009ll
#define B 131
using namespace std;
int pw[60001];
struct node{
long long pos;
long long num;
};
node s[60001];
long long hn[60001];
long long n;
char ch;
long long a,b;
long long t=1;
long long st1,en,st2,st;
long long hasher(long long begin,long long end)
{
return ((hn[end]-hn[begin-1]*pw[end-begin+1])%P+P)%P;
}
long long find(long long as,long long ae,long long bs,long long be)
{
if (as==ae&&bs==be){
if (s[as].num==s[bs].num) return 1;
else return 0;
}
int mida=(as+ae)/2,midb=(bs+be)/2;
if (hasher(as,mida)==hasher(bs,midb)){return (mida-as+1)+find(mida+1,ae,midb+1,be);}
else return find(as,mida-1,bs,midb-1);
}
int main()
{
pw[0]=1;pw[1]=B;for (int i=2;i<=50000;++i) pw[i]=(pw[i-1]*B)%P;
scanf("%c",&ch);
while (ch!='\n'){
s[t].pos=t;
s[t++].num=ch-'a'+1;
scanf("%c",&ch);
}
t--;
hn[0]=0;
for (int i=1;i<=t;++i) hn[i]=(hn[i-1]*B+s[i].num)%P;
scanf("%d",&n);
while (n--){
cin>>ch;
if (ch=='Q'){
scanf("%d%d",&a,&b);
for (int i=1;i<=t;++i){
if (s[i].pos==a) st1=i;
if (s[i].pos==b) st2=i;
}
if (st1>st2)
cout<<find(st2,st2+t-st1,st1,t)<<endl;
else cout<<find(st1,st1+t-st2,st2,t)<<endl;
}
if (ch=='I'){
cin>>ch>>a;
if (a>t) a=t+1;
for (int i=t;i>=a;--i){
s[i+1]=s[i];
}
s[a].pos=0;
s[a].num=ch-'a'+1;
t++;
for (int i=a;i<=t;++i){
hn[i]=(hn[i-1]*B+s[i].num)%P;
}
}
}
return 0;
}