結果

問題 No.1171 Runs in Subsequences
ユーザー drken1215drken1215
提出日時 2020-08-14 21:58:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 4,012 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 169,220 KB
実行使用メモリ 100,352 KB
最終ジャッジ日時 2024-04-18 21:44:53
合計ジャッジ時間 3,605 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 140 ms
97,004 KB
testcase_16 AC 136 ms
95,616 KB
testcase_17 AC 141 ms
100,352 KB
testcase_18 AC 146 ms
100,352 KB
testcase_19 AC 147 ms
100,352 KB
testcase_20 AC 142 ms
100,352 KB
testcase_21 AC 137 ms
100,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }



template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
using mint = Fp<1000000007>;

int main() {
    string S;
    cin >> S;
    int N = S.size();

    vector<vector<mint>> dp(N+1, vector<mint>(26, 0));
    dp[1][S[0]-'a'] = 1;
    auto num = dp;
    for (int i = 1; i < N; ++i) {
        for (int c = 0; c < 26; ++c) {
            num[i+1][c] += num[i][c];
            num[i+1][S[i]-'a'] += num[i][c];
        }
        num[i+1][S[i]-'a'] += 1;
        for (int c = 0; c < 26; ++c) {
            dp[i+1][c] += dp[i][c];
            if (c == S[i]-'a') {
                dp[i+1][S[i]-'a'] += dp[i][c];
            }
            else {
                dp[i+1][S[i]-'a'] += dp[i][c] + num[i][c];
            }
        }
        dp[i+1][S[i]-'a'] += 1;
    }

    /*
    for (int i = 1; i <= N; ++i) {
        for (int c = 0; c < 26; ++c) {
            if (dp[i][c] != 0) {
                cout << i << ", " << (char)('a'+c) << ": " << dp[i][c] << "," << num[i][c] << endl;
            }

        }
    }
    */
    mint res = 0;
    for (int c = 0; c < 26; ++c) res += dp[N][c];
    cout << res << endl;
}















0