diff options
| author | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
|---|---|---|
| committer | mzuenni <michi.zuendorf@gmail.com> | 2022-06-27 17:19:28 +0200 |
| commit | 5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch) | |
| tree | ed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /string/longestCommonSubsequence.cpp | |
| parent | adabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff) | |
updated tcr
Diffstat (limited to 'string/longestCommonSubsequence.cpp')
| -rw-r--r-- | string/longestCommonSubsequence.cpp | 14 |
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++; |
