[JSOI2008]火星人

本文详细介绍了一种结合Splay树和Hash算法的数据结构实现,用于高效处理字符串操作,包括计算最长相同子串长度、字符替换及插入。通过O(n*log^2)的时间复杂度,实现了对大规模数据的有效管理和快速查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题干:给你一段区间,并给你m个操作,操作包含:

1.计算以两点为起点的最长相同串长度。

2.将某字符改为另一字符。

3.在某位置后加入另一字符。

splay的题,还要配上hash,套上log级的验证,时间O(n*log^2);

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100500
#define seed 2333
#define ull unsigned long long
char ch[N],c[2];
int n,m,rt,tot,a[N];
ull mi[2*N];
struct Splay
{
    int ch[2],fa,vl,siz;
    ull hs;
}tr[N];
void update(int u)
{
    int l = tr[u].ch[0],r = tr[u].ch[1];
    tr[u].siz = tr[l].siz+tr[r].siz+1;
    tr[u].hs = tr[l].hs*mi[tr[r].siz+1]+tr[u].vl*mi[tr[r].siz]+tr[r].hs;
}
void rotate(int x)
{
    int y = tr[x].fa,z = tr[y].fa,k=(tr[y].ch[1]==x);
    tr[y].ch[k]=tr[x].ch[k^1];tr[tr[x].ch[k^1]].fa=y;
    tr[x].ch[k^1]=y;tr[y].fa=x;
    tr[z].ch[tr[z].ch[1]==y]=x;tr[x].fa=z;
    update(y);update(x);
}
void splay(int x,int goal)
{
    while(tr[x].fa!=goal)
    {
        int y = tr[x].fa,z = tr[y].fa;
        if(z!=goal)
            ((tr[y].ch[1]==x)^(tr[z].ch[1]==y))?rotate(x):rotate(y);
        rotate(x);
    }
    if(!goal)rt=x;
}
void build(int l,int r,int f)
{
    if(l>r)return ;
    int mid = (l+r)>>1;
    if(l==r)
    {
        tr[mid].hs = a[l];
    }else
    {
        build(l,mid-1,mid);
        build(mid+1,r,mid);
    }
    tr[mid].vl = a[mid];
    tr[mid].fa = f;
    tr[f].ch[mid>=f]=mid;
    update(mid);
}
int find(int x,int k)
{
    int t = tr[tr[x].ch[0]].siz;
    if(t>=k)return find(tr[x].ch[0],k);
    else if(k==t+1)return x;
    else return find(tr[x].ch[1],k-1-t);
}
int deal(int x,int y)
{
    x = find(rt,x),y = find(rt,y);
    splay(x,0);splay(y,x);
    return tr[y].ch[0];
}
ull query(int x,int y)
{
    x = deal(x,y+1);
    return tr[x].hs;
}
int get(int x,int y)
{
    int ans = 0;
    for(int i=20;i>=0;i--)
    {
        if(y+(1<<i)>n+1||x+(1<<i)>n+1)continue;
        if(query(x,x+(1<<i))==query(y,y+(1<<i)))
            x+=(1<<i),y+=(1<<i),ans|=(1<<i);
    }
    return ans;
}
void cg(int x,int k)
{
    x = find(rt,x);
    splay(x,0);
    tr[x].vl = k;
    update(x);
}
void insert(int x,int k)
{
    int l = find(rt,x),r = find(rt,x+1);
    splay(l,0);splay(r,l);
    tot++;
    tr[tot].vl = tr[tot].hs = k;
    tr[tot].fa = r;
    tr[r].ch[0]= tot;
    tr[tot].siz=1;
    update(r);
    update(rt);
}
int main()
{
    scanf("%s%d",ch+1,&m);
    n = strlen(ch+1);
    mi[0]=1;
    for(int i=1;i<=200000;i++)mi[i]=mi[i-1]*seed;
    for(int i=1;i<=n;i++)a[i+1]=ch[i]-'a'+1;
    build(1,n+2,0);
    rt=(3+n)>>1;
    tot = n+2;
    int x,y;
    for(int i=1;i<=m;i++)
    {
        scanf("%s%d",c,&x);
        if(c[0]=='Q')
        {
            scanf("%d",&y);
            printf("%d\n",get(x,y));
        }else if(c[0]=='R')
        {
            scanf("%s",c);
            cg(x+1,c[0]-'a'+1);
        }else
        {
            scanf("%s",c);
            n++;
            insert(x+1,c[0]-'a'+1);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/LiGuanlin1124/p/9608700.html

``` #include<bits/stdc++.h> #define ll long long using namespace std; struct Node{ int v,p,sz; unsigned int he; Node *cl,*cr; Node(int x):v(x),p(rand()),sz(1),he(x),cl(nullptr),cr(nullptr){} ~Node(){ delete cl; delete cr; } friend int siz(Node *x){ if(x==nullptr)return 0; return x->sz; } void push_up(){ sz=1; he=v*(1u<<siz(cl)); if(cl!=nullptr){ sz+=cl->sz; he+=cl->he; } if(cr!=nullptr){ sz+=cr->sz; he+=(1u<<(siz(cl)+1))*cr->he; } } friend Node* merge(Node *x,Node *y){ if(x==nullptr)return y; if(y==nullptr)return x; if(x->p<y->p){ x->cr=merge(x->cr,y); x->push_up(); return x; }else{ y->cl=merge(x,y->cl); y->push_up(); return y; } } friend Node* split(Node *&x,int r){ if(x==nullptr)return nullptr; if(siz(x->cl)>=r){ Node *t=split(x->cl,r); swap(t,x->cl); x->push_up(); swap(t,x); return t; }else{ Node *t=split(x->cr,r-siz(x->cl)-1); x->push_up(); return t; } } friend void change(Node *&h,int x,Node w){ Node *wr=split(h,x),*dq=split(h,x-1); delete dq; h=merge(h,merge(new Node(w),wr)); } friend void add(Node *&h,int x,Node w){ Node *wr=split(h,x); h=merge(h,merge(new Node(w),wr)); } }; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin>>s; Node *tr1=nullptr,*tr2=nullptr; for(int i=0;i<s.size();++i){ tr1=merge(tr1,new Node(s[i])); tr2=merge(tr2,new Node(s[i])); } int T; cin>>T; while(T--){ char op; cin>>op; if(op=='Q'){ int x,y; cin>>x>>y; Node *r1=split(tr1,x),*r2=split(tr2,y); int ans=0; for(int i=20;i>=0;--i){ if(ans+(1<<i)>min(r1->sz,r2->sz))continue; Node *rr1=split(r1,ans+(1<<i)),*rr2=split(r2,ans+(1<<i)); if(r1->he==r2->he)ans+=1<<i; merge(r1,rr1); merge(r2,rr2); } cout<<ans; tr1=merge(tr1,r1); tr2=merge(tr2,r2); }else if(op=='R'){ int x; char c; cin>>x>>c; change(tr1,x,Node(c)); change(tr2,x,Node(c)); }else{ int x; char c; cin>>x>>c; add(tr1,x,Node(c)); add(tr2,x,Node(c)); } } delete tr1; delete tr2; return 0; }```# P4036 [JSOI2008] 火星人 ## 题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号: ``` 序号 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m ``` 现在,火星人定义了一个函数 $LCQ(x, y)$,表示:该字符串中第 $x$ 个字符开始的字串,与该字符串中第 $y$ 个字符开始的字串,两个字串的公共前缀的长度。比方说,$LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0$ 在研究 $LCQ$ 函数的过程中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出 $LCQ$ 函数的值;同样,如果求出了 $LCQ$ 函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取 $LCQ$ 函数的快速算法,但不甘心认输的地球人又给火星人出了个难题:在求取 $LCQ$ 函数的同时,还可以改变字符串本身。具体地说,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此复杂的问题中,火星人是否还能够做到很快地求取 $LCQ$ 函数的值。 ## 输入格式 第一行给出初始的字符串。第二行是一个非负整数 $M$ ,表示操作的个数。接下来的M行,每行描述一个操作。操作有 $3$ 种,如下所示 1. 询问。语法:$Q$ $x$ $y$ ,$x$ ,$y$ 均为正整数。功能:计算 $LCQ(x,y)$ 限制:$1$ $\leq$ $x$ , $y$ $\leq$ 当前字符串长度 。 2. 修改。语法:$R$ $x$ $d$,$x$ 是正整数,$d$ 是字符。功能:将字符串中第 $x$ 个数修改为字符 $d$ 。限制:$x$ 不超过当前字符串长度。 3. 插入:语法:$I$ $x$ $d$ ,$x$ 是非负整数,$d$ 是字符。功能:在字符串第 $x$ 个字符之后插入字符 $d$ ,如果 $x=0$,则在字符串开头插入。限制:$x$ 不超过当前字符串长度 ## 输出格式 对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。 ## 输入输出样例 #1 ### 输入 #1 ``` madamimadam 7 Q 1 7 Q 4 8 Q 10 11 R 3 a Q 1 7 I 10 a Q 2 11 ``` ### 输出 #1 ``` 5 1 0 2 1 ``` ## 说明/提示 1. 所有字符串自始至终都只有小写字母构成。 2. $M\leq150,000$ 3. 字符串长度L自始至终都满足$L\leq100,000$ 4. 询问操作的个数不超过 $10,000$ 个。 对于第 $1$,$2$ 个数据,字符串长度自始至终都不超过 $1,000$ 对于第 $3$,$4$,$5$ 个数据,没有插入操作。 2024/07/40 更新一组 hack。 debug RE
04-05
``` #include<bits/stdc++.h> #define ll long long using namespace std; struct Node{ int v,p,sz; unsigned ll he; Node *cl,*cr; Node(int x):v(x),p(rand()),sz(1),he(x),cl(nullptr),cr(nullptr){} ~Node(){ delete cl; delete cr; } friend int siz(Node *x){ if(x==nullptr)return 0; return x->sz; } void push_up(){ sz=1; he=v*(1u<<siz(cl)); if(cl!=nullptr){ sz+=cl->sz; he+=cl->he; } if(cr!=nullptr){ sz+=cr->sz; he+=(1ull<<(siz(cl)+1))*cr->he; } } friend Node* merge(Node *x,Node *y){ if(x==nullptr)return y; if(y==nullptr)return x; if(x->p<y->p){ x->cr=merge(x->cr,y); x->push_up(); return x; }else{ y->cl=merge(x,y->cl); y->push_up(); return y; } } friend Node* split(Node *&x,int r){ if(x==nullptr)return nullptr; if(siz(x->cl)>=r){ Node *t=split(x->cl,r); swap(t,x->cl); x->push_up(); swap(t,x); return t; }else{ Node *t=split(x->cr,r-siz(x->cl)-1); x->push_up(); return t; } } friend void change(Node *&h,int x,Node w){ Node *wr=split(h,x),*dq=split(h,x-1); delete dq; h=merge(h,merge(new Node(w),wr)); } friend void add(Node *&h,int x,Node w){ Node *wr=split(h,x); h=merge(h,merge(new Node(w),wr)); } }; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin>>s; Node *tr1=nullptr,*tr2=nullptr; for(int i=0;i<s.size();++i){ tr1=merge(tr1,new Node(s[i]-'a'+1)); tr2=merge(tr2,new Node(s[i]-'a'+1)); } int T; cin>>T; while(T--){ char op; cin>>op; if(op=='Q'){ int x,y; cin>>x>>y; Node *r1=split(tr1,x-1),*r2=split(tr2,y-1); int ans=0; for(int i=20;i>=0;--i){ if(ans+(1<<i)>min(siz(r1),siz(r2)))continue; Node *rr1=split(r1,ans+(1<<i)),*rr2=split(r2,ans+(1<<i)); if(r1->he==r2->he)ans+=1<<i; merge(r1,rr1); merge(r2,rr2); } cout<<ans<<endl; tr1=merge(tr1,r1); tr2=merge(tr2,r2); }else if(op=='R'){ int x; char c; cin>>x>>c; change(tr1,x,Node(c-'a'+1)); change(tr2,x,Node(c-'a'+1)); }else{ int x; char c; cin>>x>>c; add(tr1,x,Node(c-'a'+1)); add(tr2,x,Node(c-'a'+1)); } } delete tr1; delete tr2; return 0; }```debug # P4036 [JSOI2008] 火星人 ## 题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。 比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号: ``` 序号 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m ``` 现在,火星人定义了一个函数 $LCQ(x, y)$,表示:该字符串中第 $x$ 个字符开始的字串,与该字符串中第 $y$ 个字符开始的字串,两个字串的公共前缀的长度。比方说,$LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0$ 在研究 $LCQ$ 函数的过程中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出 $LCQ$ 函数的值;同样,如果求出了 $LCQ$ 函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取 $LCQ$ 函数的快速算法,但不甘心认输的地球人又给火星人出了个难题:在求取 $LCQ$ 函数的同时,还可以改变字符串本身。具体地说,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此复杂的问题中,火星人是否还能够做到很快地求取 $LCQ$ 函数的值。 ## 输入格式 第一行给出初始的字符串。第二行是一个非负整数 $M$ ,表示操作的个数。接下来的M行,每行描述一个操作。操作有 $3$ 种,如下所示 1. 询问。语法:$Q$ $x$ $y$ ,$x$ ,$y$ 均为正整数。功能:计算 $LCQ(x,y)$ 限制:$1$ $\leq$ $x$ , $y$ $\leq$ 当前字符串长度 。 2. 修改。语法:$R$ $x$ $d$,$x$ 是正整数,$d$ 是字符。功能:将字符串中第 $x$ 个数修改为字符 $d$ 。限制:$x$ 不超过当前字符串长度。 3. 插入:语法:$I$ $x$ $d$ ,$x$ 是非负整数,$d$ 是字符。功能:在字符串第 $x$ 个字符之后插入字符 $d$ ,如果 $x=0$,则在字符串开头插入。限制:$x$ 不超过当前字符串长度 ## 输出格式 对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。 ## 输入输出样例 #1 ### 输入 #1 ``` madamimadam 7 Q 1 7 Q 4 8 Q 10 11 R 3 a Q 1 7 I 10 a Q 2 11 ``` ### 输出 #1 ``` 5 1 0 2 1 ``` ## 说明/提示 1. 所有字符串自始至终都只有小写字母构成。 2. $M\leq150,000$ 3. 字符串长度L自始至终都满足$L\leq100,000$ 4. 询问操作的个数不超过 $10,000$ 个。 对于第 $1$,$2$ 个数据,字符串长度自始至终都不超过 $1,000$ 对于第 $3$,$4$,$5$ 个数据,没有插入操作。 2024/07/40 更新一组 hack。
04-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值