【LittleXi】字典树

【LittleXi】字典树

没学字典树之前觉得好难,一开始学发现好简单。。。简直就是线段树的弟弟
简单来说和霍夫曼树很像,只是把01改成了26的表达形式
字典树模板:

int nex[1000100][26]{
    
    0}, cnt=0;
int exist[1000100]{
    
    0};

void insert(string s)
{
    
    
	int p = 0;
	for (int i = 0; i < s.size(); i++)
	{
    
    
		int c = s[i] - 'a';
		if (!nex[p][c])	nex[p][c] = ++cnt;
		p = nex[p][c];
	}
	exist[p] = 1;
}

int find(string s)
{
    
    
	int p = 0;
	for (int i = 0; i < s.size(); i++)
	{
    
    
		int c = s[i] - 'a';
		if (!nex[p][c]) return 0;
		p = nex[p][c];
	}
	return exist[p];
}

猜你喜欢

转载自blog.csdn.net/qq_68591679/article/details/127761575