Trie | (Insert and Search)
PROBLEM :
Trie is an efficient information retrieval data structure. Use this data structure to store Strings and search strings. Your task is to use TRIE data structure and search the given string A. If found print 1 else 0.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines.
First line of each test case consist of a integer N, denoting the number of element in a Trie to be stored.
Second line of each test case consists of N space separated strings denoting the elements to be stored in the trie.
Third line of each test case consists of a String A to be searched in the stored elements.
Output:
Print the respective output in the respective line.
Constraints:
1<=T<=20
1<=N<=20
Example:
Input:
1
8
the a there answer any by bye their
the
Output:
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define ALPHABET 26
#define CHAR_TO_INDEX(c) ((int)c-(int)'a')
typedef struct TRIE
{
struct TRIE *children[ALPHABET] ;
bool isleaf ;
}trie ;
trie* CreateTrie()
{
trie *temp ;
temp=(trie*)malloc(sizeof(trie)) ;
temp->isleaf=false ;
for(int i=0;i<ALPHABET;i++)
temp->children[i]=NULL ;
return temp ;
}
void InsertTrie(trie* root,string str)
{
int level,index ;
int len=str.length() ;
trie *temp=root ;
for(level=0;level<len;level++)
{
index=CHAR_TO_INDEX(str[level]) ;
if(!temp->children[index])
temp->children[index]=CreateTrie() ;
temp=temp->children[index] ;
}
temp->isleaf=true ;
}
bool SearchTrie(trie *root,string str)
{
int level,index ;
int len=str.length() ;
trie *temp=root ;
for(level=0;level<len;level++)
{
index=CHAR_TO_INDEX(str[level]) ;
if(!temp->children[index])
return false ;
temp=temp->children[index] ;
}
return (temp!=NULL&&temp->isleaf) ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
trie *root=CreateTrie() ;
int no ;
cin>>no ;
string str ;
for(int i=0;i<no;i++)
{
cin>>str ;
InsertTrie(root,str) ;
}
cin>>str ;
cout<<SearchTrie(root,str)<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Trie is an efficient information retrieval data structure. Use this data structure to store Strings and search strings. Your task is to use TRIE data structure and search the given string A. If found print 1 else 0.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines.
First line of each test case consist of a integer N, denoting the number of element in a Trie to be stored.
Second line of each test case consists of N space separated strings denoting the elements to be stored in the trie.
Third line of each test case consists of a String A to be searched in the stored elements.
Output:
Print the respective output in the respective line.
Constraints:
1<=T<=20
1<=N<=20
Example:
Input:
1
8
the a there answer any by bye their
the
Output:
1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define ALPHABET 26
#define CHAR_TO_INDEX(c) ((int)c-(int)'a')
typedef struct TRIE
{
struct TRIE *children[ALPHABET] ;
bool isleaf ;
}trie ;
trie* CreateTrie()
{
trie *temp ;
temp=(trie*)malloc(sizeof(trie)) ;
temp->isleaf=false ;
for(int i=0;i<ALPHABET;i++)
temp->children[i]=NULL ;
return temp ;
}
void InsertTrie(trie* root,string str)
{
int level,index ;
int len=str.length() ;
trie *temp=root ;
for(level=0;level<len;level++)
{
index=CHAR_TO_INDEX(str[level]) ;
if(!temp->children[index])
temp->children[index]=CreateTrie() ;
temp=temp->children[index] ;
}
temp->isleaf=true ;
}
bool SearchTrie(trie *root,string str)
{
int level,index ;
int len=str.length() ;
trie *temp=root ;
for(level=0;level<len;level++)
{
index=CHAR_TO_INDEX(str[level]) ;
if(!temp->children[index])
return false ;
temp=temp->children[index] ;
}
return (temp!=NULL&&temp->isleaf) ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
trie *root=CreateTrie() ;
int no ;
cin>>no ;
string str ;
for(int i=0;i<no;i++)
{
cin>>str ;
InsertTrie(root,str) ;
}
cin>>str ;
cout<<SearchTrie(root,str)<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Comments
Post a Comment