-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_suffix.cpp
71 lines (69 loc) · 1.7 KB
/
test_suffix.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
#include <iostream>
#include <fstream>
#include "suffix.hpp"
using namespace std;
int convert_to_wstring(const char* str, wstring &out_wstr) {
if (!str)
return 1;
wchar_t* wcs = NULL;
int size = 0;
string loc = setlocale(LC_ALL,NULL);
setlocale(LC_ALL, "zh_CN.utf8");
size = mbstowcs(NULL,str,0);
wcs = new wchar_t[size+1];
size = mbstowcs(wcs, str, size+1);
wcs[size] = 0;
out_wstr = wcs;
delete[] wcs;
setlocale(LC_ALL, loc.c_str());
return 0;
}
int convert_to_string(const wchar_t * wstr, string &out_str) {
if (!wstr)
return 1;
char* str = NULL;
size_t size = 0;
string loc = setlocale(LC_ALL,NULL);
setlocale(LC_ALL, "zh_CN.utf8");
size = wcstombs( NULL, wstr, 0);
str = new char[size + 1];
wcstombs(str, wstr, size);
str[size] = '\0';
out_str = str;
delete[] str;
setlocale(LC_ALL, loc.c_str());
return 0;
}
int main(int argc, char* argv[]) {
//wchar_t str[] = L"aabbaa\0ababab";
wchar_t str[] = L"to_be`or`not_to_be`";
//wchar_t str[] = L"嘻嘻哈哈嘻嘻\0嘻哈嘻哈嘻哈";
//wstring wstr(str);
ifstream infile(argv[1]);
if (!infile) {
cout << "open file error:" << argv[1] << endl;
return 0;
}
string line;
string s;
while (getline(infile, line)) {
s += line;
}
infile.close();
wstring input;
convert_to_wstring(s.c_str(),input);
size_t n = input.size();
cout << "n=" << n << endl;
SuffixInfo *sa = new SuffixInfo[n];
size_t *lcp = new size_t[n+1];
lcp[n] = 0;
build_suffix_array(input.c_str(), n, lcp, sa);
string output;
for (int i=0; i<n; i++)
{
convert_to_string(input.substr(sa[i].i, 5).c_str(), output);
//cout<<from[sa[i].i]<<' '<< output <<' '<<lcp[i]<<endl;
cout<< i << '\t' << output <<'\t'<<lcp[i]<<endl;
}
return 0;
}