ACM Trie树 字典树模板
今天做微软实习生笔试题傻逼了,写了好久呀QAQ,手生了
/* * Author: NICK WONG * Created Time: 2015/9/3 13:56:58 * File Name: */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
#define out(x) cout<<#x<<": "<<x<<endl
const double eps(1e-8);
const int maxn=10100;
const long long maxint=-1u>>1;
const long long maxlong=maxint*maxint;
typedef long long lint;
#define maxnode 41000
#define sigma_size 26
struct trie
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
trie() { sz=1; memset(ch[0],0,sizeof(ch[0])); }
inline int idx(char c) { return c-'a'; }
void insert(char *s, int vv)
{
int u=0, n=strlen(s);
for (int i=0; i<n; i++)
{
int c=idx(s[i]);
if (ch[u][c]==0) //empty
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0; //not a word
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=vv;
}
bool search(char *s, int vv)
{
int u=0, n=strlen(s);
for (int i=0; i<n; i++)
{
int c=idx(s[i]);
if (ch[u][c]==0)
return false;
u=ch[u][c];
}
if (val[u]==0) return false;
return true;
}
};
void init()
{
}
void work()
{
}
int main()
{
init();
work();
return 0;
}
参考
6天通吃树结构—— 第五天 Trie树
《算法竞赛入门经典之训练指南》
HDU1004,1247,1254,1671