POJ-2758 checking the text(哈希+二分)

4 篇文章 0 订阅
1 篇文章 0 订阅

POJ-2758 checking the text(哈希+二分)

题目:
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

题意:
给你一个字符串,有两种操作,一种是查询两个位置开始的最大公共子串,另一种是在原字符串第j个字符前加上一个字符。
插入<=200,查询<=20000

计算字符串的hash值,对于每次查询二分长度,插入时则把后面的hash值全重算一遍。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
const long long LL=1000000009;
long long hsh[60005],pow[60005];
char s[60005];
int now[50005];
int len,m_len;
void get(int l,int len)
{
    for(int i=l;i<len;i++){
        if(i==0){hsh[i]=s[i];continue;}
        hsh[i]=hsh[i-1]*130+s[i];
        hsh[i]%=LL;
    }
}
void query(int x,int y)
{
    int l=1,r=min(len-x+1,len-y+1);
    while(l<r){
        int mid=(l+r)>>1;
        if((hsh[x+mid-1]-hsh[x-1]*pow[mid]%LL+LL)%LL==(hsh[y+mid-1]-hsh[y-1]*pow[mid]%LL+LL)%LL)
            l=mid+1;
        else r=mid;
    }
    printf("%d\n",l-1);
}
void change(char c,int x)
{
    if(x>len) x=len;
    memcpy(s+x+1,s+x,sizeof(char)*(len-x));
    s[x]=c;
    len++;
    get(x,len);

    for(int i=m_len-1;i>=0;i--){
        if(now[i]>=x) now[i]++;
        else break;
    }
}
int main()
{
    scanf("%s",s);
    len=strlen(s);
    m_len=len;
    int n;
    scanf("%d",&n);
    get(0,len);
    pow[0]=1;
    for(int i=1;i<len;i++){
        pow[i]=(pow[i-1]*130)%LL;
        now[i]=i;
    }
    char choice[5];
    int x,y;
    for(int i=0;i<n;i++){
        scanf("%s",choice);
        if(choice[0]=='Q') {
            scanf("%d %d",&x,&y);
            x--;
            y--;
            query(now[x],now[y]);
           // printf("%s\n",s);
        }
        else {
            scanf("%s %d",choice,&x);
            x--;
            change(choice[0],x);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值