結果

問題 No.852 連続部分文字列
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2019-07-26 21:45:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,030 ms / 3,153 ms
コード長 2,592 bytes
コンパイル時間 2,276 ms
コンパイル使用メモリ 169,472 KB
実行使用メモリ 210,416 KB
最終ジャッジ日時 2023-09-15 01:02:07
合計ジャッジ時間 25,343 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 35 ms
6,676 KB
testcase_14 AC 17 ms
4,876 KB
testcase_15 AC 56 ms
8,972 KB
testcase_16 AC 36 ms
6,764 KB
testcase_17 AC 48 ms
8,048 KB
testcase_18 AC 61 ms
9,212 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 28 ms
6,060 KB
testcase_21 AC 62 ms
9,432 KB
testcase_22 AC 20 ms
5,296 KB
testcase_23 AC 43 ms
7,592 KB
testcase_24 AC 27 ms
5,672 KB
testcase_25 AC 40 ms
7,444 KB
testcase_26 AC 60 ms
9,240 KB
testcase_27 AC 36 ms
6,920 KB
testcase_28 AC 48 ms
8,120 KB
testcase_29 AC 62 ms
9,480 KB
testcase_30 AC 13 ms
4,448 KB
testcase_31 AC 64 ms
9,580 KB
testcase_32 AC 67 ms
9,888 KB
testcase_33 AC 2,024 ms
210,200 KB
testcase_34 AC 2,023 ms
210,192 KB
testcase_35 AC 2,020 ms
210,232 KB
testcase_36 AC 2,024 ms
210,152 KB
testcase_37 AC 2,024 ms
210,120 KB
testcase_38 AC 2,020 ms
210,188 KB
testcase_39 AC 2,022 ms
210,188 KB
testcase_40 AC 2,030 ms
210,416 KB
testcase_41 AC 2,020 ms
210,248 KB
testcase_42 AC 1,659 ms
210,220 KB
testcase_43 AC 1,475 ms
210,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
struct StringMaster {
    string S; int N; StringMaster() {}StringMaster(string s) { init(s); }
	void init(string s) { S = s; N = S.length(); initgo(); }

    // get the nearest index for the char
    vector<int> hidari[26], migi[26];
    inline void initgo() {
        rep(c, 0, 26) { migi[c].resize(N); migi[c][N - 1] = inf; }
        rrep(i, N - 2, 0) { rep(c, 0, 26) migi[c][i] = migi[c][i + 1]; migi[S[i + 1]][i] = i + 1; }
        rep(c, 0, 26) { hidari[c].resize(N); hidari[c][0] = -1; }
        rep(i, 1, N) { rep(c, 0, 26) hidari[c][i] = hidari[c][i - 1]; hidari[S[i - 1]][i] = i - 1; }
    }
    inline int gomigi(int cu, int c) { return migi[c][cu]; }
    inline int gohidari(int cu, int c) { return hidari[c][cu]; }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/














string S; int N;
bool flag[26];
//---------------------------------------------------------------------------------------------------
void _main() {
	cin >> S;
	N = S.length();
	fore(c, S) c -= 'a';
	
	StringMaster sm(S);

	double ans = 0;
	rep(L, 0, N) {
		rep(c, 0, 26) flag[c] = false;
		
		flag[S[L]] = true;
		int R = L;
		int cnt = 1;
		while (R < N) {
			pair<int, int> mi = { N, 0 };
			rep(c, 0, 26) if (!flag[c]) chmin(mi, { sm.gomigi(R, c), c });
			ans += 1.0 * cnt * (mi.first - R);
			R = mi.first;
			cnt++;
			flag[mi.second] = true;
		}
	}
	ans /= 1.0 * N * (N + 1) / 2;

	printf("%.10f\n", ans);
}



0