Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
2
Indexable
Never
#include<iostream>
#include<string>
using namespace std;

struct Node{

	int count;
	Node* Children[26];
};

Node* root;
Node* MakeNode()
{
	Node* node = new Node();
	node->count=0;
	for(int i=0;i<26;i++)
	{
		node->Children[i]=NULL;
	}
}
void insert(string s){
	Node*ptr = root;
	for(int i=0;i<s.size();i++)
	{
		int index = s[i]-'a';
		if(ptr->Children[index]==NULL)
		{
			ptr->Children[index]=new Node();
		}
		ptr = ptr->Children[index];
		ptr->count+=1;
	}
	
}
void erase(string s){
	Node*ptr = root;
	for(int i=0;i<s.size();i++)
	{
		int index = s[i]-'a';
		if(ptr->Children[index]==NULL)
		{
			return;
		}
		ptr = ptr->Children[index];
	}
	ptr->count-=1;
}
int get(string s)
{
	Node*ptr = root;
	for(int i=0;i<s.size();i++)
	{
		int index = s[i]-'a';
		if(ptr->Children[index]==NULL)
		{
			return 0;
		}
		ptr = ptr->Children[index];
	}
	return ptr->count;
}
int main()
{

	root = new Node();
	insert("abc");
	insert("cdf");
	insert("abd");
}