結果

問題 No.430 文字列検索
ユーザー tentententen
提出日時 2021-11-11 16:55:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 386 ms / 2,000 ms
コード長 2,141 bytes
コンパイル時間 2,433 ms
コンパイル使用メモリ 78,428 KB
実行使用メモリ 74,224 KB
最終ジャッジ日時 2024-05-02 11:09:16
合計ジャッジ時間 6,663 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
36,820 KB
testcase_01 AC 386 ms
74,224 KB
testcase_02 AC 171 ms
48,924 KB
testcase_03 AC 195 ms
49,708 KB
testcase_04 AC 44 ms
37,240 KB
testcase_05 AC 44 ms
37,116 KB
testcase_06 AC 45 ms
37,316 KB
testcase_07 AC 44 ms
36,948 KB
testcase_08 AC 337 ms
72,952 KB
testcase_09 AC 49 ms
37,304 KB
testcase_10 AC 115 ms
45,416 KB
testcase_11 AC 263 ms
53,132 KB
testcase_12 AC 267 ms
53,100 KB
testcase_13 AC 286 ms
53,624 KB
testcase_14 AC 254 ms
51,836 KB
testcase_15 AC 226 ms
51,172 KB
testcase_16 AC 205 ms
50,068 KB
testcase_17 AC 221 ms
50,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        char[] value = sc.next().toCharArray();
        int length = value.length;
        long[][] hashes = new long[10][length];
        ArrayList<HashMap<Long, Integer>> counts = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            counts.add(new HashMap<>());
        }
        for (int i = 0; i < length; i++) {
            hashes[0][i] = value[i] - 'A';
            counts.get(0).put(hashes[0][i], counts.get(0).getOrDefault(hashes[0][i], 0) + 1);
        }
        for (int i = 1; i < 10; i++) {
            for (int j = 0; j < length - i; j++) {
                hashes[i][j] = hashes[i - 1][j] * 26 + hashes[0][j + i];
                counts.get(i).put(hashes[i][j], counts.get(i).getOrDefault(hashes[i][j], 0) + 1);
            }
        }
        int m = sc.nextInt();
        int ans = 0;
        for (int i = 0; i < m; i++) {
            String s = sc.next();
            ans += counts.get(s.length() - 1).getOrDefault(getHash(s), 0);
        }
        System.out.println(ans);
    }
    
    static long getHash(String s) {
        long hash = 0;
        for (char c : s.toCharArray()) {
            hash *= 26;
            hash += c - 'A';
        }
        return hash;
    }
}
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