結果

問題 No.430 文字列検索
ユーザー TatsuyaaaaTatsuyaaaa
提出日時 2019-12-15 01:18:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,306 ms / 2,000 ms
コード長 901 bytes
コンパイル時間 3,443 ms
コンパイル使用メモリ 73,712 KB
実行使用メモリ 62,232 KB
最終ジャッジ日時 2023-09-10 18:45:42
合計ジャッジ時間 17,449 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,748 KB
testcase_01 AC 1,267 ms
60,468 KB
testcase_02 AC 1,191 ms
60,324 KB
testcase_03 AC 1,188 ms
60,444 KB
testcase_04 AC 120 ms
55,812 KB
testcase_05 AC 121 ms
56,068 KB
testcase_06 AC 120 ms
56,268 KB
testcase_07 AC 121 ms
56,308 KB
testcase_08 AC 211 ms
56,392 KB
testcase_09 AC 132 ms
55,756 KB
testcase_10 AC 161 ms
55,800 KB
testcase_11 AC 1,118 ms
60,588 KB
testcase_12 AC 1,207 ms
60,240 KB
testcase_13 AC 1,118 ms
58,308 KB
testcase_14 AC 1,118 ms
60,616 KB
testcase_15 AC 1,306 ms
60,176 KB
testcase_16 AC 1,219 ms
60,144 KB
testcase_17 AC 1,295 ms
62,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {

static long B = 1_000_000_007L;

static int contain(String a, String b) {
    int val = 0;
    int al = a.length();
    int bl = b.length();
    if (al>bl) return val;

    long t = 1;
    for (int i=0;i<al;i++) t*=B;

    long ah = 0;
    long bh = 0;
    for (int i=0;i<al;i++) ah=ah*B+(int)a.charAt(i);
    for (int i=0;i<al;i++) bh=bh*B+(int)b.charAt(i);

    for (int i=0;i+al<=bl;i++) {
        if (ah==bh) {
            val++;
        }
        if (i+al<bl) bh=bh*B+(int)b.charAt(i+al)-(int)b.charAt(i)*t;
    }
    return val;

}
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String S = sc.next();
        int M = sc.nextInt();
        int ans = 0;
        for (int i=0;i<M;i++) {
            String C = sc.next();
            ans += contain(C, S);
        }
        System.out.println(ans);
    }
}
0