結果

問題 No.852 連続部分文字列
ユーザー None
提出日時 2021-03-03 17:01:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 156 ms / 3,153 ms
コード長 1,086 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 116,192 KB
最終ジャッジ日時 2024-10-03 06:10:38
合計ジャッジ時間 6,076 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

def SameRange(A):
    N=len(A)
    pre_same=[-1]*N
    next_same=[N]*N
    last_pos = defaultdict(lambda: -1)
    for i, a in enumerate(A):
        j = last_pos[a]
        if j==-1:
            last_pos[a] = i
        else:
            next_same[j] = i
            pre_same[i] = j
            last_pos[a] = i
    return pre_same, next_same

def count_pair(i,l,r):
    """
    部分列 [l,r] の中で、i を含む部分列の個数
    """
    return (i-l+1)*(r-i+1)


#######################################################################################
def example():
    global input
    example = iter(
        """
7
1 1 3 3 3 2 2
        """
            .strip().split("\n"))
    input = lambda: next(example)

#######################################################################################
import sys
input = sys.stdin.readline
from collections import defaultdict

# example()

A=[]
for s in input().rstrip():
    A.append(ord(s)-ord("a"))
N=len(A)
cnt=0
L,R = SameRange(A)
for i in range(N):
    l,r=L[i],R[i]
    cnt+=count_pair(i,l+1,N-1)

T=N*(N+1)//2
print(cnt/T)
0