-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
66 lines (61 loc) · 1.65 KB
/
code
File metadata and controls
66 lines (61 loc) · 1.65 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstring>
using namespace std;
///编码,生成last数组
int getLastArray(char *lastArray,const string &str){ ///子串排序
int len=str.size();
string array[len];
for(int i=0;i<len;i++){
array[i] = str.substr(i);
}
sort(array,array+len);
for(int i=0;i<len;i++){
lastArray[i] = str.at((2*len-array[i].size()-1)%len);
}
return 0;
}
int getCountPreSum(int *preSum,const string &str){
memset(preSum,0,27*sizeof(int));
for(int i=0;i<str.size();i++){
if(str.at(i) == '#')
preSum[0]++;
else
preSum[str.at(i)-'a'+1]++;
}
for(int i=1;i<27;i++)
preSum[i] += preSum[i-1];
return 0;
}
///解码,使用last数组,恢复原来的文本块
int regainTextFromLastArray(char *lastArray,char *reGainStr,int *preSum){
int len=strlen(lastArray);
int pos=0;
char c;
for(int i=len-1;i>=0;){
reGainStr[i] = lastArray[pos];
c = lastArray[pos];
pos = preSum[c-'a']+count(lastArray,lastArray+pos,c);
i--;
}
return 0;
}
int main (){
string str("sdfsfdfdsdfgdfgfgfggfgdgfgd#");
int preSum[27];
int len=str.size();
char *lastArray = new char[len+1];
char *reGainStr = new char[len+1];
lastArray[len]='\0';
reGainStr[len]='\0';
getCountPreSum(preSum,str);
getLastArray(lastArray,str);
regainTextFromLastArray(lastArray,reGainStr,preSum);
cout<<" str: "<<str<<endl;
cout<<"lastArray : "<<lastArray<<endl;
cout<<"reGainStr : "<<reGainStr<<endl;
delete lastArray;
delete reGainStr;
return 0;
}