poj2758 checking the text

125 篇文章 0 订阅
78 篇文章 0 订阅

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.

哈希+二分求LCP,由于修改次数较少,暴力维护即可。
进阶版bzoj1014火星人,修改次数较多(点击此处查看题解),需要平衡树维护。

#include<cstdio>
#include<cstring>
char s[51000];
int f[51000];
unsigned long long h[51000],pw[51000];
int min(int x,int y)
{
    return x<y?x:y;
}
int main()
{
    int i,j,k,m,n,p=13131,q,x,y,z,T,l;
    unsigned long long x1,x2;
    char c;
    scanf("%s",s+1);
    scanf("%d",&T);
    n=l=strlen(s+1);
    pw[0]=1;
    for (i=1;i<=50500;i++)
      pw[i]=pw[i-1]*p;
    for (i=1;i<=l;i++)
      f[i]=i;
    for (i=1;i<=l;i++)
      h[i]=h[i-1]*p+s[i]-'a'+1;
    while (T--)
    {
        scanf("\n%c",&c);
        if (c=='Q')
        {
            scanf("%d%d",&x,&y);
            x=f[x];
            y=f[y];
            i=0;
            j=min(l-x,l-y)+1;
            while (i<j)
            {
                m=(i+j+1)/2;
                x1=h[x+m-1]-h[x-1]*pw[m];
                x2=h[y+m-1]-h[y-1]*pw[m];
                if (x1==x2) i=m;
                else j=m-1;
            }
            printf("%d\n",i);
        }
        else
        {
            scanf(" %c%d",&c,&x);
            if (x>l+1) x=l+1;
            for (i=l;i>=x;i--)
            {
                s[i+1]=s[i];                
            }
            s[x]=c;
            for (i=n;i>=1&&f[i]>=x;i--)
              f[i]++; 
            l++;
            for (i=x;i<=l;i++)
              h[i]=h[i-1]*p+s[i]-'a'+1;
        }
    }
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值