From 7784f65e218ae49997770da9590227d4616679b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Tue, 30 Dec 2014 11:11:47 +0800 Subject: [PATCH] add suffix_array --- TinySTL/SuffixArray.h | 55 +++++++++++++++++++++++++++++++++ TinySTL/TinySTL.vcxproj | 10 ++++++ TinySTL/TinySTL.vcxproj.filters | 30 ++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 TinySTL/SuffixArray.h diff --git a/TinySTL/SuffixArray.h b/TinySTL/SuffixArray.h new file mode 100644 index 0000000..50d1388 --- /dev/null +++ b/TinySTL/SuffixArray.h @@ -0,0 +1,55 @@ +#ifndef _SUFFIX_ARRAY_H_ +#define _SUFFIX_ARRAY_H_ + +#include +#include + +namespace TinySTL{ + + class suffix_array{ + private: + //typedef std::unique_ptr> vecPtr; + private: + std::vector _array; + public: + template + //arr - Ô´Êý×é + //len - Ô´Êý×鳤¶È + //max_len - max_len´ú±í×Ö·û´®ÖÐ×Ö·ûµÄȡֵ·¶Î§£¬ÊÇ»ùÊýÅÅÐòµÄÒ»¸ö²ÎÊý£¬ + // Èç¹ûÔ­ÐòÁж¼ÊÇ×Öĸ¿ÉÒÔÖ±½ÓÈ¡128£¬Èç¹ûÔ­ÐòÁб¾Éí¶¼ÊÇÕûÊýµÄ»°£¬Ôòm¿ÉÒÔÈ¡±È×î´óµÄÕûÊý´ó1µÄÖµ¡£ + suffix_array(InputIterator arr, int len, int max_len = 128){ + //²ÉÓÃÁËÂÞËëå¹ÂÛÎÄÖÐʵÏֵı¶ÔöËã·¨ + _array.resize(len); + int wa[1024], wb[1024], wv[1024], ws[1024]; + + int i, j, p, *x = wa, *y = wb, *t; + for (i = 0; i < max_len; i++) ws[i] = 0; + for (i = 0; i < len; i++) ws[x[i] = arr[i]]++; + for (i = 1; i < max_len; i++) ws[i] += ws[i - 1]; + for (i = len - 1; i >= 0; i--) _array[--ws[x[i]]] = i; + for (j = 1, p = 1; p < len; j *= 2, max_len = p) + { + for (p = 0, i = len - j; i < len; i++) y[p++] = i; + for (i = 0; i < len; i++) if (_array[i] >= j) y[p++] = _array[i] - j; + for (i = 0; i < len; i++) wv[i] = x[y[i]]; + for (i = 0; i < max_len; i++) ws[i] = 0; + for (i = 0; i < len; i++) ws[wv[i]]++; + for (i = 1; i < max_len; i++) ws[i] += ws[i - 1]; + for (i = len - 1; i >= 0; i--) _array[--ws[wv[i]]] = y[i]; + for (t = x, x = y, y = t, p = 1, x[_array[0]] = 0, i = 1; i < len; i++) + x[_array[i]] = cmp(y, _array[i - 1], _array[i], j) ? p - 1 : p++; + } + return; + } + + const std::vector suffixArray()const{ + return _array; + } + private: + bool cmp(int *arr, int a, int b, int l){ + return arr[a] == arr[b] && arr[a + l] == arr[b + l]; + } + }; +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index b3c6c24..d8cba9a 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -83,6 +83,10 @@ + + + + @@ -102,8 +106,14 @@ + + + + + + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index 08403ec..97e7b98 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -33,6 +33,18 @@ Test + + Test + + + Test + + + Test + + + Test + @@ -104,6 +116,24 @@ Test + + Test + + + Test + + + Test + + + Test + + + 头文件 + + + Test +