summaryrefslogtreecommitdiff
path: root/content/string/longestCommonSubsequence.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'content/string/longestCommonSubsequence.cpp')
-rw-r--r--content/string/longestCommonSubsequence.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/content/string/longestCommonSubsequence.cpp b/content/string/longestCommonSubsequence.cpp
new file mode 100644
index 0000000..6c9ea44
--- /dev/null
+++ b/content/string/longestCommonSubsequence.cpp
@@ -0,0 +1,15 @@
+string lcss(const string& a, const string& b) {
+ vector<vector<int>> m(sz(a) + 1, vector<int>(sz(b) + 1));
+ for (int i = sz(a) - 1; i >= 0; i--) {
+ for (int j = sz(b) - 1; j >= 0; j--) {
+ if (a[i] == b[j]) m[i][j] = 1 + m[i+1][j+1];
+ else m[i][j] = max(m[i+1][j], m[i][j+1]);
+ }} // Für die Länge: return m[0][0];
+ string res;
+ for (int j = 0, i = 0; j < sz(b) && i < sz(a);) {
+ if (a[i] == b[j]) res += a[i++], j++;
+ else if (m[i][j+1] > m[i+1][j]) j++;
+ else i++;
+ }
+ return res;
+}