結果

問題 No.430 文字列検索
ユーザー hermione17hermione17
提出日時 2016-10-03 00:11:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 767 ms / 2,000 ms
コード長 1,157 bytes
コンパイル時間 4,505 ms
コンパイル使用メモリ 75,140 KB
実行使用メモリ 88,252 KB
最終ジャッジ日時 2023-08-15 17:33:05
合計ジャッジ時間 12,195 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,220 KB
testcase_01 AC 767 ms
88,252 KB
testcase_02 AC 437 ms
60,344 KB
testcase_03 AC 448 ms
60,704 KB
testcase_04 AC 125 ms
55,560 KB
testcase_05 AC 127 ms
55,628 KB
testcase_06 AC 126 ms
55,808 KB
testcase_07 AC 126 ms
55,848 KB
testcase_08 AC 621 ms
84,916 KB
testcase_09 AC 136 ms
55,680 KB
testcase_10 AC 223 ms
58,128 KB
testcase_11 AC 521 ms
64,888 KB
testcase_12 AC 515 ms
65,224 KB
testcase_13 AC 506 ms
65,564 KB
testcase_14 AC 478 ms
63,048 KB
testcase_15 AC 472 ms
63,072 KB
testcase_16 AC 437 ms
60,588 KB
testcase_17 AC 438 ms
60,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Scanner;


public class Y430 {
	Y430() throws Exception {
		Scanner in = new Scanner(System.in);
		PrintStream out = new PrintStream(System.out);
		
		String s = in.next();
		int m = in.nextInt();
		String[] w = new String[m];
		
		for (int i = 0; i < m; i++) {
			w[i] = in.next();
		}
		
		HashMap<Long, Integer>[] hashMap = new HashMap[11];
		
		for (int i = 0; i < 11; i++) {
			hashMap[i] = new HashMap();			
		}
		
		for (int i = 0; i < s.length(); i++) {
			for (int j = 1; j <= 10 && i + j <= s.length(); j++) {
				Long x = encode(s.substring(i,  i+j));
				Integer g = hashMap[j].get(x);
				
				if (g == null) g = 0;
				g += 1;
				hashMap[j].put(x, g);
			}
		}
		
		int answer = 0;
		for (int i = 0; i < m; i++) {
			Long x = encode(w[i]);
			Integer g = hashMap[w[i].length()].get(x);
			if (g != null) answer += g;
		}

		
		out.println(answer);
	}
	
	long encode(String s) {
		long x = 0;
		for (int i = 0; i < s.length(); i++) {
			x = x * 26 + (int)s.charAt(i) - 'A';
		}
		return x;
	}

	public static void main(String argv[]) throws Exception {
		new Y430();
	}
}
0