Exemplo n.º 1
0
void ILCPConstruct(const SuffixArray& sa,
                   std::vector<SuffixArray::Index>* ilcp) {
  typedef SuffixArray::Index Index;
  std::vector<Index>& text_lcp = *ilcp;
  text_lcp.resize(sa.size());
  Index start = 0;
  int num_docs = 0;
  const char* text = sa.text();
  for (Index i = 0; i <= (Index)sa.size(); ++i) {
    if (i == (Index)sa.size() || (unsigned char)text[i] <= 1) {
      const char* doc = text + start;
      Index doc_len = i - start;
      SuffixArray doc_sa(doc, doc_len);
      for (Index j = 0; j < doc_len; ++j) {
        Index p = doc_sa.sa(j);
        Index lcp = doc_sa.lcp(j);
        text_lcp[start + p] = lcp;
      }
      num_docs++;
      start = i;
    }
  }
  std::vector<bool> visited(sa.size());
  // permutate text_lcp[i] = text_lcp[sa[i]] implace
  for (Index i = 0; i < (Index)sa.size(); ++i) {
    if (!visited[i]) {
      int j = i;
      while (true) {
        visited[j] = 1;
        Index to = sa.sa(j);
        if (visited[to]) break;
        std::swap(text_lcp[j], text_lcp[to]);
        j = to;
      }
    }
    // ilcp[i] = text_lcp[sa.sa(i)];
  }
}
std::vector<int> longest_common_prefix(const T &s, const SuffixArray &sa){
	const int n = sa.size();
	std::vector<int> vs(n), isa(n), lcp(n - 1);
	for(int i = 0; i + 1 < n; ++i){ vs[i] = s[i]; }
	for(int i = 0; i < n; ++i){ isa[sa[i]] = i; }
	int h = 0;
	for(int i = 0; i < n; ++i){
		const int j = isa[i];
		if(j > 0){
			const int k = j - 1;
			while(vs[sa[j] + h] == vs[sa[k] + h]){ ++h; }
			lcp[k] = h;
			if(h > 0){ --h; }
		}
	}
	return lcp;
}