poj-2758 Checking the Text
题意:
给定一个字符串,要求维护两种操作:
I:在字符串中插入一个字符;
Q:询问某两个位置開始的LCP。
插入操作<=200,字符串长度<=5w,查询操作<=2w;
题解:
第一道RKhash题,插入太少让这题变成了傻题;
注意题中描写叙述的各种恶心的下标讨论;
插入下标是当前下标。而查询是原下标;
查询就是二分答案,利用RKhash高速合并的性质搞搞。
复杂度O(mlogn+100n);
单组数据。稍微卡常;
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 51000
#define mod 1000000009ll
using namespace std;
typedef unsigned long long ull;
int p[N],len;
char str[N],op[10];
ull hash[N],pow[N];
void Build(int x)
{
for(int i=x;i<=len;i++)
hash[i]=(hash[i-1]*131+str[i])%mod;
}
int query(int x,int y)
{
int l=0,r=len-max(x,y)+1;
while(l<=r)
{
int mid=l+r>>1;
if((hash[x+mid-1]-hash[x-1]*pow[mid]%mod+mod)%mod==
(hash[y+mid-1]-hash[y-1]*pow[mid]%mod+mod)%mod)
l=mid+1;
else
r=mid-1;
}
return r;
}
int main()
{
int n,m,i,j,k,x,y;
pow[0]=1;
for(i=1;i<N;i++)
pow[i]=pow[i-1]*131%mod;
scanf("%s",str+1);
n=len=strlen(str+1);
scanf("%d",&m);
Build(1);
for(i=1;i<=n;i++)
p[i]=i;
for(i=1;i<=m;i++)
{
scanf("%s",op);
if(op[0]=='I')
{
scanf("%s%d",op,&x);
if(x>len) x=len+1;
memcpy(str+x+1,str+x,sizeof(char)*(len-x+1));
str[x]=op[0];
for(j=n;j>=1;j--)
{
if(p[j]>=x)
p[j]++;
else
break;
}
len++;
Build(x);
}
else
{
scanf("%d%d",&x,&y);
printf("%d\n",query(p[x],p[y]));
}
}
return 0;
}
poj-2758 Checking the Text的更多相关文章
- POJ 2758 Checking the Text(Hash+二分答案)
[题目链接] http://poj.org/problem?id=2758 [题目大意] 给出一个字符串,支持两个操作,在任意位置插入一个字符串,或者查询两个位置往后的最长公共前缀,注意查询的时候是原 ...
- poj 2758 && BZOJ 2258 Checking the Text 文本校对
Description 为了给Wind买生日礼物,Jiajia不得不找了一份检查文本的工作.这份工作很无聊:给你一段文本 要求比对从文本中某两个位置开始能匹配的最大长度是多少.但比无聊更糟糕的是, ...
- POJ2758 Checking the Text 哈希
注意到插入次数挺少的,于是每次暴力重构,然后哈希+二分 #include<cstdio> #include<iostream> #include<algorithm> ...
- POJ2758 Checking the Text
题目链接:https://vjudge.net/problem/POJ-2758 题目大意: 先给出一串原始字符串,在此基础上执行两种操作: 1.在第 p 个字符前添加字符 ch,如果 p 比现字符串 ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
- kuangbin带你飞 后缀数组 题解
2份模板 DC3 . 空间复杂度O3N 时间复杂度On #define F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb)) #define G(x) ((x) < ...
- [kuangbin带你飞]专题1-23题目清单总结
[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...
- ACM--[kuangbin带你飞]--专题1-23
专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- js实现点击复制网页内容(基于execCommand)
通过execCommand方法来实现,当一个HTML文档切换到设计模式 designMode时,文档对象暴露 execCommand 方法,该方法允许运行命令来操纵可编辑区域的内容.大多数命令影响文档 ...
- Java IO(三) 之 FileInputStream
前言: 对于文件系统中的文件.都能够使用FileInputStream流类以二进制的形式进行读取.可是因为Java本身的定位在JVM之上,没有处理计算机底层的能力.因此一些涉及底层处理的方法都是使用n ...
- POJ 2773
不经意看见dis后的“mod”一词后,瞬间有了思路,点进去看,却发现别人想的和我的不一样——! 我是这样想的,利用的是剩余系+欧几里德带余除法的性质. 若两者GCD=1,则必有除数和余数GCD=1.于 ...
- BestCoder Round #52 (div.2) HDU 5418 Victor and World (DP+状态压缩)
[题目链接]:pid=5418">click here~~ [题目大意]: 问题描写叙述 经过多年的努力,Victor最终考到了飞行驾照. 为了庆祝这件事,他决定给自己买一架飞机然后环 ...
- [Angular] Read Custom HTTP Headers Sent by the Server in Angular
By default the response body doesn’t contain all the data that might be needed in your app. Your ser ...
- linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'
解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如 ...
- Java的接口总结
Java最主要的封装是class.除此之外还有接口interface. 这段时间一直在想接口有什么作用呢.有了接口有哪些优点呢.结合网络上各位大神的文章,接口的作用大概体如今下面几个方面. 1.回调 ...
- node 内存消息队列
var net = require('net') var clients = [] ,msgs = {} function unWrapMsg(data){ data = data.toString( ...
- Binary Tree Inorder Traversal--leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题目大意:中序遍历二叉树 解题思路:中序遍历二叉树.中序遍历二 ...
- caffe中lenet_solver.prototxt配置文件注解
caffe框架自带的例子mnist里有一个lenet_solver.prototxt文件,这个文件是具体的训练网络的引入文件,定义了CNN网络架构之外的一些基础参数,如总的迭代次数.测试间隔.基础学习 ...