結果

問題 No.852 連続部分文字列
ユーザー kuuso1kuuso1
提出日時 2019-07-26 23:43:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 438 ms / 3,153 ms
コード長 1,982 bytes
コンパイル時間 2,684 ms
コンパイル使用メモリ 144,844 KB
実行使用メモリ 5,392 KB
最終ジャッジ日時 2023-09-15 05:06:38
合計ジャッジ時間 8,004 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,388 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 9 ms
4,380 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 14 ms
4,384 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 12 ms
4,384 KB
testcase_18 AC 15 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 7 ms
4,380 KB
testcase_21 AC 15 ms
4,380 KB
testcase_22 AC 6 ms
4,384 KB
testcase_23 AC 11 ms
4,380 KB
testcase_24 AC 6 ms
4,380 KB
testcase_25 AC 11 ms
4,384 KB
testcase_26 AC 15 ms
4,380 KB
testcase_27 AC 10 ms
4,384 KB
testcase_28 AC 12 ms
4,380 KB
testcase_29 AC 16 ms
4,380 KB
testcase_30 AC 4 ms
4,384 KB
testcase_31 AC 16 ms
4,384 KB
testcase_32 AC 17 ms
4,380 KB
testcase_33 AC 437 ms
5,092 KB
testcase_34 AC 438 ms
5,148 KB
testcase_35 AC 438 ms
5,144 KB
testcase_36 AC 437 ms
5,088 KB
testcase_37 AC 438 ms
5,204 KB
testcase_38 AC 438 ms
5,148 KB
testcase_39 AC 437 ms
5,156 KB
testcase_40 AC 437 ms
5,208 KB
testcase_41 AC 437 ms
5,204 KB
testcase_42 AC 293 ms
5,392 KB
testcase_43 AC 352 ms
5,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <bits/stdc++.h>
#include <time.h>
#include <sys/timeb.h>
#include <cstdio>
#include <sys/time.h>

using namespace std;
#define ll long long
#define uint unsigned int
#define ulong unsigned long long int


class Timer{
public:
	Timer(){
		cycle_per_sec = (ulong) 2.7e9; 
	}
	void Start(){
		begin_cycle = getCycle();
	}
	double getTime(){
		return (double)(getCycle() - begin_cycle) / cycle_per_sec;
	}
	int elapsedMilliseconds(){
		return (int)( 1000 * getTime() );
	}

	ulong getCycle(){
		uint low, high;
		__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
		return ((ulong) low) | ((ulong) high << 32);
	}
private:
	ulong begin_cycle;
	ulong cycle_per_sec;
};

Timer T;
ll Now(){return (ll) T.elapsedMilliseconds();}



template<typename T> bool InRange(T t, T l, T r){ return l <= t && t < r; }
string S;
int N;

double calc(int len){
	int dl[256]; for(int i=0;i<256;i++) dl[i] = 0;
	int dr[256]; for(int i=0;i<256;i++) dr[i] = 0;
	int lcnt = 0;
	int rcnt = 0;
	
	double cnt = 0;
	int l = 0, r = 0;
	for(int i=0;i<N;i++){
		if(l < i) l = i;
		if(r < i) r = i;
		while(l < N){
			if(dl[S[l]] > 0){
				dl[S[l]]++;
				l++;
				continue;
			}
			if(lcnt < len - 1){
				dl[S[l]] = 1;
				lcnt++;
				l++;
				continue;
			}
			break;
		}
		while(r < N){
			if(dr[S[r]] > 0){
				dr[S[r]]++;
				r++;
				continue;
			}
			if(rcnt < len){
				dr[S[r]] = 1;
				rcnt++;
				r++;
				continue;
			}
			break;
		}
		if(rcnt == len){
			cnt += r - l;
		}
		if(dl[S[i]] > 0){
			dl[S[i]]--;
			if(dl[S[i]] == 0) lcnt--;
		}
		if(dr[S[i]] > 0){
			dr[S[i]]--;
			if(dr[S[i]] == 0) rcnt--;
		}
	}
	//Console.WriteLine("len:{0}, cnt:{1}",len, cnt);
	
	double tot = (double) N * (double) (N + 1) / 2.0;
	return (double) len * cnt / tot;
	
}






int main(){
	T.Start();
	
	cin >> S;
	N = S.length();
	double ret = 0;
	for(int i=1;i<=26;i++){
		ret += calc(i);
	}
	printf("%.12f\n",ret);
	// cerr << "elapsed:" << Now() << " [ms]" << endl;
	
    return 0;
}
0