Posts

Showing posts from December, 2017

Trie--HackerRank - contacts

#include<bits/stdc++.h> #include<bits/stdc++.h> #include<algorithm> #include<iostream> #include<istream> #include<stdio.h> #include<stdlib.h> using namespace std; struct node {     int c=0;     bool mark;     node* next[26+1];     node()     {         mark=false;         for(int i=0; i<26; i++)         {             next[i]=NULL;         }     } }; node* root=NULL; void Insert(string s) {     node* newnode=root;     for(int i=0; i<s.size(); i++)     {         int id=s[i]-'a';         if(newnode->next[id]==NULL)         {             newnode->next[id]=new node();         }         newnode=newnode->next[id];         newnode->c++;     }     newnode->mark=1; } int Search(string s) {     node* newnode=root;     for(int i=0; i<s.size(); i++)     {         int id=s[i]-'a';         if(newnode->next[id]==NULL)         {             return 0;         }         n

Trie---HackerRank no-prefix-set

#include<bits/stdc++.h> using namespace std; int k,id; string ss; struct node {     bool m;     node* next[26+1];     node()     {         m=false;         for(int i=0; i<26; i++)         {             next[i]=NULL;         }     } }; node *root =NULL; void Insert(string s,int len) {     node* a=root;     int x=len-1;     for(int i=0; i<len; i++)     {         int id = s[i]-'a';         if(a->next[id]!=NULL && i==x)         {             k=1;         }         if(a->next[id]==NULL)         {             a->next[id]=new node();         }         a=a->next[id];         if(a->m==true)         {             k=1;         }     }     a->m=1; } bool Search(string s,int len) {     node *a=root;     for(int i=0; i<len; i++)     {         int id = s[i]-'a';         if(a->next[id]==NULL)         {             return false;         }         a=a->next[id];     }     return a->m

Trie--UVA 11362-----Phone List

#include<bits/stdc++.h> #include<algorithm> #include<iostream> #include<istream> #include<stdio.h> #include<stdlib.h> using namespace std; int id,m; struct node {     bool mark;     node* next[11];     node()     {         mark=false;         for(int i=0; i<10; i++)         {             next[i]=NULL;         }     } }; node* root=NULL; void Insert(string s) {     int nay=0;     node* newnode=root;     for(int i=0; i<s.size(); i++)     {         int id=s[i]-'0';         if(newnode->next[id]==NULL)         {             nay=1;             newnode->next[id]=new node();         }         if(newnode->mark==1)         {             m=0;         }         newnode=newnode->next[id];     }     if(nay==0)     {         m=0;     }     newnode->mark=1; } bool Search(string s) {     node* newnode=root;     for(int i=0; i<s.size(); i++)     {         int id=s[i]-'0';