1、最大公共子串

dp[i][j] 表示 s1 以 i 结尾和 s2 以 j 结尾的最大公共子串

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    int m = s1.size(), n = s2.size();
    int ans = 0;
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
    for(int i = 1; i <= m; i ++)
        for (int j = 1; j <= n; j++) {
            if (s1[i - 1] == s2[j - 1])
                dp[i][j] = dp[i - 1][j - 1] + 1;
            else dp[i][j] = 0;
            ans = max(ans, dp[i][j]);
        }
    cout << ans << endl;
    return 0;
}

最长公共子序列

int longestCommonSubsequence(string text1, string text2) {
        int m = text1.size(), n = text2.size();
        vector<vector<int>>dp(m + 1, vector<int>(n + 1, 0));
        for(int i = 1; i <= m; i ++){
            for(int j = 1; j <= n; 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[m][n];
    }

2、后缀求值

#include <iostream>
#include <stack>

using namespace std;

int main(){
    int temp1, temp2;
    char cur;
    string str;
    getline(cin, str);
    int i = 0;
    stack<int> st;
    while (i < str.size()){
        cur = str[i];
        if (cur >= '0' && cur <= '9') st.push(cur - '0');
        else {
            temp2 = st.top();
            st.pop();
            temp1 = st.top();
            st.pop();
            if (cur == '+') st.push(temp1 + temp2);
            else if (cur == '-') st.push(temp1 - temp2);
        }
        i++;
    }
    cout << st.top();
    return 0;
}

3、哈夫曼编码

#include <iostream>
#include<queue>
#include<algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    priority_queue<int, vector<int>, greater<int>> q;
    int sum = 0, num;
    for (int i = 0; i < n; i++) {
        cin >> num;
        q.push(num);
    }
    int num1, num2;
    while (q.size() > 1) {
        num1 = q.top(); q.pop();
        num2 = q.top(); q.pop();
        sum += num1 + num2;
        q.push(num1 + num2);
    }
    cout << sum << endl;
    return 0;
}