天天看點

最短摘要生成

題目:抽象點說,就是在一個字元串中,找一些目标字元串,找到包含所有目标字元串的最小字元串。題目雖然叫做最短摘要生成,但和實際的搜尋snippet的計算還是有比較大的差距的。

解法:文獻[1]給出了一種比較好的解法,政策還是使用雙指針,雙指針對于很多算法設計很有價值,算法的思想是采用兩個指針,開始兩個指針都指向緩沖區的頭部,尾指針向後掃描,直到頭指針和尾指針中間包含了全部的關鍵字,那麼頭指針向後移動,直到包含全部關鍵字這個條件失敗,這時截取字串并和已取得的最小字串比較,如果小則替換。頭指針、尾指針都向後一個位置(這點很重要,開始就忘記了移動頭指針,導緻程式出錯),繼續掃描。另外,由于一個關鍵字可能重複多次,是以在判斷是否包含全部關鍵字時要采用計數機制,才能保證檢視的準确。這樣隻要頭指針和尾指針掃描兩次字元串就可以完成生成算法。

具體代碼如下:

#include <stdio.h>
#include <string>
#include <map>
class KeyWordChecker {
 public:
  KeyWordChecker() : current_state_(false) {}
  void AddKeyWord(const std::string& word) {
    if (keywords_.find(word) == keywords_.end()) {      
      keywords_[word] = 0;
    }
  }
  bool IsContainAllKeyWord(const std::string& word, bool add) {
    if (keywords_.find(word) == keywords_.end()) {
      return current_state_;
    }
    if (add) {
      keywords_[word]++;
    } else {
      keywords_[word]--;
    }
    std::map<std::string, int>::iterator begin = keywords_.begin();
    int counter = 0;
    while (begin != keywords_.end()) {
      if (begin->second > 0) {
        counter++;
      } else {
        break;
      }
      begin++;
    }
    if (counter == keywords_.size()) {
      current_state_ = true;
      return true;
    } else {
      current_state_ = false;
      return false;
    }
  }
 private:
  std::map<std::string, int> keywords_;
  bool current_state_;
};

std::string GenerateSnippet(const std::string& content, KeyWordChecker* keyword_checker) {
  int begin = 0;
  int end = 0;
  std::string snippet;
  int min_length = content.size();
  while (end < content.size()) {
    if (!keyword_checker->IsContainAllKeyWord(std::string(1, content[end]), true)) {
       end++;
       continue;
    }
    while (begin <= end && keyword_checker->IsContainAllKeyWord(std::string(1, content[begin]), false)) {
      begin++;
    }
    if (end - begin + 1 < min_length) {
      snippet = content.substr(begin, end - begin + 1);
      min_length = end - begin + 1;
    }
    end++;
    begin++;
  }  
  return snippet;
}
int main(int argc, char** argv) {
  std::string content = "abbbbbcaaadebmmmmdcfg";
  KeyWordChecker keyword_checker;
  keyword_checker.AddKeyWord("b");
  keyword_checker.AddKeyWord("d");
  std::string snippet = GenerateSnippet(content, &keyword_checker);
  printf("snippet:%s\n", snippet.c_str());
}
           

參考文獻:

程式設計之美3.5

繼續閱讀