結果
| 問題 |
No.852 連続部分文字列
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-07-26 21:59:11 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 45 ms / 3,153 ms |
| コード長 | 1,236 bytes |
| コンパイル時間 | 1,542 ms |
| コンパイル使用メモリ | 170,084 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-02 07:11:56 |
| 合計ジャッジ時間 | 3,345 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll calc(string& s, char c) {
ll n = s.size();
ll res = n * (n + 1) / 2;
ll cur = 0;
for (ll i = 0; i < n; i++) {
if (s[i] == c) {
ll d = i - cur;
res -= d * (d + 1) / 2;
cur = i + 1;
}
}
ll d = n - cur;
res -= d * (d + 1) / 2;
return res;
}
ll calc2(string& s) {
int n = s.size();
vector<int> nex(26, -1);
vector<ll> dp(n, 0);
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < 26; j++) {
if (nex[j] == -1) continue;
dp[i] += dp[nex[j]];
}
int& tar = nex[s[i] - 'a'];
if (tar == -1) dp[i]++;
tar = i;
}
ll res = 0;
for (int i = 0; i < 26; i++) {
if (nex[i] != -1) res += dp[nex[i]];
}
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> s;
ll n = s.size();
ll val1 = 0;
for (int i = 0; i < 26; i++) {
val1 += calc(s, 'a' + i);
}
ll val2 = n * (n + 1) / 2;//calc2(s);
//cerr << val2 << endl;
cout << fixed << setprecision(15) << (long double)val1 / val2 << endl;
return 0;
}