POJ 2758 Checking the Text(Hash+二分答案)

【题目链接】 http://poj.org/problem?id=2758

【题目大意】

  给出一个字符串,支持两个操作,在任意位置插入一个字符串,或者查询两个位置往后的最长公共前缀,注意查询的时候是原串下标,插入的时候则是最近更新串的下标。

【题解】

  因为插入操作只有两百次,所以考虑hash重构来处理匹配问题,碰到插入就重构插入点往后的哈希表,否则二分两个位置往后的匹配长度,查hash表判断是否可行。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=60000,base=233;
typedef long long ll;
int len,n,m,pos[N],x,y;
ll hash[N],p[N];
char s[N],op[10];
void Build(int x){for(int i=x;i<=len;i++)hash[i]=hash[i-1]*base+s[i];}
ll get_hash(int L,int R){return hash[R]-hash[L-1]*p[R-L+1];}
int query(int x,int y){
    int l=0,r=len-max(x,y)+1;
    while(l<=r){
        int mid=l+r>>1;
        if(get_hash(x,x+mid-1)==get_hash(y,y+mid-1))l=mid+1;
        else r=mid-1;
    }return r;
}
int main(){
    for(int i=p[0]=1;i<N;i++)p[i]=p[i-1]*base;
    scanf("%s",s+1); n=len=strlen(s+1);
    scanf("%d",&m); Build(1);
    for(int i=1;i<=n;i++)pos[i]=i;
    for(int i=1;i<=m;i++){
        scanf("%s",op);
        if(op[0]==‘I‘){
            scanf("%s%d",op,&x);
            if(x>len)x=len+1;
            memcpy(s+x+1,s+x,(len-x+1)*sizeof(char));
            s[x]=op[0];
            for(int j=n;j>=1;j--){
                if(pos[j]>=x)pos[j]++;
                else break;
            }len++; Build(x);
        }else{
            scanf("%d%d",&x,&y);
            printf("%d\n",query(pos[x],pos[y]));
        }
    }return 0;
}

  

时间: 2025-01-25 22:36:07

POJ 2758 Checking the Text(Hash+二分答案)的相关文章

POJ 2018 Best Cow Fences(二分答案)

POJ 2018 Best Cow Fences(二分答案) Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12144 Accepted: 3958 Description Farmer John's farm consists of a long row of N (1 <= N <= >100,000)fields. Each field contains a certain nu

字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在学习后缀数组的时候已经做过一遍了,但是现在主攻字符串hash,再用字符串hash写一遍. 这题的思路是这样的: 1)取较短的串的长度作为high,然后二分答案(每次判断长度为mid=(low+high)>>1是否存在,如果存在就增加下界:不存在就缩小上界): 2)主要是对答案的判断(judge函数

poj 1275 Cashier Employment - 差分约束 - 二分答案

A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its need. The supermarket manager has hired you to help him, solve his problem. The problem is that the supermarket needs different number of cashiers at d

POJ 2774 Long Long Message (Hash + 二分)

Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 34473   Accepted: 13834 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days

poj 2349 Arctic Network MST/二分答案

poj 2349 Arctic Network 题目传送 Sol: 方法一: 贪心的想,发现n个点只需要n-1条边即可,求MST即可,再把MST中最大的m-1条边去掉,第m大就是答案. code: #include<string> #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define IL inline #define RG register

POJ 3080 Blue Jeans(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通过拼接符拼成一个串,做一遍后缀数组,二分答案,对于二分所得值,将h数组大于这个值的相邻元素分为一组,判断组内元素是否覆盖全字典,是则答案成立,对于答案扫描sa,输出第一个扫描到的子串即可. [代码] #include <cstdio> #include <cstring> #inclu

【二分答案+智障的字符串hash】BZOJ2946-[Poi2000]公共串(Ranklist倒一达成!!!!!)【含hash知识点】

[题目大意] 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. [字符串hash的小笔记] hash[i]=(hash[i-1]*p+idx(s[i]))%mod,idx为映射值,一般a..z映射1..26: 习惯上,p取一个6到8位的素数即可,mod一般取大素数 1e9+7(1000000007)或1e9+9(1000000009). hash[i]=(hash[i-1]*p+idx(s[i]))%mod 表示第 i 个前缀的hash值,是一个hash的前缀和,那么,要求S[l…r]

POJ 3294 Life Forms(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=3294 [题目大意] 求出在至少在一半字符串中出现的最长子串. 如果有多个符合的答案,请按照字典序输出. [题解] 将所有的字符串通过不同的拼接符相连,作一次后缀数组, 二分答案的长度,然后在h数组中分组,判断是否可行, 按照sa扫描输出长度为L的答案即可.注意在一个子串中重复出现答案串的情况. [代码] #include <cstdio> #include <cstring> #include <vecto

POJ 3484 Showstopper(二分答案)

[题目链接] http://poj.org/problem?id=3484 [题目大意] 给出n个等差数列的首项末项和公差.求在数列中出现奇数次的数.题目保证至多只有一个数符合要求. [题解] 因为只有一个数符合要求,所以在数列中数出现次数的前缀和必定有奇偶分界线, 所以我们二分答案,计算前缀和的奇偶性进行判断,得到该数的位置. [代码] #include <cstdio> #include <algorithm> #include <cstring> using na