HDOJ 1251 -- 统计难题 Trie
2013-03-07 13:51
519 查看
Trie的写法是很灵活的,感觉跟segment tree一样,要活用
/* PROG: 统计难题 ID : LANG: C++ */ #include <string> #include <cstdio> #include <cstdlib> #include <memory.h> #include <iostream> #include <algorithm> using namespace std; struct Trie_Node { int cnt; Trie_Node *branch[27]; Trie_Node():cnt( 0 ) { memset( branch, 0, sizeof(branch) ); }// Init }; class Trie { public: Trie(); void Trie_Insert( char ss[] ); int Trie_Find( char ss[] ); private: Trie_Node *root; }t; Trie::Trie() { root = new Trie_Node(); }// Trie void Trie::Trie_Insert( char ss[] ) { Trie_Node *ptr = root; Trie_Node *temp = NULL; int slen = strlen( ss ); for ( int i=0; i<slen ;++i ) { if ( ptr->branch[ ss[i]-'a' ]==NULL ) { temp = new Trie_Node(); ptr->branch[ ss[i]-'a' ] = temp; } ++( ptr->branch[ ss[i]-'a' ]->cnt ); ptr = ptr->branch[ ss[i]-'a' ]; }// Insert return ; }// Trie_Insert int Trie::Trie_Find( char ss[] ) { int i, slen = strlen( ss ); Trie_Node *ptr = root; for ( i=0; i<slen; ++i ) { ptr = ptr->branch[ ss[i]-'a' ]; if ( ptr==NULL ) break; } if ( i==slen ) return ( ptr->cnt ); else return 0; }// Trie_Find int main() { char vocabu[12]; while ( gets( vocabu ) ) { if ( !strcmp( vocabu, "" ) ) break; t.Trie_Insert( vocabu ); }// creat the trie while ( EOF != scanf("%s", vocabu) ) { printf("%d\n", t.Trie_Find( vocabu )); }// Find the prefix return 0; }
相关文章推荐
- HDOJ 1251 统计难题 (trie)
- HDOJ 1251 统计难题(j简单的字典树trie的应用)
- HDOJ 1251 -- 统计难题 Trie
- HDOJ-1251-统计难题 解题报告
- HDOJ 1251 统计难题 (字典树)
- HDOJ 1251 HDU 1251 统计难题 ACM 1251 IN HDU
- hdu_1251 统计难题 trie模版
- hdoj 1251 统计难题 【字典树】
- 统计难题 HDOJ --1251
- HDU 1251 统计难题 字典树入门(Trie)
- hdu_1251统计难题(字典树Trie)
- hdu_1251 统计难题 trie模版
- HDU1251——trie模板——统计难题
- HDOJ&nbsp;&nbsp;1251&nbsp;&nbsp;统计难题
- hdoj 1251 统计难题 【字典树】
- hdoj 1251 统计难题(经典字典树)
- (字典树Trie Tree)HDU 1251 统计难题
- HDOJ 1251 统计难题(trie树入门)
- hdu_1251 统计难题 trie模版
- hdoj-1251-统计难题【map】