diff options
| author | JBatzill <batzilljohannes@gmail.com> | 2014-11-20 15:31:04 +0100 |
|---|---|---|
| committer | JBatzill <batzilljohannes@gmail.com> | 2014-11-20 15:31:04 +0100 |
| commit | 0a62776ef5a36754448239506827e6290d4a312f (patch) | |
| tree | 924249bdf58e2297b5b7e8770ac6f30536126eda | |
| parent | f0136de737adaa8b95e81aa17a1288d996be6268 (diff) | |
created and tested longest substring
tested with: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3664
| -rw-r--r-- | sonstiges/LCSOS.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/sonstiges/LCSOS.cpp b/sonstiges/LCSOS.cpp new file mode 100644 index 0000000..103e503 --- /dev/null +++ b/sonstiges/LCSOS.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); +} |
