OJSF 信息学 Wiki · 第 124 / 144 节 · T17

第 124 节|T17 字符串与平衡树训练

样例与错因 · 课末题

85 分钟:读题 5′、A 30′、B 35′、复测 15′。两题独立,满分 100;均为自编训练。

1. 开始前

允许先写小规模基准。A 先定义“整次匹配都在区间内”;B 先弄清计数是按不同键还是按份数。答案默认折叠,限时结束再打开。

2. A|区间内有几次(45 分)

输入文本 s、模式 p(各为非空小写串,长度≤2×10^5),再读 q(1≤q≤2×10^5)和 q 行 l r(1 基闭区间)。逐行统计 p 在 [l,r] 内完整出现的次数,重叠也算。分组:A1 n,q≤80(15);A2 只问整个文本(10);A3 任意区间、重叠和短于模式的查询(20)。

aaaaa aaa
4
1 5
2 4
2 3
3 5

逐行输出 3、1、0、1。

3. B|可重排名册(55 分)

起初为空。第一行 q(1≤q≤2×10^5)。随后 q 行 op x:1 插入 x;2 删除一份 x(不存在则忽略);3 查询严格比 x 小的元素份数;4 按值递增找第 x 份(1 基),越界输出 None。1…3 的键 |x|≤10^9,4 有 |x|≤2×10^5。分组:B1 q≤200(15);B2 不删除且键互异(15);B3 任意合法输入,含重复、负键、空集合(25)。

9
4 1
1 5
1 2
1 5
3 5
4 3
2 5
4 3
3 6

逐行输出 None、1、5、None、2。

4. 提示阶梯

A:只统计起点够吗?

给每个起点标记该处是否完整匹配;查询时资格区间是 [l,r−|p|+1]。例如查询 [2,3]、模式长 3,最后合法起点是 1,小于 2,直接回答 0。

B:连续两份相同值怎么办?

同一节点可记 cnt;其子树大小等于左右大小之和加 cnt。第 k 小若落在本节点的 cnt 个名次里,直接返回该键;严格查询不能把等值的 cnt 加入。

5. 独立复测

A:s=ababa、p=aba,分别询问 [1,4]、[2,5]、[2,4]。B:插入 −1 两次,删 7,再查第 2 个、严格小于 −1 的数量、第 0 个。

6. 看分组找错误

展开复测答案

A 为 1、1、0;最后的区间虽长度等于模式,起点却不匹配。A3 错:先检查右端减去 m−1,或匹配成功后是否保留重叠。B 为 −1、0、None;B3 错:检查删不存在是否误减,清空后是否先判断名次再查。

7. 综合题解析与参考程序

A:KMP 标记起点 + 前缀和

用 KMP 给 1 基匹配起点记 1。样例起点为 1、2、3;询问 [2,4] 时最后合法起点是 2,答案恰 1。前缀和定义 P[t]=1…t 起点的标记总和,答案 P[r−m+1]−P[l−1]。O(n+m+q) 时间、O(n+m) 空间。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int> prefixFunction(const string& p){
    int m=static_cast<int>(p.size());
    vector<int> pi(m);
    for(int i=1,j=0;i<m;++i){
        // j 是当前已知的候选边界长度;失配时改看下一条更短边界。
        while(j>0 && p[i]!=p[j]) j=pi[j-1];
        if(p[i]==p[j]) ++j;
        pi[i]=j;
    }
    return pi;
}
int main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);
    string s,p;int q;
    if(!(cin>>s>>p>>q)) return 0;
    auto pi=prefixFunction(p);
    int n=static_cast<int>(s.size()), m=static_cast<int>(p.size());
    vector<int> pref(n+1);
    for(int i=0,j=0;i<n;++i){
        while(j && s[i]!=p[j]) j=pi[j-1];
        if(s[i]==p[j]) ++j;
        if(j==m){
            pref[i-m+2]=1; // 标记 1 基起点,不是终点。
            j=pi[j-1];
        }
    }
    for(int i=1;i<=n;++i) pref[i]+=pref[i-1];
    while(q--){
        int l,r;cin>>l>>r;
        int last=r-m+1;
        cout<<(last<l?0:pref[last]-pref[l-1])<<'\n';
    }
}
B:FHQ-Treap 同键合份数

用两次分裂得到小于 x、等于 x、大于 x 三段;删一个只改中间节点的 cnt。合回时按键有序。查询 3 不改树,查询 4 先检查 x∈[1,size]。每操作期望 O(log q),不保证最坏;空间 O(q)。若允许先读全部操作,坐标压缩加树状数组也可做,但此处训练在线 Treap。

#include <iostream>
#include <random>
#include <utility>
#include <vector>
using namespace std;
class FHQ {
    struct Node {long long key;unsigned long long pri;int l,r,cnt,sz;};
    vector<Node> t{{0,0,0,0,0,0}};
    mt19937_64 gen{random_device{}()};
    int rt=0;
    void pull(int u) {t[u].sz=t[t[u].l].sz+t[t[u].r].sz+t[u].cnt;}
    pair<int,int> split(int u,long long x,bool eq) {
        if (!u) return {0,0};
        if (t[u].key<x || (eq && t[u].key==x)) {
            auto p=split(t[u].r,x,eq);
            t[u].r=p.first;pull(u);return {u,p.second};
        }
        auto p=split(t[u].l,x,eq);
        t[u].l=p.second;pull(u);return {p.first,u}; // 只更改跨过分界的一侧
    }
    int merge(int u,int v) {
        if (!u || !v) return u?u:v;
        if (t[u].pri>=t[v].pri) {
            t[u].r=merge(t[u].r,v);pull(u);return u;
        }
        t[v].l=merge(u,t[v].l);pull(v);return v;
    }
public:
    void alter(long long x,int add) {
        auto left=split(rt,x,false);
        auto mid=split(left.second,x,true); // <x、=x、>x 三段
        if (mid.first) {
            t[mid.first].cnt+=add;pull(mid.first);
            if (!t[mid.first].cnt) mid.first=0; // 每个不同键只建一个节点
        } else if (add>0) {
            t.push_back({x,gen(),0,0,1,1});mid.first=static_cast<int>(t.size())-1;
        }
        rt=merge(merge(left.first,mid.first),mid.second); // 恢复所有未修改的块
    }
    int size() const {return t[rt].sz;}
    int count(long long x,bool eq) const {
        int u=rt,ans=0;
        while (u) {
            if (t[u].key<x || (eq && t[u].key==x)) {
                ans+=t[t[u].l].sz+t[u].cnt;u=t[u].r;
            } else u=t[u].l;
        }
        return ans;
    }
    long long kth(int k) const {
        int u=rt;
        while (u) {
            int s=t[t[u].l].sz;
            if (k<=s) u=t[u].l;
            else if (k<=s+t[u].cnt) return t[u].key;
            else {k-=s+t[u].cnt;u=t[u].r;}
        }
        return 0; // 调用前校验名次
    }
};
int main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int q;
    if(!(cin>>q)) return 0;
    FHQ t;
    while(q--){
        int op,x;cin>>op>>x;
        if(op<=2) t.alter(x,op==1?1:-1);
        else if(op==3) cout<<t.count(x,false)<<'\n'; // 必须是严格小于。
        else if(x<1 || x>t.size()) cout<<"None\n"; // 0 与负值本身可作合法键。
        else cout<<t.kth(x)<<'\n';
    }
}

上一节:Manacher与回文半径 · 回到课程目录 · 下一节:加乘原理与组合数