結果

問題 No.1725 [Cherry 3rd Tune D] 無言の言葉
ユーザー KoDKoD
提出日時 2021-10-29 21:51:33
言語 C++23(draft)
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 4,000 ms
コード長 3,181 bytes
コンパイル時間 3,264 ms
コンパイル使用メモリ 254,056 KB
実行使用メモリ 44,196 KB
最終ジャッジ日時 2023-07-29 07:39:17
合計ジャッジ時間 7,080 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 5 ms
5,364 KB
testcase_13 AC 5 ms
5,632 KB
testcase_14 AC 20 ms
5,300 KB
testcase_15 AC 7 ms
5,896 KB
testcase_16 AC 7 ms
6,260 KB
testcase_17 AC 10 ms
6,564 KB
testcase_18 AC 23 ms
5,072 KB
testcase_19 AC 8 ms
4,768 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 21 ms
4,376 KB
testcase_22 AC 35 ms
16,280 KB
testcase_23 AC 35 ms
14,888 KB
testcase_24 AC 24 ms
11,256 KB
testcase_25 AC 44 ms
23,652 KB
testcase_26 AC 34 ms
18,916 KB
testcase_27 AC 37 ms
26,496 KB
testcase_28 AC 52 ms
25,192 KB
testcase_29 AC 49 ms
24,140 KB
testcase_30 AC 49 ms
28,736 KB
testcase_31 AC 29 ms
14,972 KB
testcase_32 AC 63 ms
38,404 KB
testcase_33 AC 64 ms
39,508 KB
testcase_34 AC 61 ms
36,520 KB
testcase_35 AC 55 ms
32,808 KB
testcase_36 AC 58 ms
35,996 KB
testcase_37 AC 39 ms
4,380 KB
testcase_38 AC 39 ms
4,380 KB
testcase_39 AC 39 ms
4,380 KB
testcase_40 AC 40 ms
4,376 KB
testcase_41 AC 39 ms
4,380 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 35 ms
44,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class F> struct RecursiveLambda : private F {
    explicit constexpr RecursiveLambda(F&& f) : F(std::forward<F>(f)) {}
    template <class... Args> constexpr decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class F> constexpr decltype(auto) rec_lambda(F&& f) {
    using G = std::decay_t<F>;
    return RecursiveLambda<G>(std::forward<G>(f));
}

template <class T> using Vec = std::vector<T>;

std::array<Vec<usize>, 26> count(const std::string& s) {
    const auto n = s.size();
    std::array<Vec<usize>, 26> ret;
    for (const auto k : rep(0, 26)) {
        const char c = 'a' + k;
        ret[k] = Vec<usize>(n + 1);
        for (const auto i : rep(0, n)) {
            ret[k][i + 1] = ret[k][i] + (s[i] == c);
        }
    }
    return ret;
}

void main_() {
    std::string X, Y;
    std::cin >> X >> Y;
    usize Q;
    std::cin >> Q;
    Vec<usize> len;
    len.push_back(X.size());
    while (len.back() < 1000000000) {
        len.push_back(len.back() + Y.size() + len.back());
    }
    const auto cntX = count(X);
    const auto cntY = count(Y);
    std::array<usize, 26> allX, allY;
    for (const auto i : rep(0, 26)) {
        allX[i] = cntX[i][X.size()];
        allY[i] = cntY[i][Y.size()];
    }
    Vec<std::array<usize, 26>> whole(len.size());
    whole[0] = allX;
    for (const auto i : rep(1, len.size())) {
        for (const auto k : rep(0, 26)) {
            whole[i][k] = 2 * whole[i - 1][k] + allY[k];
        }
    }
    const auto dfs = rec_lambda([&](auto&& dfs, const usize level, usize r, const usize k) -> usize {
        if (r == len[level]) return whole[level][k];
        if (level == 0) return cntX[k][r];
        if (r <= len[level - 1]) return dfs(level - 1, r, k);
        r -= len[level - 1];
        usize ret = whole[level - 1][k];
        if (r <= Y.size()) return ret + cntY[k][r];
        r -= Y.size();
        ret += allY[k];
        return ret + whole[level - 1][k] - dfs(level - 1, len[level - 1] - r, k);
    });
    while (Q--) {
        usize l, r;
        char c;
        std::cin >> l >> r >> c;
        std::cout << dfs(len.size() - 1, r, c - 'a') - dfs(len.size() - 1, l - 1, c - 'a') << '\n';
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    main_();
    return 0;
}
0