标签:blog io ar sp for div on log bs
#pragma once
#include <string>
using namespace std;
#define MAX_CHAR 26
struct node
{
bool isWord;
node* next[MAX_CHAR];
node()
{
isWord = false;
for(int i = 0;i<MAX_CHAR;i++)
next[i] = NULL;
}
};
class Tire
{
public:
Tire(){root = NULL;}
void insert(string& str)
{
if(!root)
root = new node;
node* location = root;
for(int i = 0;i< (int)str.length();i++)
{
int num = str[i] - ‘a‘;
if(!location->next[num])
{
location->next[num] = new node;
}
location = location->next[num];
}
location->isWord = true;
}
bool search(string& str)
{
if(!root) return false;
node* location = root;
for (int i = 0;i<(int)str.length();i++)
{
int num = str[i] - ‘a‘;
if(!location->next[num])
return false;
location = location->next[num];
}
return location->isWord;
}
public:
node* root;
};
标签:blog io ar sp for div on log bs
原文地址:http://www.cnblogs.com/kangbry/p/4089529.html