Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define P(x,y) make_pair(x,y)
- using namespace std;
- struct node;
- typedef node* pnode;
- struct node{
- int cnt;
- bool isend;
- pnode child[26];
- node(){ cnt=isend=0; memset(child , 0 , sizeof(child)); }
- };
- class Trie{
- public:
- pnode head;
- void Clear(pnode &it){
- if(!it) return;
- delete(it);
- for(int j=0;j<26;j++)
- Clear(it->child[j]);
- }
- void init(){
- Clear(head);
- head = new node();
- }
- void insert_(string str){
- pnode it = head;
- it->cnt++;
- for(auto ch : str){
- int let = ch-'a';
- if(it->child[let] == NULL) it->child[let] = new node();
- it = it->child[let];
- it->cnt++;
- }
- it->isend=1;
- }
- bool find_(string str){
- pnode it = head;
- for(auto ch : str){
- int let = ch - 'a';
- if(it->child[let] == NULL) it->child[let] = new node();
- it = it->child[let];
- }
- return it->isend;
- }
- };
- int main(){
- Trie T;
- T.init();
- T.insert_("asd");
- if(T.find_("asd")) puts("yeah");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement