結果

問題 No.238 Mr. K's Another Gift
ユーザー MisterMister
提出日時 2020-09-04 17:52:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,261 bytes
コンパイル時間 605 ms
コンパイル使用メモリ 76,088 KB
実行使用メモリ 8,360 KB
最終ジャッジ日時 2023-08-17 12:07:59
合計ジャッジ時間 2,925 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 5 ms
7,356 KB
testcase_06 AC 6 ms
8,200 KB
testcase_07 AC 6 ms
8,192 KB
testcase_08 AC 7 ms
8,188 KB
testcase_09 AC 7 ms
8,148 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 4 ms
5,888 KB
testcase_15 AC 6 ms
7,512 KB
testcase_16 AC 6 ms
7,936 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,544 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 7 ms
8,264 KB
testcase_27 AC 6 ms
8,360 KB
testcase_28 AC 7 ms
8,188 KB
testcase_29 AC 7 ms
8,252 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 5 ms
6,852 KB
testcase_36 AC 4 ms
5,384 KB
testcase_37 AC 18 ms
6,996 KB
testcase_38 AC 3 ms
4,984 KB
testcase_39 AC 2 ms
4,384 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

void solve() {
    std::string s;
    std::cin >> s;
    int n = s.length();

    std::string fore, back;

    auto dfs = [&](auto&& f, int l, int r, bool rem) -> bool {
        if (l == r) fore.push_back(s[l++]);
        if (l > r) {
            if (rem) back.push_back(s[r]);
            return true;
        }

        if (s[l] == s[r]) {
            if (f(f, l + 1, r - 1, rem)) {
                fore.push_back(s[l]);
                back.push_back(s[r]);
                return true;
            }
        }

        if (rem) {
            if (f(f, l, r - 1, false)) {
                fore.push_back(s[r]);
                back.push_back(s[r]);
                return true;
            }
            if (f(f, l + 1, r, false)) {
                fore.push_back(s[l]);
                back.push_back(s[l]);
                return true;
            }
        }

        return false;
    };

    if (!dfs(dfs, 0, n - 1, true)) {
        std::cout << "NA\n";
    } else {
        std::reverse(fore.begin(), fore.end());
        std::cout << fore << back << "\n";
    }
}

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

    solve();

    return 0;
}
0