summaryrefslogtreecommitdiff
path: root/string/longestCommonSubsequence.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'string/longestCommonSubsequence.cpp')
-rw-r--r--string/longestCommonSubsequence.cpp14
1 files changed, 6 insertions, 8 deletions
diff --git a/string/longestCommonSubsequence.cpp b/string/longestCommonSubsequence.cpp
index 7bcf851..dd2368e 100644
--- a/string/longestCommonSubsequence.cpp
+++ b/string/longestCommonSubsequence.cpp
@@ -1,14 +1,12 @@
-// Laufzeit: O(|a|*|b|)
-string lcss(string &a, string &b) {
- int m[a.length() + 1][b.length() + 1], x=0, y=0;
- memset(m, 0, sizeof(m));
- for(int y = a.length() - 1; y >= 0; y--) {
- for(int x = b.length() - 1; x >= 0; x--) {
+string lcss(string& a, string& b) {
+ vector<vector<int>> m(a.size() + 1, vector<int>(b.size() + 1));
+ for(int y = a.size() - 1; y >= 0; y--) {
+ for(int x = b.size() - 1; x >= 0; x--) {
if(a[y] == b[x]) m[y][x] = 1 + m[y+1][x+1];
else m[y][x] = max(m[y+1][x], m[y][x+1]);
}} // Für die Länge: return m[0][0];
- string res;
- while(x < b.length() && y < a.length()) {
+ string res; int x=0; int y=0;
+ while(x < b.size() && y < a.size()) {
if(a[y] == b[x]) res += a[y++], x++;
else if(m[y][x+1] > m[y+1][x+1]) x++;
else y++;