-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathBlocksort.cpp
More file actions
48 lines (39 loc) · 1.05 KB
/
Copy pathBlocksort.cpp
File metadata and controls
48 lines (39 loc) · 1.05 KB
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
#include <vector>
#include <iostream>
#include <algorithm>
class Blocksort
{
public:
static std::string encode(const std::string &input,int &top);
static std::string decode(const std::string &input,int top);
};
std::string Blocksort::encode(const std::string &input, int &top)
{
std::string buff,result;
buff = input + input;
std::vector<std::string> vectemp;
for(int i=0; i < buff.size()/2;++i){
std::string temp=buff.substr(i,input.size());
vectemp.push_back(temp);
}
std::stable_sort(vectemp.begin(),vectemp.end());
for(int i=0; i < vectemp.size();++i){
if( input == vectemp[i] ) top = i;
result += *(vectemp[i].end()-1);
}
return result;
}
std::string Blocksort::decode(const std::string &input,int top)
{
std::string result,inputs=input;
std::vector<int> idx(input.size());
for (int i = 0; i != idx.size(); ++i) idx[i] = i;
std::stable_sort(idx.begin(),idx.end(),
[inputs](int i1,int i2) {return inputs[i1] < inputs[i2];});
int p = top;
for(int i=0;i<input.size();++i) {
p = idx[p];
result += inputs[p];
}
return result;
}