結果

問題 No.852 連続部分文字列
ユーザー Mister
提出日時 2020-04-27 11:45:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 277 ms / 3,153 ms
コード長 1,099 bytes
コンパイル時間 827 ms
コンパイル使用メモリ 79,720 KB
最終ジャッジ日時 2025-01-10 02:27:48
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ldouble = long double;
using lint = long long;

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

    std::vector<lint> sum(27, 0);
    for (int k = 0; k <= 26; ++k) {
        std::vector<int> cnt(26, 0);
        int kind = 0;
        int r = 0;

        for (int l = 0; l < n; ++l) {
            while (r < n && kind <= k) {
                int c = s[r] - 'a';
                if (cnt[c] == 0) ++kind;
                ++cnt[c];
                ++r;
            }

            sum[k] += r - l - (kind > k);

            {
                int c = s[l] - 'a';
                --cnt[c];
                if (cnt[c] == 0) --kind;
            }
        }
    }

    lint ans = 0;
    for (int k = 1; k <= 26; ++k) {
        ans += (sum[k] - sum[k - 1]) * k;
    }

    std::cout << std::fixed << std::setprecision(10)
              << ldouble(ans) / (n * (n + 1) / 2) << std::endl;
}

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

    solve();

    return 0;
}
0