Trie树板题段错误求调
  • 板块学术版
  • 楼主Rainsleep
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/10/17 21:30
  • 上次更新2023/10/27 07:04:36
查看原帖
Trie树板题段错误求调
666796
Rainsleep楼主2022/10/17 21:30
#include<bits/stdc++.h>

using namespace std;

const int N = 2e6 + 10;

int idx = 0, cnt[N], tr[N][26] ;

string s;

void update()
{
    
    int p = 0;
    
    for(int i(0);i < s.length(); ++ i)
    {
        int to = s[i] - 'a';
        
        if(!tr[p][to])
            tr[p][to] = ++ idx;
        
        p = tr[p][to];
    }
    
    
    ++ cnt[p];
}

int query()
{
    int p = 0;
    
    for(int i(0);i < s.length(); ++ i)
    {
        int to = s[i] - 'a';
        
        if(!tr[p][to])
            return 0;
        
        p = tr[p][to];
    }
    
    return cnt[p];
}

int main()
{
    
    int Q;
    
    scanf("%d", &Q);
    
    while(Q -- )
    {
        char op;
        
        cin >> op >> s;
        
        if(op == 'I')
            update();
        else
            printf("%d\n", query());
    }
    
    return 0;
}

qwq谢谢

2022/10/17 21:30
加载中...