题目链接:https://vjudge.net/problem/POJ-2758

题目大意:

  先给出一串原始字符串,在此基础上执行两种操作:

  1、在第 p 个字符前添加字符 ch,如果 p 比现字符串的长度 nowlen 大,则添加到现字符串的末尾。注意,这里的所有位置都是相对于变化后的字符串而言的!

  2、查询第 i 个和第 j 个后缀的最长公共前缀。注意,这里的位置是相对于原始字符串而言的。

解题思路:

  这道题真的可以说是最近几个月我做的最痛苦的一道题之一了。真的很煎熬。

  一开始,我的思路是后缀数组加模拟(因为实在 kuangbin 的后缀数组专题里面碰到的嘛~)。发现其实在这种做法中后缀数组作用并不大,主要的工作都在于模拟字符的添加和最长公共前缀的查询,后缀数组只不过是起到了加速的作用,而且模拟的过程及其之麻烦,感觉不是很自然,所以就去查了一眼题解,发现还真的有人用后缀数组做的,扫了几眼他的思路,好像也是后缀数组加暴力的做法。。。所以我决定自己动手写一版。。。然后。。。一场悲剧。。。真的是各种不好写。。。好不容易把它调到可以过我能找到的所有样例和我自己造的几组数据。。。但还是各种WA。。。真的好痛苦。。。整整一天都调不出来。。。在此贴上代码,以后哪天再回过头来看看吧:

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm> using namespace std;
const int maxn=+,inf=0x7fffffff;
char inp[maxn];
int s[maxn];
int n,k;
int Rank[maxn],sa[maxn],tmp[maxn],height[maxn];
bool compare_sa(int i,int j){
if(Rank[i]!=Rank[j]) return Rank[i]<Rank[j];
else{
int ri=i+k<=n?Rank[i+k]:-;
int rj=j+k<=n?Rank[j+k]:-;
return ri<rj;
}
}
void construct_sa(){
for(int i=;i<=n;i++){
sa[i]=i;
Rank[i]=i<n?s[i]:-;
}
for(k=;k<=n;k*=){
sort(sa,sa+n+,compare_sa);
tmp[sa[]]=;
for(int i=;i<=n;i++){
tmp[sa[i]]=tmp[sa[i-]]+(compare_sa(sa[i-],sa[i])?:);
}
for(int i=;i<=n;i++){
Rank[i]=tmp[i];
}
}
}
void construct_height(){
int h=;
height[]=;
for(int i=;i<n;i++){
int j=sa[Rank[i]-];
if(h>) h--;
for(;j+h<n&&i+h<n;h++){
if(s[j+h]!=s[i+h]) break;
}
height[Rank[i]-]=h;
}
}
vector<int> add[];
set<int> inserts;
int main()
{
freopen("in.txt","r",stdin);
char ord[],ins[];
int have,a,b;
scanf("%s",inp);
n=strlen(inp);
int nowlen=n;
for(int i=;i<n;i++) s[i]=inp[i]-'A';
construct_sa();
construct_height();
scanf("%d",&have);
while(have--){
// for(int i=0;i<n;i++){
// if(inserts.count(i)){
// for(int j=0;j<add[i].size();j++) printf("%c",'A'+add[i][j]);
// }
// printf("%c",'A'+s[i]);
// }
// for(int i=0;i<add[n].size();i++) printf("%c",'A'+add[n][i]);
// printf("\n");
scanf("%s",ord);
if(ord[]=='Q'){
scanf("%d%d",&a,&b); //a<b
a--,b--;
if(a>b) swap(a,b);
int same=inf;
if(Rank[a]<Rank[b]){
for(int i=Rank[a];i<Rank[b];i++){
if(height[i]<same) same=height[i];
}
}
else{
for(int i=Rank[b];i<Rank[a];i++){
if(height[i]<same) same=height[i];
}
} bool go=true;
int ans=,last_a=a,last_b=b,last_a_in=,last_b_in=;
set<int>::iterator k1=inserts.upper_bound(a);
set<int>::iterator k2=inserts.upper_bound(b);
int p1=*k1,p2=*k2;
if(k1==inserts.end()) p1=inf;
if(k2==inserts.end()) p2=inf;
if(p1-a>same && p2-b>same){
printf("%d\n",same);
continue;
}
while(go){
// printf("p1: %d, p2: %d, ans: %d, last_b: %d, last_a: %d\n",p1,p2,ans,last_b,last_a);
if(p1-a>same && p2-b>same){
while(last_a<n&&last_b<n&&s[last_a++]==s[last_b++]) ans++;
break;
}
if(last_a_in&&last_b_in){
for(;last_a_in<add[p1].size()&&last_b_in<add[p2].size();last_a_in++,last_b_in++){
if(add[p1][last_a_in]!=add[p2][last_b_in]){
go=false; break;
}else ans++;
}if(!go) break;
if(last_a_in==add[p1].size()){
k1=inserts.upper_bound(p1);
last_a=p1,last_a_in=;
p1=*k1;
if(k1==inserts.end()) p1=inf;
}
if(last_b_in==add[p2].size()){
k2=inserts.upper_bound(p2);
last_b=p2,last_b_in=;
p2=*k2;
if(k2==inserts.end()) p2=inf;
}
}else if(last_a_in){
for(;last_a_in<add[p1].size()&&last_b<n&&last_b<p2;last_a_in++,last_b++){
if(add[p1][last_a_in]!=s[last_b]){
go=false; break;
}else ans++;
}if(!go) break;
if(last_a_in==add[p1].size()&&last_b==p2){
k1=inserts.upper_bound(p1);
last_a=p1,last_a_in=;
p1=*k1;
if(k1==inserts.end()) p1=inf; if(add[p2][last_b_in++]==s[last_a++]) ans++;
else{
go=false;
}
}else if(last_a_in==add[p1].size()){
k1=inserts.upper_bound(p1);
last_a=p1,last_a_in=;
p1=*k1;
if(k1==inserts.end()) p1=inf;
}else{
if(last_a_in<add[p1].size()&&last_b_in<add[p2].size()&&add[p2][last_b_in++]==add[p1][last_a_in++]) ans++;
else{
go=false;
}
}if(!go) break;
}else if(last_b_in){
for(;last_b_in<add[p2].size()&&last_a<n&&last_a<p1;last_b_in++,last_a++){
if(add[p2][last_b_in]!=s[last_a]){
go=false; break;
}else ans++;
}if(!go) break;
if(last_b_in==add[p2].size()&&last_a==p1){
k2=inserts.upper_bound(p2);
last_b=p2,last_b_in=;
p2=*k2;
if(k2==inserts.end()) p2=inf; if(add[p1][last_a_in++]==s[last_b++]) ans++;
else{
go=false;
}
}else if(last_b_in==add[p2].size()){
k2=inserts.upper_bound(p2);
last_b=p2,last_b_in=;
p2=*k2;
if(k2==inserts.end()) p2=inf;
}else{
if(add[p1][last_a_in++]==add[p2][last_b_in++]) ans++;
else{
go=false;
}
}if(!go) break;
}else{
if(p1-a<p2-b){
ans+=p1-last_a;
last_b+=(p1-last_a);
last_a=p1;
if(add[p1][last_a_in++]==s[last_b++]) ans++;
else
go=false;
}
else if(p1-a>p2-b){
ans+=p2-last_b;
last_a+=(p2-last_b);
last_b=p2;
if(add[p2][last_b_in++]==s[last_a++]) ans++;
else
go=false;
}
else{
if(p1<=n){
ans+=p1-last_a;
last_a=p1,last_b=p2;
if(add[p1][last_a_in++]==add[p2][last_b_in++]) ans++;
else
go=false;
}
else{
while(last_a<n&&last_b<n&&s[last_a++]==s[last_b++]) ans++;
go=false;
}
}
}
}
printf("%d\n",ans);
}
else{
scanf("%s %d",ins,&a);
a--;
// printf("nowlen: %d, a: %d\n",nowlen,a);
if(a>=nowlen){
add[n].push_back(ins[]-'A');
inserts.insert(n);
}
else{
int now=;
set<int>::iterator k3=inserts.upper_bound(now);
int last=*k3;
if(k3==inserts.end()) last=inf;
// printf("now: %d, last: %d\n",now,last);
if(last>a){
add[a].push_back(ins[]-'A');
inserts.insert(a);
}
else{
while(){
now=last-;
// printf("a: %d\n",a);
if(a-now>add[last].size()+){
a-=add[last].size();
now++;
k3=inserts.upper_bound(now);
last=*k3;
if(k3==inserts.end()) last=inf;
// printf("a: %d, now: %d, last: %d\n",a,now,last);
}
else if(a-now==add[last].size()+){
add[last].push_back(ins[]-'A');
break;
}
else{
// printf("pos: %d\n",a-now);
add[last].insert(add[last].begin()+a-now-,ins[]-'A');
break;
}
if(a-now<last-now){
add[a].push_back(ins[]-'A');
inserts.insert(a);
break;
}
}
}
}
++nowlen;
}
}
return ;
}

  后来实在受不了了,在网上学了一种哈希的做法,二分又写挂了。。。好累。。。

AC代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=;
unsigned long long hashs[maxn],pw[maxn];
char inp[maxn];
int len,old[maxn],nowlen;
void build(int s){
for(int i=s;i<=nowlen;i++)
hashs[i]=hashs[i-]*+inp[i];
} int query(int a,int b){
if(a>b) swap(a,b);
int l=,r=nowlen-b+,mid; //此处要把 l 和 r 之间的距离尽量压缩,不然会WA
int ans=;
while(l<r){
mid=(l+r+)/; //此处令mid稍微右偏,然后 r 修改的时候用 r=mid-1,稍微左偏,提高精度,不然也会WA
if(hashs[a+mid-]-hashs[a-]*pw[mid]==hashs[b+mid-]-hashs[b-]*pw[mid]) l=mid,ans=mid;
else r=mid-;
}
return ans;
}
int main(){
int have,a,b;
char ord[],ins[];
scanf("%s",inp+);
scanf("%d",&have);
nowlen=strlen(inp+);
len=nowlen;
pw[]=;
for(int i=;i<=len+;i++) pw[i]=pw[i-]*;
for(int i=;i<=len;i++) old[i]=i;
build();
for(int j=;j<have;j++){
scanf("%s",ord);
if(ord[]=='Q'){
scanf("%d%d",&a,&b);
printf("%d\n",query(old[a],old[b]));
}
else{
scanf("%s %d",ins,&a);
if(a>nowlen) a=nowlen+;
for(int i=nowlen;i>=a;i--) inp[i+]=inp[i];
inp[a]=ins[];nowlen++;
build(a);
for(int i=len;i>=;i--){
if(old[i]>=a) old[i]++;
else break;
}
}
}
return ;
}

POJ2758 Checking the Text的更多相关文章

  1. POJ2758 Checking the Text 哈希

    注意到插入次数挺少的,于是每次暴力重构,然后哈希+二分 #include<cstdio> #include<iostream> #include<algorithm> ...

  2. poj-2758 Checking the Text

    题意: 给定一个字符串,要求维护两种操作: I:在字符串中插入一个字符: Q:询问某两个位置開始的LCP. 插入操作<=200,字符串长度<=5w,查询操作<=2w: 题解: 第一道 ...

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

    [题目链接] http://poj.org/problem?id=2758 [题目大意] 给出一个字符串,支持两个操作,在任意位置插入一个字符串,或者查询两个位置往后的最长公共前缀,注意查询的时候是原 ...

  4. poj 2758 && BZOJ 2258 Checking the Text 文本校对

    Description   为了给Wind买生日礼物,Jiajia不得不找了一份检查文本的工作.这份工作很无聊:给你一段文本 要求比对从文本中某两个位置开始能匹配的最大长度是多少.但比无聊更糟糕的是, ...

  5. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  6. 环境搭建文档——Windows下的Git搭建详解

    Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.具体安装步骤如下: 第一步:先从官网下载最新版本的Git 官网地址:https://git-scm.com/do ...

  7. Git换行符是如何精确控制的

    Git换行符是如何精确控制的 Checkout Windows-style, commit Unix-style Git will convert LF to CRLF when checking o ...

  8. 在windows下执行./configure,make,makeinstall源码安装程序spice-gtk

    使用MSYS软件,在我的上一篇博客中有软件下载地址.本文使用MSYS进行源码编译spice-gtk-0.33. 首先打开MSYS软件,进入你源码所在目录,例如:cd  /c/Users/Admi... ...

  9. #Git 详细中文安装教程

    Step 1 Information 信息 Please read the following important information before continuing 继续之前,请阅读以下重要 ...

随机推荐

  1. Java反射机制概念及使用

    反射机制 —— 将类中的所有成员反射成对于的类. 以“com.test.Person”类为例                      转换对应的类                获取方法      ...

  2. HDU 5416 CBR and tree

    #include<bits/stdc++.h> using namespace std; #define for(i,a,b) for(int i=a;i<=b;++i) //T,N ...

  3. css之颜色表示法

    css之颜色表示法 十六进制颜色 所有浏览器都支持十六进制颜色值. 十六进制颜色是这样规定的:#RRGGBB,其中的 RR(红色).GG(绿色).BB(蓝色)十六进制整数规定了颜色的成分.所有值必须介 ...

  4. 8.Python中装饰器是什么?

    Python中装饰器是什么? A Python decorator is a specific change that we make in Python syntax to alter functi ...

  5. RHCS图形界面建立GFS共享下

    我们上面通过图形界面实现了GFS,我们这里使用字符界面实现 1.1.       系统基础配置 5台节点均采用相同配置. 配置/etc/hosts文件 # vi /etc/hosts 127.0.0. ...

  6. UVA-1 #1. A + B Problem

    给你两个数 aa 和 bb,请输出他们的和. 输入格式 一行,两个用空格隔开的整数 aa 和 bb. 输出格式 一个整数,表示 a+ba+b. 样例一 input 2 3 output 5 限制与约定 ...

  7. [NOI 2020 Online] 入门组T1 文具采购(洛谷 P6188)题解

    原题传送门 题目部分:(来自于考试题面,经整理) [题目描述] 小明的班上共有 n 元班费,同学们准备使用班费集体购买 3 种物品: 1.圆规,每个 7 元. 2.笔,每支 4 元. 3.笔记本,每本 ...

  8. spark系列-7、spark调优

    官网说明:http://spark.apache.org/docs/2.1.1/tuning.html#data-serialization 一.JVM调优 1.1.Java虚拟机垃圾回收调优的背景 ...

  9. spark系列-8、Spark Streaming

    参考链接:http://spark.apache.org/docs/latest/streaming-programming-guide.html 一.Spark Streaming 介绍 Spark ...

  10. uniapp 踩坑

    获取数据 可在 onLoad 生命周期中获取数据,接收一个参数 option 为上个页面传递的参数. 点击事件tap代替click 两者都会在点击时触发,但是在web手机端,clikc会有300ms延 ...