String matching (2) - Trie: dictionary tree

1. Introduction

Trie, also known as dictionary tree or prefix tree, is an ordered tree-like data structure used to store associative arrays, the keys of which are usually strings.

2. Function

Make many strings into a set of strings, and you can quickly find them (this article takes how many words are the prefix of a sentence as an example).

3. Realization

Trie is a rooted tree, it must have a root node (we can record the root node as 0), each edge has a character weight (you can also take the ID of this character as a weight), and the node on the What is stored is the number of words for the node.

We can use son[i][j] to represent the son of the ith node numbered j, and use w[i] to store the number of words of the ith node, so that a Trie can be constructed.

The time complexity of constructing a Trie is O(∑ string length), and the query time is O(maximum string length), but in practice, the complexity may be reduced.

It can be said that the efficiency of Trie is relatively high.

4. Template

//Title: Enter a number n, then read in n words s[1]...s[n], then read in a number m, and then read in m sentences t[1]...t[ m], for each sentence t[i], find how many words are its prefixes
//This code only has lowercase letters by default
#include<bits/stdc++.h>
#define N 1000//Maximum number of strings
#define L 100//Maximum length of string
#define C 26 // total characters
using namespace std;
int n,m;
struct Trie
{
	int len,son[N*L+5][C],w[N*L+5];//The number of nodes stored in len, the son[] array is used to store the child nodes of each node, w[] The array stores the number of words per node
	int getID(char x) {return x-'a';} //Get the ID of this character
	void Insert(string s) //Insert string s into Trie
	{
		int Now=0;
		for(int i=0;i<s.length();i++)
		{
	 		if(son[Now][getID(s[i])]==0) son[Now][getID(s[i])]=++len;//If the current character does not appear, add it child nodes of the current node
			Now=son[Now][getID(s[i])];//Continue to operate
		}
		w[Now]++;//The final Now is the word node, add 1 to the number of words in this node
	}
	int get_pre(string s) //find the number of prefixes of string s
	{
		int Now=0,res=0;//res statistical results
		for(int i=0;i<s.length();i++)
		{
			res+=w[Now];//Add the number of words of the current node
			if(son[Now][getID(s[i])]!=0) Now=son[Now][getID(s[i])];//Continue to operate
			else return res;//If the current character is not a child node of the current node, return the result
		}
		return res+w[Now];
	}
} t;
intmain()
{
	string x;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) cin>>x,t.Insert(x);
	scanf("%d",&m);
	for(int i=1;i<=m;i++) cin>>x,printf("%d\n",t.get_pre(x));
	return 0;
}




Note: If you learned about the Trie algorithm through this article, please like it before leaving. Of course, you are also welcome to point out the shortcomings of this article in the discussion area, and the author will correct this article in time

Copyright statement: Please indicate the address for reprinting

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325756822&siteId=291194637