#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int const N= 1000000;
struct Trie{
int id; // 标记每一个单词
int cnt; // 标记单词前缀的数量
int next[26]; // 26 个孩子结点
void init(){
id= 0; cnt= 0;
for( int i= 0; i< 26; ++i ) next= 0;
}
};
Trie tree[N];
int num= 0, root= 0, cid= 0;
void insert( char* s ){
int rt= root;
while( *s ){
int t= *s- 'a';
if( tree[rt].next[t]== 0 ){
tree[++num].init();
tree[rt].next[t]= num;
}
rt= tree[rt].next[t];
tree[rt].cnt++;
s++;
}
tree[rt].id= ++cid;
}
int find( char* s ){
int rt= root;
while( *s ){
int t= *s- 'a';
rt= tree[rt].next[t];
if( rt== 0 ) return 0;
s++;
}
return tree[rt].cnt;
}
int main(){
char str[100];
while( gets(str)!= NULL ){
if( strlen(str)== 0 ) break;
insert( str );
}
while( gets(str)!= NULL )
printf("%d\n", find(str) );
return 0;
}
Another implement: http://www.cnblogs.com/Xredman/archive/2009/04/10/1433167.html
分享到:
相关推荐
trie - 单词查找树实现Go实现,极快的前缀/模糊字符串搜索的数据结构和相关算法
marisa_trie-0.7.7-cp310-cp310-win_amd64
marisa_trie-0.7.7-cp37-cp37m-win32
hash trie树 字典树,完整的sdk开发包 具有说明文档
trie-0.1.1.tar
marisa_trie-0.7.7-cp38-cp38-win_amd64
marisa_trie-0.7.5-cp34-cp34m-win32
marisa_trie-0.7.5-cp27-cp27m-win32
php-ext-trie-filter-php7
HAT-trie: A Cache-conscious Trie-based Data Structure for StringsNikolas Askitis Ranjan SinhaSchool of Computer Science and Information Technology, RMIT University, Melbourne 3001, Australia. Email: {...
marisa_trie-0.7.5-cp27-cp27m-win_amd64
marisa_trie-0.7.5-cp35-cp35m-win_amd64
资源来自pypi官网,解压后可用。 资源全名:marisa_trie-0.7.6-cp38-cp38-win32.whl
ACM Trie树 模板,字典树模板,数据结构
marisa_trie-0.7.7-cp38-cp38-win32
marisa_trie-0.7.7-cp39-cp39-win32
marisa_trie-0.7.7-cp310-cp310-win32
marisa_trie-0.7.5-cp36-cp36m-win32
marisa_trie-0.7.5-cp35-cp35m-win32
资源来自pypi官网。 资源全名:marisa-trie-0.3.4.tar.gz