[关闭]
@pastqing 2014-08-16T02:26:30.000000Z 字数 3132 阅读 1364

0809

c++


文本纠错项目

  1. size_t editDistance(const char *str1, const char *str2)
  2. {
  3. int memo[100][100];
  4. int len1 = strlen(str1);
  5. int len2 = strlen(str2);
  6. for( int i = 0; i < len1 +1 ; ++i)//str1为空时
  7. memo[i][0] = i ;
  8. for(int i = 0; i< len2 +1 ; ++i)
  9. memo[0][i] = i;
  10. for(int i = 1; i <= len1; ++i )
  11. {
  12. for( int j = 1; j <= len2; ++j )
  13. {
  14. if( str1[i-1] == str2[j-1] )
  15. {
  16. memo[i][j] = memo[i-1][j-1];
  17. }
  18. else
  19. {
  20. memo[i][j] = myMin(memo[i-1][j], memo[i][j-1], memo[i-1][j-1])+1;
  21. }
  22. }
  23. }
  24. return memo[len1][len2];
  25. }

使用索引,找出查询词每个单词取并集,剔除高频出现的字母

  1. std::map<std::string, int > index_[26]; //建立26个首字母索引,放入单词和词频对
  2. void TextQuery::make_index()
  3. {
  4. //Fre_line 是一个词频和 set行号结构体
  5. //遍历将每个单词和词频做成pair放入索引
  6. for(map<string, Fre_line>::iterator it = word_.begin(); it != word_.end(); ++it)
  7. {
  8. pair<string, int> temp = make_pair(it->first, (it->second).cnt); //单词,词频
  9. insert_index(temp);
  10. }
  11. }
  12. void TextQuery::insert_index(std::pair<string, int> word)
  13. {
  14. for(string::iterator it = word.first.begin(); it != word.first.end(); ++it)
  15. {
  16. index_[*it - 97].insert(word);//小写字母-97就是map对象字母表的下表
  17. }
  18. }
  19. //输入单词后: 1.排除高频字母
  20. //2.对所在字母的索引进行查找,符合要求放入临时set中
  21. //3.若set不空,将set中的结构体放入优先队列
  22. for(string::iterator ix = word.begin(); ix != word.end(); ++ix)
  23. {
  24. if(*ix == 'i' || *ix == 'e' || *ix == 'a')
  25. continue;
  26. for(map<string, int>::iterator iter = index_[*ix - 97].begin(); iter != index_[*ix - 97].end(); ++iter )
  27. {
  28. temp = edit_distance(word, iter->first);
  29. //cout << temp << endl;
  30. if(temp >= 3)
  31. continue;
  32. if(temp < 3)
  33. {
  34. candidate cand;
  35. // bug memset(&cand, 0, sizeof(cand));
  36. cand.word = iter->first;
  37. cand.distance = temp;
  38. cand.frequence = iter->second;
  39. //cout << cand.word << " " << cand.frequence << " " << cand.distance << endl;
  40. //
  41. //首先放入set集合,防止重复,再写入cand_
  42. set_words.insert(cand);
  43. }
  44. }
  45. }

使用cache提高程序效率

LRU的典型实现是hash map + doubly linked list,
双向链表用于存储数据结点,并且它是按照结点最近被使用的时间来存储的。 如果一个结点被访问了, 我们有理由相信它在接下来的一段时间被访问的概率要大于其它结点。于是, 我们把它放到双向链表的头部。当我们往双向链表里插入一个结点, 我们也有可能很快就会使用到它,同样把它插入到头部。 我们使用这种方式不断地调整着双向链表,链表尾部的结点自然也就是最近一段时间, 最久没有使用到的结点。那么,当我们的Cache满了, 需要替换掉的就是双向链表中最后的那个结点(不是尾结点,头尾结点不存储实际内容)。

  1. //cache类的简单实现
  2. template<typename K, typename T>
  3. class cacheThread
  4. {
  5. public:
  6. explicit cacheThread(size_t size);
  7. ~cacheThread();
  8. T getValue(K key);
  9. void putValue(K key, T data);
  10. Node<K, T>* getHead();
  11. Node<K, T>* getTail();
  12. private:
  13. size_t cacheSize_;
  14. std::unordered_map<K, Node<K, T>* >cacheMap_;//map中存的是key与>对应value的结点地址
  15. std::vector<Node<K, T>* >saveEntries_;
  16. Node<K, T> *head_;
  17. Node<K, T> *tail_;
  18. Node<K, T> *entries_;//链表结点类型
  19. void detach(Node<K, T>*);//分离结点
  20. void attach(Node<K, T>*);//头插法
  21. };
  22. /*cache中,我们就可以通过Put接口将数据插入双向链表中。 如果此时的Cache还
  23. 没满,那么我们将新结点插入到链表头部, 同时用哈希表保存结点的键值及结点>地址对。如果Cache已经满了, 我们就将链表中的最后一个结点(注意不是尾结点)的内容替换为新内容, 然后移动到头部,更新哈希表。*/
  24. template<typename K, typename T>
  25. inline void cacheThread<K, T>::putValue(K key, T data)
  26. {
  27. Node<K, T> *node = cacheMap_[key];
  28. if(node)//如果这个结点已经存在
  29. {
  30. detach(node);
  31. node->data_ = data;
  32. attach(node);
  33. }else
  34. {
  35. if(saveEntries_.empty())//所有结点都已经弹出,即cache满了
  36. {
  37. node = tail_->pre_;
  38. detach(node);
  39. cacheMap_.erase(node->key_);
  40. }else
  41. {
  42. node = saveEntries_.back();
  43. saveEntries_.pop_back();
  44. }
  45. node->key_ = key;
  46. node->data_ = data;
  47. cacheMap_[key] = node;
  48. attach(node);
  49. }
  50. }
  51. /*当我们通过键值来访问类型为T的数据时, 调用getValue函数,如果键值为key>的数据已经存在cache中,则返回该数据,并且将该数据的结点移动到头部,如不>在则调用putValue插入链表 */
  52. template<typename K, typename T>
  53. inline T cacheThread<K, T>::getValue(K key)
  54. {
  55. Node<K, T>* node = cacheMap_[key];
  56. if( node )
  57. {
  58. detach(node);
  59. attach(node);
  60. return node->data_;
  61. }else
  62. return T();
  63. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注