結果

問題 No.852 連続部分文字列
ユーザー MarcusAureliusAntoninusMarcusAureliusAntoninus
提出日時 2019-07-26 21:42:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 314 ms / 3,153 ms
コード長 1,591 bytes
コンパイル時間 2,204 ms
コンパイル使用メモリ 202,808 KB
実行使用メモリ 106,000 KB
最終ジャッジ日時 2023-09-15 00:56:24
合計ジャッジ時間 7,294 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 5 ms
4,920 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 5 ms
5,916 KB
testcase_16 AC 4 ms
4,884 KB
testcase_17 AC 5 ms
5,372 KB
testcase_18 AC 7 ms
6,312 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 7 ms
6,164 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 5 ms
5,152 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 5 ms
5,148 KB
testcase_26 AC 6 ms
5,896 KB
testcase_27 AC 4 ms
5,076 KB
testcase_28 AC 5 ms
5,376 KB
testcase_29 AC 7 ms
6,236 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 7 ms
6,212 KB
testcase_32 AC 6 ms
6,344 KB
testcase_33 AC 314 ms
105,716 KB
testcase_34 AC 286 ms
105,680 KB
testcase_35 AC 283 ms
105,752 KB
testcase_36 AC 283 ms
105,672 KB
testcase_37 AC 282 ms
105,656 KB
testcase_38 AC 281 ms
106,000 KB
testcase_39 AC 281 ms
105,844 KB
testcase_40 AC 280 ms
105,728 KB
testcase_41 AC 279 ms
105,680 KB
testcase_42 AC 277 ms
105,768 KB
testcase_43 AC 282 ms
105,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

///////////////////////
// 部分列オートマトン //
//////////////////////

// 文字列の連続とは限らない部分列を重複なく列挙するオートマトンを行列の形式で作成する
// ret[i][j]=kはi番目の文字のあとに文字'a'+jがくる場合kに遷移することを意味する
// ただしインデックスは引数の文字列の前後に番兵を置いた文字列S'と一致している
// 例えばS="abcbca"ならば
// S' = $ a b c b c a $
// ret= 1 6 6 6 6 6 7 7
//      2 2 4 4 7 7 7 7
//      3 3 3 5 5 7 7 7
// となる(ここではabcの3文字のみで考えたが、実際は26文字のものが作成される)

std::vector<std::array<int, 26>> subsequenceAutomaton(const std::string& S, const char initial_char = 'a')
{
	std::vector<std::array<int, 26>> table(S.size() + 2);
	std::fill(table.back().begin(), table.back().end(), (int)table.size() - 1);
	table[table.size() - 2] = table.back();

	for (int s_i{(int)S.size() - 1}; s_i >= 0; s_i--)
	{
		std::copy(table[s_i + 1].begin(), table[s_i + 1].end(), table[s_i].begin());
		table[s_i][S[s_i] - initial_char] = s_i + 1;
	}
	return std::move(table);
}


int main()
{
	std::string S;
	std::cin >> S;
	auto table{subsequenceAutomaton(S)};
	int64_t count{};
	for (int c_i{}; c_i < 26; c_i++)
		for (int s_i{}; s_i < (int)S.size(); s_i++)
			count += (int)S.size() - table[s_i][c_i] + 1;
	long double ans{(long double)count};
	ans /= ((int64_t)S.size() + 1) * S.size() / 2;
	std::cout << std::setprecision(10) << std::fixed << ans << std::endl;

	return 0;
}
0