-
Notifications
You must be signed in to change notification settings - Fork 2
/
bwtinverse.cpp
77 lines (66 loc) · 2 KB
/
bwtinverse.cpp
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
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <map>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
string InverseBWT(const string& bwt)
{
std::string text;
text.resize(bwt.size());
// write your code here
text[0] = '$'; // This will always be first characters
size_t pos = 1;
vector<int> front_count;
front_count.push_back(1); // count of `$`
size_t t = std::count(bwt.begin(), bwt.end(), 'A');
std::fill(text.begin() + pos, text.begin() + pos + t, 'A');
pos += t;
t = std::count(bwt.begin(), bwt.end(), 'C');
std::fill(text.begin() + pos, text.begin() + pos + t, 'C');
pos += t;
t = std::count(bwt.begin(), bwt.end(), 'G');
std::fill(text.begin() + pos, text.begin() + pos + t, 'G');
pos += t;
t = std::count(bwt.begin(), bwt.end(), 'T');
std::fill(text.begin() + pos, text.begin() + pos + t, 'T');
pos += t;
// For first column we store the index of each string, BWT= TTCCTAACG$A , first column = $AAACCCGTTT
// first = $= [0] , A=[0,1,2] , C=[3,4,5] G= [6], T=[7,8,9]
std::map<char, vector<int> > first;
for (int i = 0; i < bwt.size(); i++)
first[text[i]].push_back(i);
//Store count of each character in last column like TTCCTAACG$A is stored as [0,1,0,1,2,0,1,2,0,0,2]
std::map<char, int> m;
vector<int> last;
m['A'] = 0;
m['C'] = 0;
m['G'] = 0;
m['T'] = 0;
m['$'] = 0;
for (int i = 0; i < bwt.size(); i++)
last.push_back(m[bwt[i]]++);
int index = 0;
std::string res;
res.resize(bwt.size());
int store = bwt.size();
res[--store] = '$';
while (bwt[index] != '$')
{
res[--store] = (bwt[index]);
int count = last[index];
index = first[bwt[index]][count];
}
return res;
}
int main()
{
string bwt;
cin >> bwt;
cout << InverseBWT(bwt) << endl;
return 0;
}