Esempio n. 1
0
int main() {
    string s1, s2, s3;
    SuffixTree<'#'> tree;
    cin >> s1 >> s2 >> s3;
    tree.add_string(s1);
    tree.add_string(s2);
    tree.add_string(s3);
    tree.build_suffix_tree();
    tree.solve();
    /*
    {
        SuffixTree<'#'> tree;
        tree.add_string("baab");
        tree.build_suffix_tree();
        tree.print_preorder();
        tree.print_suffix_array();
    }

    {
        SuffixTree<'#'> tree;
        tree.add_string("banana");
        tree.add_string("xana");
        tree.build_suffix_tree();
        tree.find_max_common_substring();
    }
    */
    return 0;
}
Esempio n. 2
0
int suffixTreeTest()
{
	SuffixTree t;
	string test;
	//char c;
	//
	//for(int i = 0; i < 1000000; i++){
	//	c = random() % 10 + '0';
	//	test.append(1, c);
	//}
	//test.append(1, 'e');

	ifstream data;
	data.open("output");
	getline(data, test);
	

	cout << "str: " << test << endl;
//	t.addString("abcabxabcd");
//	t.addString("banana$");
//	t.addString("516571614e");
	t.addString(test);
//	t.addString("1231241e");
//	t.debugPrintEdges(NULL, 0);
//	printf("pasikartojimai: \n");
	t.getSeqs();
	

	

	return SUCC;
}
SuffixTree buildTempSuffixTree(const vector <int> &input) {
    if (input.size() == 0) {
        return SuffixTree();
    }
    if (input.size() == 1) {
        SuffixTree result;
        unsigned int newNodeIndex = result.newNode(result.root, 0, 1, 0);
        result[result.root].push_back(newNodeIndex);
        return result;
    }

    SuffixTree compressed = buildTempSuffixTree(compressInput(input));
    decompress(compressed, input);

    SuffixTree &even = compressed;
    checkTree("EVEN:\n", even, input);

    SuffixTree odd = buildOddSuffixTree(even, input);
    checkTree("ODD:\n", odd, input);

    SuffixTree almostResult = mergeTrees(even, odd, input);
    checkTree("ALMOST:\n", almostResult, input);

    cleanTreeDfs(almostResult.root, -1, almostResult);
    checkTree("RESL:\n", almostResult, input);

    return almostResult;
}
Esempio n. 4
0
File: main.cpp Progetto: CCJY/coliru
int main()
{
    SuffixTree<std::string> tree;
    tree.add_string("mississippi$");
    tree.add_string("missouri$");
    tree.dump_tree();
    return 0;
}
void correctMerge(unsigned int v, unsigned int parentsPlace, SuffixTree &merged,
    IndexedPair<MergeTreesStruct> &updatedTrees,
    const vector <unsigned int> &trueLength, const vector <int> &input,
    unsigned int copyTree
) {
    if (merged[v].isHiddenInfo()) {
        doSomething(updatedTrees, [&merged, &v] (MergeTreesStruct &tree) {
            tree.evaluate(merged, v);
        });
        if (copyTree == 2 && merged[v].depth != trueLength[v]) {
            unsigned int commonLength = trueLength[v]
                - merged[merged[v].parent].depth;
            MergeTreesStruct& donor = minimal(updatedTrees,
                [&input, &commonLength] (MergeTreesStruct &tree)
                    -> int {
                    auto const &node = tree.tree[tree.info];
                    return input[tree.suffix[tree.info]
                        + (node.parent == -1 ? 0 : tree.tree[node.parent].depth)
                        + commonLength];
                }
            );
            copyTree = donor.number;
            copyNodeExceptParentAndChildren(donor.tree[donor.info], merged[v]);
            unsigned int newNodeIndex = merged.splitEdge(merged[v].parent,
                parentsPlace, commonLength
            );
            unsigned int newCopy = merged.newNode(newNodeIndex);
            merged[newNodeIndex].push_back(newCopy);
            MergeTreesStruct &notDonor = *xorPointers(&donor, &updatedTrees[0],
                &updatedTrees[1]
            );
            copyNodeExceptParentAndChildren(notDonor.tree[notDonor.info],
                merged[newCopy]
            );
            merged[newCopy].indexOfParentEdge += commonLength;
            copySubTree(notDonor.tree, merged, notDonor.info, newCopy);
        } else {
            MergeTreesStruct &donor = minimal(updatedTrees,
                [&copyTree] (MergeTreesStruct &tree) -> pair<bool, bool> {
                    return make_pair(copyTree == 2 || copyTree != tree.number,
                        !(tree.tree[tree.info].leaf != -1)
                    );
                }
            );
            copyNodeExceptParentAndChildren(donor.tree[donor.info], merged[v]);
        }
    }
    if (copyTree != 2 && merged[v].leaf != -1
            && merged[v].leaf % 2 != copyTree) {
        merged[v].leaf = -1;
    }
    for (unsigned int i = 0; i < merged[v].size(); ++i) {
        unsigned int u = merged[v][i];
        correctMerge(u, i, merged, updatedTrees, trueLength, input, copyTree);
    }
}
Esempio n. 6
0
int main ()
{
	SuffixTree tree ("bibs");
	list<int> indexes = tree.search ("ib");

	for (list<int>::iterator it = indexes.begin(); it != indexes.end(); it++)
	{
		cout << *it << endl;
	}
}
Esempio n. 7
0
int main(int argc, char* argv[]) {
  SuffixTree tree;
  // Must be called with 1 and only 1 parameter.
  if (argc != 2) {
    std::cout << "usage: suffixtree inputstring" << std::endl;
    exit(1);
  }
  else {
    tree.construct(argv[1]);
    std::cout << tree.log_tree() << std::endl;
  }
}
Esempio n. 8
0
        void generateBitmapAux(pair <size_t,size_t> nodes,BitString *bitmap,uint &bitmap_pos,vector<pair<uint,uint> > &result) {
            pair <size_t,size_t> r;
            size_t pl_aux = nodes.first;
            size_t pr_aux = nodes.second;

            size_t next_vl,next_vr;
            size_t nsibling_l,nsibling_r;
            if (pr_aux - pl_aux != 1) {
                // cout << "entre!" << endl;
                if (pl_aux == pr_aux)  {
                    bitmap_pos++;
                    // cout << "(";
                    bitmap->setBit(bitmap_pos);    
                    // cout << ")";            
                    bitmap_pos++;
                    //return;
                }
                result.push_back(make_pair(pl_aux,pr_aux));
            }
            cst->FChild(pl_aux, pr_aux, &next_vl, &next_vr);
            cst->NSibling(pl_aux,pr_aux,&nsibling_l,&nsibling_r);
            r = make_pair(next_vl,next_vr);
            if (next_vl != (size_t)-1 && next_vr != (size_t)-1) {
                if (next_vl == next_vr)  {
                    generateBitmapAux(r,bitmap,bitmap_pos,result);
                } else { 
                    bitmap_pos++; // leave a 0
                    // cout << "(";
                    generateBitmapAux(r,bitmap,bitmap_pos,result);
                    // cout << ")";
                    bitmap->setBit(bitmap_pos);
                    bitmap_pos++;
                    pl_aux = next_vl;
                    pr_aux = next_vr;
                }
            }
            if (nsibling_l != (size_t)-1 && nsibling_r != (size_t)-1) {
                r = make_pair(nsibling_l,nsibling_r);
                if (nsibling_l == nsibling_r)  {
                    generateBitmapAux(r,bitmap,bitmap_pos,result);
                } else { 
                bitmap_pos++;
                // cout << "(";
                generateBitmapAux(r,bitmap,bitmap_pos,result);
                 // cout << ")";
                bitmap->setBit(bitmap_pos);
                bitmap_pos++;
                }

            }
        }
Esempio n. 9
0
int main()
{
    alphabet = "abcdefghijklmnopqrstuvwxyz1234567890-#";
    alphabetSize = alphabet.length();
    string s1,s2;
    //cout<<"Finding longest common substring using suffix trees\n";
    //cout<<"Enter 1st String: ";
    cin>>s1; 
   // cout<<"Enter 2nd String: ";
    cin>>s2;
    SuffixTree st;
    st.findLCS(s1, s2);         
    return 0;
}
Esempio n. 10
0
int main() {
    scanf("%s", buffer + 1);
    n = strlen(buffer + 1);

    SuffixTree *tree = new SuffixTree;
    for (int i = 1; buffer[i]; i++) {
        tree->append(buffer[i] - 'a' + 1);
    }  // for
    tree->append(SuffixTree::EOFCHAR);

    tree->sort(sa);
    for (int i = 1; i <= n; i++) {
        sa[i] = sa[i + 1];
    }  // for

    for (int i = 1; i <= n; i++) {
        rnk[sa[i]] = i;
    }  // for

    int j = 0;
    for (int i = 1; i <= n; i++) {
        if (rnk[i] == 1)
            continue;

        j--;

        if (j < 0)
            j = 0;

        while (buffer[sa[rnk[i]] + j] == buffer[sa[rnk[i] - 1] + j]) {
            j++;
        }  // while

        lcp[rnk[i]] = j;
    }  // for

    for (int i = 1; i <= n; i++) {
        printf("%d ", sa[i]);
    }  // for
    printf("\n");

    for (int i = 2; i <= n; i++) {
        printf("%d ", lcp[i]);
    }  // for
    printf("\n");

    return 0;
}  // function main
Esempio n. 11
0
        pair<BitString *,uint> generateBitmap(size_t length,vector<pair<uint,uint> > &nodes ) {
            uint bitmap_pos = 0;
            BitString *bitmap = new BitString(length);
            size_t a,b;
            cst->Root(&a,&b);
            bitmap_pos++;
            // cout << "(" ;
            generateBitmapAux(make_pair(a,b),bitmap,bitmap_pos,nodes);
            bitmap->setBit(bitmap_pos);
            bitmap_pos++;
            // cout << ")";
            // cout << "nodes.size() = " << nodes.size() << endl;
            BitString *new_bitmap = new BitString(bitmap->getData(),bitmap_pos);
            // for (int i = 0 ; i < nodes.size();i++) {
            //     cout << nodes[i].first << endl;
            // }
            // cout << "=========== tree ==========" << endl;
            // for (int i = 0 ;i < bitmap_pos;i++) {
            //     if (bitmap->getBit(i)) 
            //         cout << ")";
            //     else 
            //         cout << "(";
            // }
            // cout << endl;

            return make_pair(new_bitmap,bitmap_pos);
        }
Esempio n. 12
0
File: main.cpp Progetto: riteme/test
int main() {
    // freopen("subst1.in", "r", stdin);
    // freopen("subst1.out", "w", stdout);

    scanf("%s", buf);
    n = strlen(buf);

    SuffixTree *tree = new SuffixTree;
    for (size_t pos = 0; buf[pos]; pos++) {
        tree->append(buf[pos] - 'A');
    }  // for
    tree->append(EOFCHAR);
    printf("%lld\n", tree->solve() - n - 1);

    return 0;
}  // function main
Esempio n. 13
0
unsigned long long countSubstrings(SuffixTree &tree, unsigned int v) {
    tree.checkNode(tree[v]);
    unsigned long long sum = tree[v].lengthOfEdge(tree);
    for (auto const &u: tree[v]) {
        sum += countSubstrings(tree, u);
    }
    return sum;
}
Esempio n. 14
0
int main(){
    std::string testString("mississippi");
    std::string stringList[] = {"is", "sip", "hi", "sis"};
    SuffixTree *tree = new SuffixTree(testString);
    for (const auto &x : stringList){
        std::vector<int> list = tree->search(x);
        if (!list.empty()) {
            std::cout << x << ": ";
            for (const auto &y: list){
                std::cout << y << " ";
            }
            std::cout << std::endl;
        }else{
            std::cout << x << ": not found" << std::endl;
        }
    }
}
Esempio n. 15
0
unsigned int appendCopyNode(const SuffixTree &from, SuffixTree &to,
    unsigned int toStart, unsigned int u
) {
    unsigned int newStart = to.newNode(toStart);
    to[toStart].push_back(newStart);
    copyNodeExceptParentAndChildren(from[u], to[newStart]);
    copySubTree(from, to, u, newStart);
    return newStart;
}
Esempio n. 16
0
SuffixTree buildSuffixTreeFromSA(vector <unsigned int> &sa,
    vector <unsigned int> &lcp,
    unsigned int length
) {
    vector <int> tmp;
    SuffixTree result = buildTempSuffixTree(tmp);
    int newNodeIndex
        = result.newNode(result.root, sa[0], length - sa[0], sa[0]);
    result[result.root].push_back(newNodeIndex);
    unsigned int current = newNodeIndex;

    for (unsigned int i = 1; i < sa.size(); ++i) {
        while (result[current].parent != -1
            && result[result[current].parent].depth >= lcp[i - 1]
        ) {
            current = result[current].parent;
        }
        unsigned int parent;
        if (result[current].parent != -1 &&
            result[result[current].parent].depth == lcp[i - 1]
        ) {
            parent = result[current].parent;
        } else if (result[current].depth == lcp[i - 1]) {
            parent = current;
        } else {
            unsigned int currentParent = result[current].parent;
            parent = result.splitEdge(currentParent,
                result[currentParent].size() - 1,
                lcp[i - 1] - result[currentParent].depth
            );
            result[parent].leaf = (length - sa[i] == lcp[i - 1] ? sa[i] : -1);
        }
        if (lcp[i - 1] != length - sa[i]) {
            newNodeIndex = result.newNode(parent, sa[i] + lcp[i - 1],
                length - sa[i], sa[i]
            );
            result[parent].push_back(newNodeIndex);
            parent = newNodeIndex;
        }
        current = parent;
    }
    return result;
}
Esempio n. 17
0
int main()
{
	/*char needle[1000], haystack[1000];
	cin >> needle >> haystack;
	
	cout << NaiveSearch::search(needle, strlen(needle), haystack, strlen(haystack)) << endl;
	cout << BMHSearch::search(needle, strlen(needle), haystack, strlen(haystack)) << endl;*/

	char *word = "What";

	SuffixTree t = SuffixTree(word, 4);
	t.print();

	cin.ignore();
	cin.clear();
	cin.get();

	return 0;
}
Esempio n. 18
0
void checkDfs(const SuffixTree &tree, unsigned int v, const vector<int> &input
) {
    #ifdef _PRINT_DBG
        printf("%d -> %d [label=\"", tree[v].parent, v);
        for (int i = tree[v].indexOfParentEdge;
            i != tree[v].lastIndex(tree); ++i) {
            printf("%d", input[i]);
        }
        printf(", %d, %d\"]\n", tree[v].leaf, tree[v].depth);
    #endif

    tree.checkNode(tree[v]);
    for (auto const &u: tree[v]) {
        checkDfs(tree, u, input);
    }
}
Esempio n. 19
0
int find_match(SuffixTree& base, string prefix, int procs, int rank)
{
	struct timespec pp_time2_b = recorder_wtime();
	Preprocess pp_right(prefix + string("log.") + to_string(rank), procs, rank, true);
	pp_right.run();
	str_hmap_list right = pp_right.get_data();
	struct timespec pp_time2_e = recorder_wtime();
	pp_time += (pp_time2_e - pp_time2_b);

	struct timespec m_time_b = recorder_wtime();
	ofstream fout("st/suffix_merged", ios::app);
	fout << rank << "-info--------" << endl;
	
	int begin = 0, end = 0, limit = right.size();
	int pos;
	int sub_begin;

	int length, final_pos, begin_pos;

	typename str_hmap_list::iterator r_iter = right.begin();
	++r_iter;	// skip the empty element
	
	while (r_iter != right.end()) {
		typename str_hmap_list::iterator pre_iter = r_iter;
		r_iter = base.inc_search(r_iter, &final_pos);

		if (r_iter == pre_iter) {
			fout << "insert " << *r_iter << endl;
			++r_iter;
		}
		else {
			length = r_iter - pre_iter;
			begin_pos = final_pos - length + 1;
			fout << "keep " << begin_pos << " " << final_pos << endl;
		}
	}
	fout.close();
	struct timespec m_time_e = recorder_wtime();
	merge_time += (m_time_e - m_time_b);

	return 0;
}
Esempio n. 20
0
int cleanTreeDfs(unsigned int v, unsigned int parent, SuffixTree &tree) {
    int leaf = tree[v].leaf;
    for (unsigned int i = 0; i < tree[v].size(); ++i) {
        unsigned int u = tree[v][i];
        int newLeaf = cleanTreeDfs(u, i, tree);
        if (leaf == -1) {
            leaf = newLeaf;
        }
    }
    unsigned int freeCell = 0;
    for (unsigned int i = 0; i < tree[v].size(); ++i) {
        if (tree[v][i] != -1) {
            tree[v][freeCell] = tree[v][i];
            ++freeCell;
        }
    }
    tree[v].resize(freeCell);
    if (tree[v].size() <= 1 && tree[v].leaf == -1 && v != tree.root) {
        tree.deleteUselessNode(v, parent, leaf);
    }
    return leaf;
}
Esempio n. 21
0
bool checkStringOccurence(SuffixTree &tree, string word) {
  return tree.travelByString("smh") == tree.end();
}
Esempio n. 22
0
void mergeNodes(unsigned int first, unsigned int second, unsigned int to,
    SuffixTree &result, SuffixTree &tree1, SuffixTree &tree2,
    const vector <int> &input
) {
    IndexedPair<MergeNodesStruct> merging(MergeNodesStruct(tree1, first, input),
        MergeNodesStruct(tree2, second, input)
    );
    while (!merging[0].end() && !merging[1].end()) {
        doSomething(merging, [] (MergeNodesStruct &instance) {
            instance.evaluate();
        });
        if (merging[0].symbol == merging[1].symbol) {
            doSomething(merging, [] (MergeNodesStruct &instance) {
                instance.length
                    = instance.tree[instance.child].lengthOfEdge(instance.tree);
            });
            unsigned int minimalLength
                = min(merging[0].length, merging[1].length);
            if (merging[0].length != merging[1].length) {
                MergeNodesStruct &splitVictim = minimal(merging,
                    [] (const MergeNodesStruct &instance) -> int {
                        return -static_cast<int>(instance.length);
                    }
                );

                splitVictim.split(minimalLength);
            }
            unsigned int newNodeIndex = result.newNode(to);
            auto &newNode = result[newNodeIndex];
            result[to].push_back(newNodeIndex);

            if (minimalLength == 1) {
                MergeNodesStruct &recipient = minimal(merging,
                    [] (const MergeNodesStruct &instance)
                        -> pair<unsigned int, int> {
                        return make_pair(instance.length,
                            -instance.tree[instance.child].leaf);
                    }
                );
                copyNodeExceptParentAndChildren(recipient.tree[recipient.child],
                    newNode
                );
            } else {
                newNode.setHiddenInfo(merging[0].child, merging[1].child);
                newNode.depth = minimalLength + result[to].depth;
            }
            mergeNodes(merging[0].child, merging[1].child, newNodeIndex,
                result, tree1, tree2, input);
            doSomething(merging, [] (MergeNodesStruct &instance) {
                ++instance.childIndex;
            });
        } else {
            MergeNodesStruct &recipient = minimal(merging,
                [] (const MergeNodesStruct &instance) -> int {
                    return instance.symbol;
                }
            );
            ++recipient.childIndex;
            appendCopyNode(recipient.tree, result, to, recipient.child);
        }
    }
    MergeNodesStruct &recipient = minimal(merging,
        [] (const MergeNodesStruct & instance) -> bool {
            return instance.end();
        }
    );
    for (; !recipient.end(); ++recipient.childIndex) {
        recipient.evaluate();
        appendCopyNode(recipient.tree, result, to, recipient.child);
    }
}
Esempio n. 23
0
File: main.cpp Progetto: CCJY/coliru
int main(int, char**){
  SuffixTree<std::string> blob;
  std::cout << "blob" << std::endl;
  blob.test("cacao");
  return 0;
}
Esempio n. 24
0
int decompressDfs(unsigned int v, unsigned int inParentIndex, SuffixTree &tree,
    const vector <int> &input, unsigned int depth
) {
    int leaf = -1;
    unsigned int oldDepth = tree[v].depth;
    tree[v].depth = depth;

    for (unsigned int i = 0; i < tree[v].size(); ++i) {
        unsigned int u = tree[v][i];
        int newLeaf = decompressDfs(u, i, tree, input,
            depth + tree[u].lengthOfEdge(tree, oldDepth) * 2
            - (tree[u].lastIndex(tree, oldDepth) == input.size() / 2 + 1)
        );
        if (leaf == -1) {
            leaf = newLeaf;
        }
    }
    tree[v].indexOfParentEdge *= 2;
    if (tree[v].leaf != -1) {
        tree[v].leaf = min(tree[v].leaf * 2, static_cast<int>(input.size()));
    }

    vector <unsigned int> myNewChildren;
    if (tree[v].size()) {
        vector <unsigned int> similarKids;
        unsigned int firstKidIndex = 0;
        similarKids.push_back(tree[v][0]);
        for (unsigned int i = 1; i <= tree[v].size(); ++i) {
            if (i != tree[v].size()) {
                unsigned int current = tree[v][i];
                unsigned int previous = tree[v][i - 1];
                while (i < tree[v].size()
                    && input[tree[current].indexOfParentEdge]
                        == input[tree[previous].indexOfParentEdge]
                ) {
                    similarKids.push_back(current);
                    ++i;
                    previous = current;
                    if (i != tree[v].size()) {
                        current = tree[v][i];
                    }
                }
            }
            if (similarKids.size() != 1) {
                unsigned int newNodeIndex = tree.splitEdge(v, firstKidIndex, 1);
                auto &newNode = tree[newNodeIndex];
                for (unsigned int j = 1; j < similarKids.size(); ++j) {
                    auto &currentChild = tree[similarKids[j]];
                    currentChild.parent = newNodeIndex;
                    ++currentChild.indexOfParentEdge;
                    newNode.push_back(similarKids[j]);
                }
                if (newNode.size()
                    && tree[newNode[0]].lengthOfEdge(tree) == 0
                ) {
                    newNode.leaf = tree[newNode[0]].leaf;
                    tree.deleteUselessNode(newNode[0], 0, newNode.leaf);
                    newNode.deleteFirstChild();
                }
                myNewChildren.push_back(newNodeIndex);

                tree.checkNode(tree[newNodeIndex]);
            } else {
                myNewChildren.push_back(similarKids.back());
            }
            if (i != tree[v].size()) {
                similarKids.clear();
                similarKids.push_back(tree[v][i]);
                firstKidIndex = i;
            }
        }
    }
    tree[v].renewChildren(myNewChildren);
    if (tree[v].size() == 1 && v != tree.root && tree[v].leaf == -1) {
        tree.deleteUselessNode(v, inParentIndex, leaf);
    }
    if (tree[v].leaf != -1) {
        leaf = tree[v].leaf;
    }
    return leaf;
}