結果

問題 No.852 連続部分文字列
ユーザー tentententen
提出日時 2021-11-04 09:23:12
言語 Java
(openjdk 23)
結果
AC  
実行時間 771 ms / 3,153 ms
コード長 1,741 bytes
コンパイル時間 4,627 ms
コンパイル使用メモリ 77,960 KB
実行使用メモリ 106,556 KB
最終ジャッジ日時 2024-10-14 05:54:56
合計ジャッジ時間 15,736 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static int n;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        char[] values = sc.next().toCharArray();
        int length = values.length;
        ArrayList<TreeSet<Integer>> alpha = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            alpha.add(new TreeSet<>());
            alpha.get(i).add(length + 1);
        }
        for (int i = 0; i < length; i++) {
            alpha.get(values[i] - 'a').add(i + 1);
        }
        long count = 0;
        long base = (long)length * (length + 1) / 2;
        for (TreeSet<Integer> set : alpha) {
            int prev = 0;
            long tmp = 0;
            for (int x : set) {
                tmp += (long)(x - prev - 1) * (x - prev) / 2;
                prev = x;
            }
            count += base - tmp;
        }
        System.out.println((double)count / base);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0