//题目:最长公共子序列
//
//给定两个字符串,找到它们的最长公共子序列的长度。
//
//问题函数定义:
//
//cpp
//int longestCommonSubsequence(string text1, string text2);
//裁判测试程序样例:
//
//cpp
#include<iostream>
#include<stack>
#include <vector>
#include<string>
#include<algorithm>
#include<unordered_map>
#include <climits>
#include<queue>
#include<unordered_set>
#include<cctype>
using namespace std;
#define maxsize 100
int longestCommonSubsequence(string text1, string text2) {
vector<vector<int>> dp(text1.size() + 1, vector<int>(text2.size() + 1, 0));
for (int i = 1; i < text1.size() + 1; i++) {
for (int j = 1; j < text2.size() + 1; j++) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[text1.size()][text2.size()];
}
int main() {
string text1 = "abcde";
string text2 = "ace";
int length = longestCommonSubsequence(text1, text2);
cout << "Length of longest common subsequence: " << length << endl;
return 0;
}
//输入样例:
//
//abcde
//ace
//输出样例:
//
//Length of longest common subsequence : 3