OJSF 信息学 Wiki · 第 121 / 144 节 · K105

第 121 节|K105 Trie 与前缀统计

独立过关 · 样例与错因 · 课末题

目标:区分“走得通”“恰好是词”“有几个词经过”,不按单词总数线性扫描。建议 75 分钟。

1. 起点检查(8 分钟)

只录入 apple 后,查 app:路径存在,但 app 并未作为单词录入。写下这两个问题分别应回答什么。

2. 用一条路径表示一个前缀(15 分钟)

根代表空前缀,边表示下一个字符。同一前缀共用节点。节点 u 的 pass 是以该前缀开头的已录入词份数,end 是在这里结束的份数。同一个 app 插两次,pass 和 end 都应该加两次。按字母沿边走,孩子编号 0 专门代表缺边,根也编号 0 但不作为孩子指回。

对存了 26 个 4 字节 int 的节点,500001 个节点光孩子数组就约 52 MB,不含标记和 vector 扩容。固定 26 字母用数组,单字符转移 O(1),每个单词 O(长度)。节点数至多总插入字符数+1;数组版每个节点至少 26 个 int,请先估空间。实际字符集大且分支稀疏时,考虑字典边或排序后前缀区间,别机械开超大数组。

3. 手推(10 分钟)

插入 app、apple、app、apt 后,ap 的 pass=4,app 的 pass=3、end=2;ap 的 end=0。删除一个 app 后,ap 的 pass=3,app 的 pass=2、end=1;apple 仍能查到。

4. 改一点再做(8 分钟)

删除不存在的 ap 会怎样?虽然路径存在,end=0,必须什么也不改。删除后留下 pass=0 的空节点不影响正确性;本课不回收编号。

5. 独立过关(16 分钟)

最初空字典。第一行 q,接着 q 行 op s;1 插入一份 s,2 删除一份 s(不存在忽略),3 输出以 s 开头的词份数。1≤q≤2×10^5,所有字符串均非空小写,总字符数≤2×10^5。

8
1 app
1 apple
1 app
3 app
2 ap
2 app
3 app
3 ap

逐行输出 3、2、2。

6. 自查与复测

展开:删错时先看 end

先走完整路径确认 end>0,再走第二遍扣 pass;删不存在的词不能扣任何数据。复测只加 abc,然后查 ab 得 1;删 ab 再查 ab 仍得 1;删 abc 再查 ab 得 0。

展开独立题完整 C++14 参考(先独立通过样例)
#include <array>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Node{
    array<int,26> next{};
    int pass=0, terminal=0;
};
int main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int q;
    if(!(cin>>q)) return 0;
    vector<Node> t(1);
    while(q--){
        int op; string s;cin>>op>>s;
        int u=0;
        if(op==1){
            for(char c:s){
                int z=c-'a';
                if(!t[u].next[z]){
                    t[u].next[z]=static_cast<int>(t.size());
                    t.emplace_back();
                }
                u=t[u].next[z];
                ++t[u].pass; // 按份数统计经过这个前缀的单词。
            }
            ++t[u].terminal;
            continue;
        }
        for(char c:s){
            u=t[u].next[c-'a'];
            if(!u) break;
        }
        if(op==3){cout<<(u?t[u].pass:0)<<'\n';continue;}
        if(!u || !t[u].terminal) continue; // 未查到完整词时不修改。
        --t[u].terminal;
        u=0;
        for(char c:s){
            u=t[u].next[c-'a'];
            --t[u].pass;
        }
    }
}

7. 经典题解析|P2580 于是他错误的点名开始了

题面。真题 n≤10^4,m≤10^5,名字只含小写字母且长度≤50。先输入名单(n 和 n 个名字),再输入 m 个查询。不是名单里的准确名字输出 WRONG;第一次点到名单中的名字输出 OK,后续输出 REPEAT。只把终止节点标为在名单中,查到路径但不是终止节点仍为 WRONG。不用计 pass;另加 called 标记是否已点过。输入名单 app、apple,查询 ap、app、app、apple,答案依次 WRONG、OK、REPEAT、OK。时间为输入字符总数的线性量,空间 O(节点数×26)。

#include <array>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Node{
    array<int,26> next{};
    bool terminal=false, called=false;
};
int main(){
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int n;
    if(!(cin>>n)) return 0;
    vector<Node> t(1);
    string s;
    while(n--){
        cin>>s;
        int u=0;
        for(char c:s){
            int x=c-'a';
            if(!t[u].next[x]){
                t[u].next[x]=static_cast<int>(t.size()); // 只保留编号,不持有失效引用。
                t.emplace_back();
            }
            u=t[u].next[x];
        }
        t[u].terminal=true; // 路径中间节点不一定是完整名字。
    }
    int m;cin>>m;
    while(m--){
        cin>>s;
        int u=0;
        for(char c:s){
            u=t[u].next[c-'a'];
            if(!u) break; // 查询不创建节点。
        }
        if(!u || !t[u].terminal) cout<<"WRONG\n";
        else if(t[u].called) cout<<"REPEAT\n";
        else { cout<<"OK\n";t[u].called=true; }
    }
}

上一节:KMP与前缀函数 · 回到课程目录 · 下一节:01-Trie与最大异或