免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4737 | 回复: 8
打印 上一主题 下一主题

[算法] 字典系列算法题目 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-09 15:18 |只看该作者 |倒序浏览
统计出,以某个字符串为前缀的,单词数量。  (单词本身,也是自己的前缀)



(字典先由用户输入(无序输入);然后再由用户输入一个字符串,要求系统输出所有以这个字符串为前缀的单词)


比如 :
请输入字典: banana   bamboo bee  absolute  band adam bra  boost (回车)
请输入待查找字符串:     ba(回车)
结果是:                     3

论坛徽章:
0
2 [报告]
发表于 2008-04-09 15:24 |只看该作者
trie树。

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
3 [报告]
发表于 2008-04-09 15:46 |只看该作者
原帖由 cugb_cat 于 2008-4-9 15:24 发表
trie树。

顶,那天忘了这个树的名字,还特意问了一下。

论坛徽章:
0
4 [报告]
发表于 2008-04-09 15:47 |只看该作者
原帖由 cjaizss 于 2008-4-9 15:46 发表

顶,那天忘了这个树的名字,还特意问了一下。

在水木上问的的?

论坛徽章:
0
5 [报告]
发表于 2008-04-09 16:29 |只看该作者
恩 好 trie树

继续  换个题

用户输入一堆无序数   要求找出所有的满足以下条件的数a(a可以被拆分为2个数b,c ,而且b,c也属于用户输入的这一堆数)

比如输入

a
ahat
hat
hatword
hziee
word

那么ahat hatword满足条件 需要被打印出来

(因为ahat可以被写为 a和hat...)

要求高效实现

论坛徽章:
0
6 [报告]
发表于 2008-04-09 16:31 |只看该作者
最后来一个 实际原理都是一样的..

就是看优化的如何了

给定一个字符串例如“appleuwebananasdf”,和一个词典库(里面有apple ,banana ……),将其分词。结果应该为 “apple uwe banana sdf ”。
注意考虑词典库的大小。

请给出实现代码,并描述其时间复杂度。

论坛徽章:
0
7 [报告]
发表于 2008-04-09 16:56 |只看该作者
原帖由 wilbur8415 于 2008-4-9 16:31 发表
最后来一个 实际原理都是一样的..

就是看优化的如何了

给定一个字符串例如“appleuwebananasdf”,和一个词典库(里面有apple ,banana ……),将其分词。结果应该为 “apple uwe banana sdf ”。
注意 ...

这个还是trie树,只要词典库是以trie树组织的就行。

论坛徽章:
1
双子座
日期:2015-01-04 14:25:06
8 [报告]
发表于 2008-04-10 09:52 |只看该作者
这段时间正好学C++ 加之以前了解了一下trie 写了一个程序 贴出来让大家批批

  1. #include <iostream>
  2. #include <queue>

  3. using namespace std;

  4. class trieNode{
  5.     private:
  6.         char key;
  7.         int value;
  8.         trieNode *next;
  9.         trieNode *child;

  10.     public:
  11.         friend class trie;
  12.         trieNode():key('\0'),value(-1),next(NULL),child(NULL){}
  13.         trieNode(char key,int value,trieNode *next,trieNode *child);
  14.         void setValue(int value){this->value = value;}
  15. };

  16. trieNode::trieNode(char key,int value,trieNode *next,trieNode *child){
  17.     this->key = key;
  18.     this->value = value;
  19.     this->next = next;
  20.     this->child = child;
  21. }

  22. class trie{
  23.     private:
  24.         trieNode head;
  25.     public:
  26.         trie():head('\0',-1,NULL,NULL){}
  27.         void addItem(const char *str,int value);
  28.         void printTrie();
  29.         void printNode(trieNode *first);
  30.         int findPre(const char *str);
  31. };

  32. void trie::addItem(const char *str,int value){
  33.     const char *p;
  34.     trieNode *root;
  35.     trieNode *t;
  36.     trieNode *pre;
  37.     trieNode *node;

  38.     if(str == NULL){
  39.         return ;
  40.     }
  41.     root = &head;
  42.    
  43.     for(p = str;;p++){
  44.         pre = NULL;
  45.         for(t = root->child;t != NULL;t = t->next){
  46.             if(t->key == *p){
  47.                 break;
  48.             }
  49.             pre = t;
  50.         }
  51.         if(t == NULL){
  52.             node = new trieNode(*p,-1,NULL,NULL);   
  53.             if(node == NULL){
  54.                 return ;
  55.             }
  56.             if(pre == NULL){
  57.                 root->child = node;
  58.             }
  59.             else{
  60.                 pre->next = node;
  61.             }
  62.             root = node;
  63.         }
  64.         else{
  65.             root = t;
  66.         }
  67.         if(*p == '\0'){
  68.             node->setValue(value);
  69.             break;
  70.         }
  71.     }
  72. }

  73. void trie::printTrie(){
  74.     queue<trieNode *> triequ[2];
  75.     trieNode *p;
  76.     trieNode *root;
  77.     int i = 0;
  78.    
  79.     triequ[i].push(&head);
  80.     while(!triequ[i].empty()){
  81.         while(!triequ[i].empty()){
  82.             root = triequ[i].front();
  83.             triequ[i].pop();
  84.             if(root == NULL){
  85.                 cout << endl;
  86.                 continue;
  87.             }
  88.             for(p = root->child;p != NULL;p = p->next){
  89.                 triequ[1 - i].push(p);
  90.                 cout << p->key << "(" << p->value << "," << root->key << ")\t";
  91.             }
  92.         }
  93.         cout << endl;
  94.         i = 1 - i;
  95.     }
  96. }

  97. int trie::findPre(const char *str){
  98.     int count = 0;
  99.     const char *p;
  100.     trieNode *root;
  101.     trieNode *t = NULL;
  102.     queue<trieNode *> triequ;
  103.    
  104.     if(str == NULL || *str == '\0'){
  105.         return count;
  106.     }
  107.     root = &head;
  108.    
  109.     for(p = str;*p != '\0';p++){
  110.         for(t = root->child;t != NULL;t = t->next){
  111.             if(t->key == *p){
  112.                 break;
  113.             }
  114.         }
  115.         if(t == NULL){
  116.             return count;
  117.         }
  118.         root = t;
  119.     }
  120.     triequ.push(t);
  121.     while(!triequ.empty()){
  122.         root = triequ.front();
  123.         triequ.pop();
  124.         for(t = root->child;t != NULL;t = t->next){
  125.             triequ.push(t);
  126.             if(t->key == '\0'){
  127.                 count++;
  128.             }
  129.         }
  130.     }
  131.    
  132.     return count;
  133. }

  134. int main(){
  135.     trie t;
  136.     t.addItem("banana",1);
  137.     t.addItem("bamboo",2);
  138.     t.addItem("bee",3);
  139.     t.addItem("absolute",4);
  140.     t.addItem("bane",5);
  141.     t.addItem("adam",6);
  142.     t.addItem("bra",7);
  143.     t.addItem("boost",8);

  144.     t.printTrie();

  145.     cout << t.findPre("bo") << endl;
  146.         
  147.     return 0;
  148. }
复制代码

[ 本帖最后由 yecheng_110 于 2008-4-10 09:57 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2008-05-31 23:14 |只看该作者
继续 学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP