結果

問題 No.1725 [Cherry 3rd Tune D] 無言の言葉
ユーザー KoDKoD
提出日時 2021-10-29 21:51:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 4,000 ms
コード長 3,181 bytes
コンパイル時間 4,293 ms
コンパイル使用メモリ 284,792 KB
実行使用メモリ 44,160 KB
最終ジャッジ日時 2024-04-16 14:37:41
合計ジャッジ時間 6,881 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 7 ms
5,504 KB
testcase_14 AC 24 ms
5,504 KB
testcase_15 AC 7 ms
6,016 KB
testcase_16 AC 8 ms
6,272 KB
testcase_17 AC 13 ms
6,656 KB
testcase_18 AC 27 ms
5,376 KB
testcase_19 AC 10 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 25 ms
5,376 KB
testcase_22 AC 42 ms
16,384 KB
testcase_23 AC 41 ms
14,976 KB
testcase_24 AC 26 ms
11,520 KB
testcase_25 AC 53 ms
23,808 KB
testcase_26 AC 40 ms
18,944 KB
testcase_27 AC 44 ms
26,624 KB
testcase_28 AC 57 ms
25,344 KB
testcase_29 AC 56 ms
24,320 KB
testcase_30 AC 54 ms
28,800 KB
testcase_31 AC 33 ms
14,976 KB
testcase_32 AC 70 ms
38,784 KB
testcase_33 AC 73 ms
39,552 KB
testcase_34 AC 68 ms
36,864 KB
testcase_35 AC 65 ms
32,896 KB
testcase_36 AC 68 ms
36,224 KB
testcase_37 AC 42 ms
5,376 KB
testcase_38 AC 43 ms
5,376 KB
testcase_39 AC 43 ms
5,376 KB
testcase_40 AC 42 ms
5,376 KB
testcase_41 AC 42 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 38 ms
44,160 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