设为首页 加入收藏

TOP

HDU 1251 统计难题-字典树
2015-07-22 20:10:01 来源: 作者: 【 】 浏览:5
Tags:HDU 1251 统计 难题 字典

?

基础字典树:

?

#include
  
   
#include
   
     char s[15]; struct node { int cnt; node *next[26]; void init() //初始化 { cnt=0; for(int i=0;i<26;i++) { next[i]=NULL; } } } ; void insert(node *root,char *s) { node *p=root; for(int i=0;s[i];i++) { int t=s[i]-'a'; if(p->next[t]==NULL) { p->next[t]=new node; p=p->next[t]; p->init() ; } else p=p->next[t]; p->cnt++; } } int find(node *root, char *s) { node *p=root; for(int i=0;s[i];i++) { int t=s[i]-'a'; p=p->next[t]; if(!p) return 0; } return p->cnt; } int main() { node *root=new node(); root->init() ; while(gets(s)&&strlen(s))//输入控制!!! { insert(root,s); } while(gets(s)) { printf(%d ,find(root,s)); } return 0; }
   
  


?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDOJ 2030 汉字统计(借此题来谈谈.. 下一篇poj 2031 Building a Space Stati..

评论

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