设为首页 加入收藏

TOP

hdu 1247 Hat’s Words (字典树模板)
2015-07-20 17:44:59 来源: 作者: 【 】 浏览:4
Tags:hdu 1247 Hat Words 字典 模板
//那个单词是有出现的两个单词构成的
# include 
  
   
# include 
   
     # include 
    
      # include 
     
       # define MAX 26 using namespace std; typedef struct Trie_Node { bool isWord; struct Trie_Node *next[MAX]; } Trie; char s[50000][50]; void insert(Trie *root,char *word)//生成字典树 { Trie *p=root; while(*word!='\0') { if(p->next[*word-'a']==NULL) { Trie *temp=(Trie*)malloc(sizeof(Trie)); for(int i=0; i
      
       next[i]=NULL; } temp->isWord=false; p->next[*word-'a']=temp; } p=p->next[*word-'a']; word++; } p->isWord=true; } bool search(Trie *root,char *word)//查找单词是否存在 { Trie *p=root; for(int i=0; word[i]!='\0'; i++) { if(p==NULL||p->next[word[i]-'a']==NULL) return false; p=p->next[word[i]-'a']; } return p->isWord; } void del(Trie *root)//释放空间 { for(int i=0; i
       
        next[i]!=NULL) { del(root->next[i]); } } free(root);//释放malloc申请的空间 } int main() { int i,j; int count=0; char str[50]; Trie *root=(Trie*)malloc(sizeof(Trie)); for(i=0; i
        
         next[i]=NULL; } root->isWord=false; while(~scanf("%s",str)) { strcpy(s[count++],str); insert(root,str); } for(i=0; i
         
          
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA 11256 - Repetitive Multiple.. 下一篇C++之类的成员函数的调用 vs. 普..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)
·Linux常用命令60条( (2025-12-25 00:50:40)
·nginx 监听一个端口 (2025-12-25 00:19:30)
·整个互联网就没有一 (2025-12-25 00:19:27)