Created
February 18, 2019 07:38
-
-
Save sandeshbhusal/153e3de78684c524efeecc14e310e5fd to your computer and use it in GitHub Desktop.
Reverse Burrows Wheeler Transformation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <cassert> | |
bool sortFunction(std::string s1, std::string s2){ | |
if(s1[0] < s2[0]) return true; | |
if(s2[0] < s1[0]) return false; | |
int i = 0; | |
int min = s1.size() < s2.size() ? s1.size() : s2.size(); | |
bool isMinS1 = (min == s1.size()); | |
bool isMinS2 = (min == s2.size()); | |
for(i = 1; i<min; i++){ | |
if(s1[i] < s2[i]) return true; | |
if(s2[i] < s1[i]) return false; | |
} | |
if(isMinS1) return true; | |
else if(isMinS2) return false; | |
else return false; | |
} | |
void rotateStringsToRight(std::vector<std::string> &stringArray){ | |
for(int i=0; i<stringArray.size(); i++){ | |
std::string &x = stringArray[i]; | |
x = x + x[x.size() - 1]; | |
for(int j= x.size() -1; j>=0; j--){ | |
x[j] = x[j-1]; | |
} | |
x[0] = '^'; | |
} | |
} | |
void prependToBegin(std::vector<std::string> &stringArray, const std::string &toPrepend){ | |
assert(stringArray.size() == toPrepend.size() && "Arrays need to have the same sizes."); | |
for(int i=0; i<stringArray.size(); i++){ | |
stringArray[i][0] = toPrepend[i]; | |
} | |
} | |
int main(void){ | |
std::string input; | |
std::cin >> input; | |
int index; | |
std::cin >> index; | |
std::vector<std::string> combinations; | |
for(char ch: input) | |
combinations.push_back(std::string(1, ch)); | |
for(int x =1; x<input.size(); x++){ | |
std::cout << "PASS # " << x << std::endl; | |
std::cout << "-------------------" << std::endl; | |
std::cout << "SORT: " << std::endl; | |
std::sort(combinations.begin(), combinations.end(), sortFunction); | |
for(std::string x: combinations){ | |
std::cout << x << std::endl; | |
} | |
std::cout << "ROTATE: " << std::endl; | |
rotateStringsToRight(combinations); | |
std::sort(combinations.begin(), combinations.end(), sortFunction); | |
for(std::string x: combinations){ | |
std::cout << x << std::endl; | |
} | |
std::cout << "PREPEND: " << std::endl; | |
prependToBegin(combinations, input); | |
for(std::string x: combinations){ | |
std::cout << x << std::endl; | |
} | |
std::cout << "-------------------" << std::endl; | |
} | |
// Final Sorting. | |
std::sort(combinations.begin(), combinations.end(), sortFunction); | |
std::cout << "Original String: " << combinations[index] << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment