summaryrefslogtreecommitdiff
path: root/sonstiges/SufixArray.cpp
diff options
context:
space:
mode:
authorJBatzill <batzilljohannes@gmail.com>2014-11-20 15:35:18 +0100
committerJBatzill <batzilljohannes@gmail.com>2014-11-20 15:35:18 +0100
commit6a062d45137b95cd550ea79d97deceefd0a75ac6 (patch)
tree60aa8691ca951a549988c3d2690a274417276515 /sonstiges/SufixArray.cpp
parent1053eaa33bf30c46549188f559d59edcd200d0a5 (diff)
Rename LCSOS.cpp to SufixArray.cpp
Diffstat (limited to 'sonstiges/SufixArray.cpp')
-rw-r--r--sonstiges/SufixArray.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/sonstiges/SufixArray.cpp b/sonstiges/SufixArray.cpp
new file mode 100644
index 0000000..103e503
--- /dev/null
+++ b/sonstiges/SufixArray.cpp
@@ -0,0 +1,22 @@
+//longest common substring in one string (overlapping not excluded)
+string lsubs(string s) {
+ if(s.length() == 0) return "";
+ vector<int> a(s.length());
+ for(int k = 0; k < a.size(); k++) a[k] = k;
+ sort(a.begin(), a.end(), [&] (const int &u, const int &l) {
+ int ui = u, li = l;
+ while(ui < s.length() && li < s.length()) {
+ if(s[ui] < s[li]) return true;
+ else if(s[ui] > s[li]) return false;
+ ui++; li++;
+ }
+ return !(ui < s.length());
+ });
+ int r = 0, m=0, c=0;
+ for(int i = 0; i < a.size() - 1; i++) {
+ c = 0;
+ while(a[i]+c < s.length() && a[i+1]+c < s.length() && s[a[i]+c] == s[a[i+1]+c]) c++;
+ if(c > m) r=i, m=c;
+ }
+ return m == 0 ? "" : s.substr(a[r], m);
+}