Advertisement
ProfBerrier

Hack 'n' Slash BWT Reverser

Mar 25th, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. string decodeBWT(const string &input);
  11.  
  12. int main()
  13. {
  14.     string transformed =
  15.         "SGGCSNTSSSFEESRWOTOSOYGL"
  16.         "LESMDETNOEDRTSOOOTSTGERO"
  17.         "AFFESAEEENTETEONEYRTGDTY"
  18.         "EDNEESYEYTGEDNRLRIIETEST"
  19.         "RSYEGUERRE.OMMM.MC...EGR"
  20.         "RW.RFHWWE.HHMRMCHHHFWW.."
  21.         ".A.M...IIE.SSNEEEEIARNN."
  22.         "..HHLWHKHNMLHRNKSVRLRHWV"
  23.         "DMLBDVRTHFBTBEERHERVDHLD"
  24.         "HVDTGSYMN.OOO...N...SNSR"
  25.         ".NNNNONNEAAAAOII.TW...TT"
  26.         "TTTTWWTTTTTTP..GG.FFLW.."
  27.         "EHVHNTTT.H.DD..RW.IALLUA"
  28.         "ZZB.E.OIPB..BEAANRI...RR"
  29.         "UO.OE..OEEOOUAOOOO.OIIOI"
  30.         "IIIAOA.OAAAATTSDGSTTTL.."
  31.         ".LNFSSII.PW..CL.IFEVFFFP"
  32.         "LN.YH.MCCRAU....EEELEAEO"
  33.         "TTTGUOUUEE..EOOOORUKAAEY"
  34.         "ESIILAINWIIUSNN.L..EOOAU"
  35.         "..SOUIAASASUEIUNSI..$..."
  36.         ".O.YE.IAAA.....X...OFTF."
  37.         "GGSBAJOOBPPAAOOOAAE....L"
  38.         "........OEMLMBAH..ANZZUU";
  39.  
  40.     string result = decodeBWT(transformed);
  41.  
  42.     cout << result << endl;
  43.  
  44.     system("pause");
  45.     return 0;
  46. }
  47.  
  48. string decodeBWT(const string &input)
  49. {
  50.     // Sanity check
  51.     if(input == "") return "";
  52.  
  53.     // Create empty table
  54.     vector<string> table(input.length(), "");
  55.  
  56.     // For every character in the string
  57.     for(size_t i=0; i<input.length(); i++)
  58.     {
  59.         // Insert the character into each row at the front
  60.         for(size_t j=0; j<table.size(); j++)
  61.         {
  62.             table[j] = input.substr(j, 1) + table[j];
  63.         }
  64.  
  65.         // Sort the rows (lexicagraphic sort by strings)
  66.         sort(table.begin(), table.end());
  67.     }
  68.  
  69.     // Find the row that starts with '$' and return that row (with the '$' removed)
  70.     for(size_t i=0; i<table.size(); i++)
  71.     {
  72.         if(table[i].at(i) == '$') return table[i].substr(1);
  73.     }
  74.  
  75.     // In case it didn't find one
  76.     return "?";
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement