跳到正文

个人记录

dfhds

SEF_711约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年5月24日
更新
2026年5月24日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;

const int kN = 1e4 + 5;
int n, m;
string ans;
int a[kN], b[kN];
bool dp[kN][kN];
void print(int x, int y) {
    if (x == 0) {
        return;
    }
    if (y >= a[x] && dp[x - 1][y - a[x]] == 1) {
        print(x - 1, y - a[x]);
        ans += 'H';
    }
    else if (y >= b[x] && dp[x - 1][y - b[x]] == 1) {
        print(x - 1, y - b[x]);
        ans += 'T';
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
    }
    dp[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if(dp[i - 1][j] == 1) {
                dp[i][j + a[i]] = 1;
                dp[i][j + b[i]] = 1;
            }
        }
    }
    if(dp[n][m] == 1) {
        cout << "Yes\n";
        print(n, m);
        cout << ans;
    }
    else {
        cout << "No";
    }
    return 0;
}
原文