結果

問題 No.430 文字列検索
ユーザー tentententen
提出日時 2023-04-05 18:57:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 2,619 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 85,408 KB
実行使用メモリ 86,384 KB
最終ジャッジ日時 2024-04-10 01:16:46
合計ジャッジ時間 6,646 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
50,492 KB
testcase_01 AC 395 ms
86,384 KB
testcase_02 AC 175 ms
60,816 KB
testcase_03 AC 205 ms
61,560 KB
testcase_04 AC 49 ms
50,064 KB
testcase_05 AC 49 ms
50,052 KB
testcase_06 AC 49 ms
50,384 KB
testcase_07 AC 51 ms
50,540 KB
testcase_08 AC 345 ms
83,768 KB
testcase_09 AC 51 ms
50,424 KB
testcase_10 AC 118 ms
56,800 KB
testcase_11 AC 280 ms
64,796 KB
testcase_12 AC 283 ms
64,828 KB
testcase_13 AC 275 ms
64,556 KB
testcase_14 AC 265 ms
62,364 KB
testcase_15 AC 228 ms
61,912 KB
testcase_16 AC 205 ms
61,784 KB
testcase_17 AC 231 ms
61,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0