結果

問題 No.2947 Sing a Song
ユーザー zawakasu
提出日時 2024-10-25 21:34:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 1,343 bytes
コンパイル時間 890 ms
コンパイル使用メモリ 81,652 KB
最終ジャッジ日時 2025-02-24 22:50:18
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13 RE * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cassert>
#include <string>
#include <vector>

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int N;
    std::string S, T;
    std::cin >> N >> S >> T;
    const int MAX{200010};
    std::vector<int> dp(MAX, -1), recov(MAX, -1);
    dp[0] = 0;
    for (int i{} ; i < MAX ; i++) {
        if (i + (int)S.size() < MAX and dp[i + S.size()] < dp[i] + 1) {
            dp[i + S.size()] = dp[i] + 1;
            recov[i + S.size()] = 0;
        }
        if (i + (int)T.size() < MAX and dp[i + T.size()] < dp[i]) {
            dp[i + T.size()] = dp[i];
            recov[i + T.size()] = 1;
        }
    }
    auto get{[&](int x) -> std::vector<std::string> {
        std::vector<std::string> res;
        while (x) {
            if (recov[x] == 0) {
                res.push_back(S);
                x -= (int)S.size();
            }
            else if (recov[x] == 1) {
                res.push_back(T);
                x -= (int)T.size();
            }
            else {
                assert(false);
            }
        }
        return res;
    }};
    for (int i{} ; i < N ; i++) {
        int A;
        std::cin >> A;
        auto res{get(A)};
        for (int j{} ; j < (int)res.size() ; j++) std::cout << res[j] << (j + 1 == (int)res.size() ? '\n' : ' ');
    }
}
0