summaryrefslogtreecommitdiffstats
path: root/04-algorithms_on_strings/01-suffix_trees/04-suffix_tree/suffix_tree.cpp
blob: 96fdf838d61050c85839b517de82564d38aafcf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <algorithm>
#include <cassert>
#include <map>
#include <string>
#include <vector>
#include <stack>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
using std::stack;

int const Letters =    5;
int const NA      =   -1;

struct Node
{
    int from;
    int len;
    int childs [Letters];

	Node () : from(0), len(0)
	{
        std::fill(childs, childs + Letters, NA);
	}

    Node(int f, int l) : from(f), len(l)
    {
        std::fill(childs, childs + Letters, NA);
    }

    void print(const string& text)
    {
        std::cout << from << ";" << len << " ";
        for(int i = from; i < (from + len); i++)
            std::cout << text[i];
        std::cout << " - A:" << childs[0] << " C:" << childs[1]
            << " G:" << childs[2] << " T:" << childs[3] << " $:" << childs[4]<< std::endl;
    }
};

struct SuffixTree
{
    const string& text;
    vector<Node> nodes;

    SuffixTree(const string& t) : text(t)
    {
        nodes.push_back(Node());
        for (int i = 0; i < t.size(); i++)
            add(i, text.size() - i, 0);
    }

    void add(int from, int len, int into)
    {
        int i = toInt(text[from]);
        int n = nodes[into].childs[i];
        if (n == NA) {
            nodes.push_back(Node(from, len));
            nodes[into].childs[i] = nodes.size() - 1;
        } else {
            insert(from, len, n, into);
        }
    }

    void insert(int from, int len, int n, int parent)
    {
        Node &node = nodes[n];

        int i = from;
        int j = node.from;
        int to = from + std::min(len, node.len);

        for( ; (text[i] == text[j] && i < to); i++, j++) { }

        if (i < to) {
            int matchLen = j - node.from;
            int s = nodes.size();
            // new match node
            nodes.push_back(Node(node.from, matchLen));
            // shrink n to mismatch part
            nodes[n].from = j;
            nodes[n].len = nodes[n].len - matchLen;
            // parent[from] -> match[j] -> n
            nodes[parent].childs[toInt(text[from])] = s;
            nodes[s].childs[toInt(text[j])] = n;
            // new mismatch node
            nodes.push_back(Node(i, len - matchLen));
            // match[i] -> mismatch
            nodes[s].childs[toInt(text[i])] = s + 1;
        } else if (i == to) {
            add(i, len - i + from, n);
        } else if (i == from) {
            std::cout << "missmatch at 0 !!!!!!!!!!!!!!!!!!" << std::endl;
            nodes[n].print(text);
            std::cout << &text[from]<< std::endl;
            std::cout << &text[i]<< std::endl;
        }
    }

    void travers(vector<string> &r)
    {
        stack<int> s;
        s.push(0);
        while (!s.empty()) {
            int n = s.top();
            s.pop();
            Node &node = nodes[n];

            if (n != 0)
                r.push_back(text.substr(node.from, node.len));

            for(int i = 0; i < Letters; i++) {
                int nn = node.childs[i];
                if (nn != NA)
                    s.push(nn);
            }
        }
    }

    int toInt(char c)
    {
        switch (c)
        {
            case 'A': return 0; break;
            case 'C': return 1; break;
            case 'G': return 2; break;
            case 'T': return 3; break;
            case '$': return 4; break;
            default: assert (false); return -1;
        }
    }


};

vector<string> ComputeSuffixTreeEdges(const string& text)
{
    vector<string> result;

    SuffixTree t(text);
    t.travers(result);

    return result;
}

int main() {
  string text;
  cin >> text;
  vector<string> edges = ComputeSuffixTreeEdges(text);
  for (int i = 0; i < edges.size(); ++i) {
    cout << edges[i] << endl;
  }
  return 0;
}