#Trie Trie树调试模板 20.09.21

调试模板

可以看看数据的变化

#include <bits/stdc++.h>

using namespace std;

const int N = 100010, Oi = 30, O2 = 6, Oj = 26;

int son[N][26], cnt[N], idx;
char str[N];

void out(){
	for(int i = 0; i < Oi; i ++){
		for(int j = 0; j < Oj; j ++){
			printf("%d ", son[i][j]);
		}
		printf("          %d" , i);
		puts("");
	}
	puts("");
	
//	for(int i = 0; i < O2; i ++)
//		printf("%d ", cnt[i]);
//	puts("");
//	puts("");
	
	for(int i = 0; i < O2; i ++)
		printf("%c ", str[i]);
	puts("");	
	puts("");
	
	puts("");
}

void insert(char *str)
{
    int p = 0;
    for (int i = 0; str[i]; i ++ )
    {
        int u = str[i] - 'a';
        if (!son[p][u]){
        	son[p][u] = ++ idx;
   			out();
        }
        p = son[p][u];
//        out();
    }
    cnt[p] ++ ;
//    out();
}

int query(char *str)
{
    int p = 0;
    for (int i = 0; str[i]; i ++ )
    {
        int u = str[i] - 'a';
        if (!son[p][u]) return 0;
        p = son[p][u];
        out();
    }
    return cnt[p];
}

int main()
{
	freopen("ttt.in", "r", stdin);
	freopen("ttt.out", "w", stdout);
	
	
    int n;
    scanf("%d", &n);
    while (n -- )
    {
        char op[2];
        scanf("%s%s", op, str);
        if (*op == 'I') insert(str);
        else printf("%d
", query(str));
    }

    return 0;
}

原文地址:https://www.cnblogs.com/yuanyulin/p/14026721.html